C#实现word和pdf格式互转

 更新时间:2024年10月30日 09:40:51   作者:昔舍  
这篇文章主要为大家详细介绍了如何通过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+"\\";
            }
        }
 
        //保存为PDF
        private 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转word
            pDF.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();
        }
    }
}

到此这篇关于C#实现word和pdf格式互转的文章就介绍到这了,更多相关C# word和pdf互转内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Unity实现人物旋转和移动效果

    Unity实现人物旋转和移动效果

    这篇文章主要为大家详细介绍了Unity实现人物旋转和移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C# wpf使用ffmpeg命令行实现录屏的示例代码

    C# wpf使用ffmpeg命令行实现录屏的示例代码

    本文主要介绍了C# wpf使用ffmpeg命令行实现录屏的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • C#实现的调用DOS命令操作类实例

    C#实现的调用DOS命令操作类实例

    这篇文章主要介绍了C#实现的调用DOS命令操作类,实例分析了C#调用系统常用DOS命令的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C# 拓展方法的简单实例

    C# 拓展方法的简单实例

    这篇文章介绍了C# 拓展方法的简单实例,有需要的朋友可以参考一下
    2013-08-08
  • C# 实现Excel 打印与页面设置功能(从入门到精通)

    C# 实现Excel 打印与页面设置功能(从入门到精通)

    本文介绍了使用Spire.XLSfor.NET组件在C#中实现Excel文件的打印设置和静默打印功能,通过本文的示例代码,读者可以快速将这些功能集成到自己的C#项目中,提高办公效率和文档的专业度,感兴趣的朋友跟随小编一起看看吧
    2026-04-04
  • C# 使用Winform 获取下拉框 选中的值

    C# 使用Winform 获取下拉框 选中的值

    这篇文章主要介绍了C# 使用Winform 获取下拉框 选中的值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • 详解C#中的out和ref

    详解C#中的out和ref

    本文主要介绍了out和ref的相关知识。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • C#如何操作Excel数据透视表

    C#如何操作Excel数据透视表

    这篇文章主要为大家详细介绍了C#如何操作Excel数据透视表, 创建透视表、设置行折叠、展开等操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • 浅谈c# 浮点数计算

    浅谈c# 浮点数计算

    本文通过具体的示例给大家演示了下C#中浮点数运算所遇到的问题及解决方法,有需要的小伙伴可以参考下
    2017-09-09
  • C#中Hashtable和Dictionary的区别与用法示例

    C#中Hashtable和Dictionary的区别与用法示例

    由于 Hashtable 和 Dictionary 同时存在, 在使用场景上必然存在选择性, 并不任何时刻都能相互替代。所以这篇文章主要给大家介绍了关于C#中Hashtable和Dictionary区别的相关资料,需要的朋友可以参考下
    2021-05-05

最新评论