当前位置: 首页 > news >正文

C#实现word和pdf格式互转

1、word转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转PDF : Form{public word转PDF(){InitializeComponent();//窗体居中this.StartPosition = FormStartPosition.CenterScreen;//无边框this.FormBorderStyle = FormBorderStyle.None;//放大无效this.MaximizeBox = false;//版权label4.Text = "该应用由昔舍版权所有,如修改源码请联系15574296763@163.com,侵权后果自负!!!";}private void button1_Click(object sender, EventArgs e){OpenFileDialog openFileDialog = new OpenFileDialog();if(openFileDialog.ShowDialog() == DialogResult.OK){string file = openFileDialog.FileName.ToLower();//获取文件扩展名string extension = System.IO.Path.GetExtension(file);if(extension != ".doc" && extension != ".docx"){MessageBox.Show("请选择word文件", "错误提示");}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+"\\";}}//保存为PDFprivate void button3_Click(object sender, EventArgs e){if (textBox1.Text.Length == 0 && textBox2.Text.Length == 0 && textBox3.Text.Length ==0){MessageBox.Show("请选择要转换的原文件和要保存的路径", "错误提示");}else{try{//创建一个word实例Application wordapp = new Application();//创建一个word文档对象,并打开word文件Document wordDoc = wordapp.Documents.Open(textBox1.Text);//获取文件扩展名string extension = System.IO.Path.GetExtension(textBox2.Text);//设置保存路径,保存文件名称和文件格式if (extension !=".pdf"){try{string savePath = textBox2.Text + textBox3.Text + ".pdf";wordDoc.SaveAs2(savePath, WdSaveFormat.wdFormatPDF);}catch{MessageBox.Show("请检查选择的文件是否有效,保存的路径是否存在", "错误提示");}}else{try{string savePath = textBox2.Text + textBox3.Text;wordDoc.SaveAs2(savePath, WdSaveFormat.wdFormatPDF);}catch{MessageBox.Show("请检查选择的文件是否有效,保存的路径是否存在", "错误提示");}}//保存以后打开文件路径string openfilePath = textBox2.Text;System.Diagnostics.Process.Start(openfilePath);}catch (Exception ex){MessageBox.Show("请检查选择的文件是否有效,保存的路径是否存在", "错误提示");}}}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){PDF转word pDF = new PDF转word();//隐藏本窗体this.Hide();//打开PDF转wordpDF.Show();}private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){this.Close();PDF转word pDF = new PDF转word();pDF.Close();}}
}

2、pdf转word功能实现:

使用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转word : Form{public PDF转word(){InitializeComponent();//窗体居中this.StartPosition = FormStartPosition.CenterScreen;//无边框this.FormBorderStyle = FormBorderStyle.None;//放大无效this.MaximizeBox = false;//版权label4.Text = "该应用由昔舍版权所有,如修改源码请联系15574296763@163.com,侵权后果自负!!!";}private void button1_Click(object sender, EventArgs e){//获取PDF文件OpenFileDialog openFileDialog = new OpenFileDialog();if(openFileDialog.ShowDialog() == DialogResult.OK){//获取文件名string files = openFileDialog.FileName.ToLower();//获取文件扩展名string extension = System.IO.Path.GetExtension(files);if(extension != ".pdf"){MessageBox.Show("请选择PDF文件", "错误提示");}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实例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("请确定文件选择正确", "错误提示");}}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){this.Close();word转PDF word = new word转PDF();word.Close();}private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){word转PDF word = new word转PDF();//隐藏本窗体this.Hide();word.Show();}}
}

版权所有,使用请留言声明并在代码中添加引用说明!!!!!


http://www.mrgr.cn/news/60862.html

相关文章:

  • unity后端kbengine用DOTween让 移动同步丝滑
  • CI/CD 的原理
  • Python中的函数是什么?
  • Python报错:AttributeError: module ‘abc‘ has no attribute ‘ABCMeta‘
  • 先来先服务(FCFS,First-Come, First-Served)调度算法
  • CSP-J复赛集训200-300分(19): [2024 CSP-J 复赛] T3:小木棍
  • Java最全面试题->数据库/中间件->KafKa面试题
  • C#笔记2
  • Python中的列表是什么?它们有什么用途?
  • MATLAB的PCA人脸识别系统
  • Java | Leetcode Java题解之第518题零钱兑换II
  • Golang | Leetcode Golang题解之第517题超级洗衣机
  • TSO的资料
  • 音视频入门基础:AAC专题(12)——FFmpeg源码中,解码AudioSpecificConfig的实现
  • 音视频入门基础:FLV专题(18)——Audio Tag简介
  • 哈希及其封装实现unordermap和set
  • Redis 分布式锁:原理、实现及最佳实践
  • redis详细教程(3.ZSet,Bitmap,HyperLogLog)
  • Python量化交易(五):量化择时策略
  • YOLO框架最新综述从YOLOV1-YOLOV11(2024年10月23)