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)所有,如修改源碼請聯(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("請選擇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("請選擇要轉(zhuǎn)換的原文件和要保存的路徑", "錯(cuò)誤提示"); } else { try { //創(chuàng)建一個(gè)word實(shí)例 Application wordapp = new Application(); //創(chuàng)建一個(gè)word文檔對象,并打開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("請檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示"); } } else { try { string savePath = textBox2.Text + textBox3.Text; wordDoc.SaveAs2(savePath, WdSaveFormat.wdFormatPDF); } catch { MessageBox.Show("請檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示"); } } //保存以后打開文件路徑 string openfilePath = textBox2.Text; System.Diagnostics.Process.Start(openfilePath); } catch (Exception ex) { MessageBox.Show("請檢查選擇的文件是否有效,保存的路徑是否存在", "錯(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/手動破解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)所有,如修改源碼請聯(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("請選擇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("請確定文件選擇正確", "錯(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#關(guān)于System.Collections空間詳解
這篇文章主要介紹了C#關(guān)于System.Collections空間,需要的朋友可以參考下2014-07-07通過VS中的數(shù)據(jù)源選擇對話框簡單實(shí)現(xiàn)數(shù)據(jù)庫連接配置[圖]
通過VS中的數(shù)據(jù)源選擇對話框簡單實(shí)現(xiàn)數(shù)據(jù)庫連接配置[圖]...2007-03-03Unity的BuildPlayerProcessor實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的BuildPlayerProcessor實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05C# 中 TryParse如何將字符串轉(zhuǎn)換為特定類型
在 C# 中,TryParse 是一個(gè)用于將字符串轉(zhuǎn)換為特定類型的方法,它用于嘗試解析字符串并將其轉(zhuǎn)換為指定類型的值,而不會引發(fā)異常,這篇文章主要介紹了C# 中 TryParse 將字符串轉(zhuǎn)換為特定類型的方法,需要的朋友可以參考下2024-03-03C#使用Aspose.Cells創(chuàng)建和讀取Excel文件
這篇文章主要為大家詳細(xì)介紹了C#使用Aspose.Cells創(chuàng)建和讀取Excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10