当前位置: 代码网 > it编程>编程语言>Asp.net > C#实现word和pdf格式互转

C#实现word和pdf格式互转

2024年10月30日 Asp.net 我要评论
1、word转pdf使用nuget:microsoft.office.interop.wordwinform页面:后端代码://using spire.doc;//using spire.pdf;us

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互转内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com