C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)
1、word轉(zhuǎn)pdf
使用nuget:
Microsoft.Office.Interop.Word
winform頁面:

后端代碼:
//using Spire.Doc;
//using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Aspose.Words;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Word.Application;
namespace file_operations
{
public partial class word轉(zhuǎn)PDF : Form
{
public word轉(zhuǎn)PDF()
{
InitializeComponent();
//窗體居中
this.StartPosition = FormStartPosition.CenterScreen;
//無邊框
this.FormBorderStyle = FormBorderStyle.None;
//放大無效
this.MaximizeBox = false;
//版權(quán)
label4.Text = "該應(yīng)用由昔舍版權(quán)所有,如修改源碼請(qǐng)聯(lián)系15574296763@163.com,侵權(quán)后果自負(fù)!!!";
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
string file = openFileDialog.FileName.ToLower();
//獲取文件擴(kuò)展名
string extension = System.IO.Path.GetExtension(file);
if(extension != ".doc" && extension != ".docx")
{
MessageBox.Show("請(qǐng)選擇word文件", "錯(cuò)誤提示");
}
else {
textBox1.Text = file;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if(folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text = folderBrowserDialog.SelectedPath+"\\";
}
}
//保存為PDF
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0 && textBox2.Text.Length == 0 && textBox3.Text.Length ==0)
{
MessageBox.Show("請(qǐng)選擇要轉(zhuǎn)換的原文件和要保存的路徑", "錯(cuò)誤提示");
}
else
{
try
{
//創(chuàng)建一個(gè)word實(shí)例
Application wordapp = new Application();
//創(chuàng)建一個(gè)word文檔對(duì)象,并打開word文件
Document wordDoc = wordapp.Documents.Open(textBox1.Text);
//獲取文件擴(kuò)展名
string extension = System.IO.Path.GetExtension(textBox2.Text);
//設(shè)置保存路徑,保存文件名稱和文件格式
if (extension !=".pdf")
{
try
{
string savePath = textBox2.Text + textBox3.Text + ".pdf";
wordDoc.SaveAs2(savePath, WdSaveFormat.wdFormatPDF);
}
catch
{
MessageBox.Show("請(qǐng)檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示");
}
}
else
{
try
{
string savePath = textBox2.Text + textBox3.Text;
wordDoc.SaveAs2(savePath, WdSaveFormat.wdFormatPDF);
}
catch
{
MessageBox.Show("請(qǐng)檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示");
}
}
//保存以后打開文件路徑
string openfilePath = textBox2.Text;
System.Diagnostics.Process.Start(openfilePath);
}
catch (Exception ex)
{
MessageBox.Show("請(qǐng)檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示");
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PDF轉(zhuǎn)word pDF = new PDF轉(zhuǎn)word();
//隱藏本窗體
this.Hide();
//打開PDF轉(zhuǎn)word
pDF.Show();
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Close();
PDF轉(zhuǎn)word pDF = new PDF轉(zhuǎn)word();
pDF.Close();
}
}
}2、pdf轉(zhuǎn)word功能實(shí)現(xiàn):
使用nuget:
破解的Spire.pdf
下載地址:crack-spire/手動(dòng)破解Spire.PDF,已破解下載鏈接在底部.md at main · zhjunbai/crack-spire · GitHub
winform頁面:

后端代碼:
using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
using System.Threading;
namespace file_operations
{
public partial class PDF轉(zhuǎn)word : Form
{
public PDF轉(zhuǎn)word()
{
InitializeComponent();
//窗體居中
this.StartPosition = FormStartPosition.CenterScreen;
//無邊框
this.FormBorderStyle = FormBorderStyle.None;
//放大無效
this.MaximizeBox = false;
//版權(quán)
label4.Text = "該應(yīng)用由昔舍版權(quán)所有,如修改源碼請(qǐng)聯(lián)系15574296763@163.com,侵權(quán)后果自負(fù)!!!";
}
private void button1_Click(object sender, EventArgs e)
{
//獲取PDF文件
OpenFileDialog openFileDialog = new OpenFileDialog();
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
//獲取文件名
string files = openFileDialog.FileName.ToLower();
//獲取文件擴(kuò)展名
string extension = System.IO.Path.GetExtension(files);
if(extension != ".pdf")
{
MessageBox.Show("請(qǐng)選擇PDF文件", "錯(cuò)誤提示");
}
else
{
pdftext.Text = files;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog openFileDialog = new FolderBrowserDialog();
if(openFileDialog.ShowDialog() == DialogResult.OK) {
wordPath.Text = openFileDialog.SelectedPath + "\\";
}
}
private void button3_Click(object sender, EventArgs e)
{
//初始化pdfDocument實(shí)例
PdfDocument doc = new PdfDocument();
try
{
//加載PDF文檔
doc.LoadFromFile(pdftext.Text);
//保存為DOC格式文檔
string savePath = wordPath.Text + wordname.Text + ".DOC";
doc.SaveToFile(savePath, FileFormat.DOC);
Thread.Sleep(3000);
//保存以后打開文件路徑
string openfilePath = wordPath.Text;
System.Diagnostics.Process.Start(openfilePath);
}
catch
{
MessageBox.Show("請(qǐng)確定文件選擇正確", "錯(cuò)誤提示");
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Close();
word轉(zhuǎn)PDF word = new word轉(zhuǎn)PDF();
word.Close();
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
word轉(zhuǎn)PDF word = new word轉(zhuǎn)PDF();
//隱藏本窗體
this.Hide();
word.Show();
}
}
}到此這篇關(guān)于C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)C# word和pdf互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity實(shí)現(xiàn)人物旋轉(zhuǎn)和移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)人物旋轉(zhuǎn)和移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
C# wpf使用ffmpeg命令行實(shí)現(xiàn)錄屏的示例代碼
本文主要介紹了C# wpf使用ffmpeg命令行實(shí)現(xiàn)錄屏的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
C#實(shí)現(xiàn)的調(diào)用DOS命令操作類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的調(diào)用DOS命令操作類,實(shí)例分析了C#調(diào)用系統(tǒng)常用DOS命令的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#?實(shí)現(xiàn)Excel?打印與頁面設(shè)置功能(從入門到精通)
本文介紹了使用Spire.XLSfor.NET組件在C#中實(shí)現(xiàn)Excel文件的打印設(shè)置和靜默打印功能,通過本文的示例代碼,讀者可以快速將這些功能集成到自己的C#項(xiàng)目中,提高辦公效率和文檔的專業(yè)度,感興趣的朋友跟隨小編一起看看吧2026-04-04
C#中Hashtable和Dictionary的區(qū)別與用法示例
由于 Hashtable 和 Dictionary 同時(shí)存在, 在使用場(chǎng)景上必然存在選擇性, 并不任何時(shí)刻都能相互替代。所以這篇文章主要給大家介紹了關(guān)于C#中Hashtable和Dictionary區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-05-05

