C# word格式转换为pdf

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

源码如下:

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

猜你喜欢

转载自www.cnblogs.com/mikeyu/p/11163932.html