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

C# word转pdf

程序员文章站 2022-06-02 21:19:00
...
写在最前面,如果要部署,服务器需要安装相同版本的office,否则会出现一大堆问题
https://www.cnblogs.com/5426z/articles/4865312.html

需要单独引入的是COM中的Microsoft Office 12.0 Object Library

public static bool WordToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Microsoft.Office.Interop.Word._Application application = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word._Document document = null;
            try
            {
                application.Visible = false;
                document = application.Documents.Open(sourcePath);
                document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document.Close();
                application.Quit();
            }
            return result;
        }


参考:
https://www.cnblogs.com/wangxlei/p/9431149.html