C# word格式转换为pdf

时间:2022-11-11 20:57:09

引用 Microsoft.Office.Interop.Word 这个dll,可以在解决方案浏览器中搜索到并下载。

C# word格式转换为pdf

源码如下:

public bool WordToPDF(string sourcePath)
        {
            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);
                string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置
                if (!File.Exists(@PDFPath))//存在PDF,不需要继续转换
                {
                    document.ExportAsFixedFormat(PDFPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
                }
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document.Close();
            }
            return result;
        }

 

 

摘自https://www.cnblogs.com/alannxu/p/10972947.html