欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#开发word批量转pdf源码分享

程序员文章站 2024-02-19 21:41:52
微软office word本身已经提供了另存为pdf文档功能,对于少量文档,手工使用该方式进行word转换为pdf尚可,一旦需要处理大量的文档,可能就显得有些捉襟见肘了。不...

微软office word本身已经提供了另存为pdf文档功能,对于少量文档,手工使用该方式进行word转换为pdf尚可,一旦需要处理大量的文档,可能就显得有些捉襟见肘了。不过对于已经安装有office环境,借助一些简单的代码即可实现批量word转pdf了。

源码:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.io;
using system.text;
using system.threading;
using microsoft.office.interop.word;

namespace word2pdf
{
    class program
    {
        public static microsoft.office.interop.word.document worddocument { get; set; }

        static void main(string[] args)
        {
            string strfolder_f = null;
            string strfolder_t = null;
            string strflag = null;
            system.console.writeline("请输入word文档所在目录");
            strfolder_f = system.console.readline();
            if (strfolder_f.substring(strfolder_f.length - 1, 1) != "\\")
            {
                strfolder_f += "\\";
            }
            strfolder_t = strfolder_f + @"pdf\";
            system.console.writeline("\n创建pdf文档,请确认!");
            system.console.write("y(yes) or n(no) ?  ");
            strflag = system.console.readline();
            if (strflag == "y")
            {
                system.console.writeline("\n开始创建pdf文档...");
                checkfolder(strfolder_t);
                string strpdffile = null;
                directoryinfo thefolder = new directoryinfo(strfolder_f);

                microsoft.office.interop.word.application appword = new microsoft.office.interop.word.application();
                object parammissing = type.missing;

                foreach (fileinfo nextfile in thefolder.getfiles())
                {
                    strpdffile = path.changeextension(strfolder_t + nextfile.name, ".pdf");
                    worddocument = appword.documents.open(nextfile.fullname);
                    if (worddocument != null)
                    {
                        worddocument.exportasfixedformat(strpdffile, wdexportformat.wdexportformatpdf);
                        worddocument.close(ref parammissing, ref parammissing, ref parammissing);
                        worddocument = null;
                    }
                    system.console.write(".. ");
                }

                if (appword != null)
                {
                    appword.quit(ref parammissing, ref parammissing, ref parammissing);
                    appword = null;
                }
            }

            //killprocessbyname("winword");
            gc.collect();
            gc.waitforpendingfinalizers();

            system.console.write("\n处理完毕,输入任意键退出");
            system.console.readkey();
        }

        static void checkfolder(string strfolderpath)
        {
            if (directory.exists(strfolderpath))
            {
                directory.delete(strfolderpath, true);
                directory.createdirectory(strfolderpath);
            }
            else
            {
                directory.createdirectory(strfolderpath);
            }
        }

        static void killprocessbyname(string name)
        {
            process[] ps = process.getprocessesbyname(name);
            foreach (process p in ps)
            {
                if (p.processname == name)
                    p.kill();
            }
        }
    }
}

需要注意的两个问题:①及时关闭代码中所打开的文档,见49行,否则会产生临时文件;②及时关闭“winword”线程,否则所处理的word文档会一直处于被该线程占用的情况。