C# 将word/ppt文档转换为Pdf的三种方法

      业务中,经常需要将office 文件上传并通过网页进行在线阅读,一种最普遍的方法就是转换office文件到pdf格式,以pdf文件方式进行在线预览。

       但将office 文件转换为Pdf的方法很多,各有利弊。

方法一:利用office自带的COM类型库组件实现转换Pdf功能。只要安装了office的服务器上都可以调用,不需要额外的第三方组件,功能也更加丰富和强大,几乎可以不受限制的操作office所有类型文件。缺点是部署问题多,发布到客户服务器进行调试的话问题很多。禁忌:1,开发的时候调用,不同office版本的COM组件,比如Microsoft.Office.Interop.Word是v14,那ppt、excel等组件都要统一版本,不然问题很多;2,部署的服务器上只能安装一个版本的office,比如开发时调用的Office 2010,那部署的服务器就只能装 office 2010,建议不要混装各种版本来匹配组件型号,最终会导致哪个都不能用;3,常见的故障问题和解决方法附录在该节末尾。

(1)利用Microsoft.Office.Interop.Word实现word转换pdf.

首先安装office 2010或其他更高版本。

添加引用Microsoft.Office.Interop. Word:

C#代码中添加引用:

using System.Text;

using Microsoft.Office.Interop.Word;

using WdExportFormat = Microsoft.Office.Interop.Word.WdExportFormat;

创建WordToPdf方法:

/// <summary>

        /// 把Word文件转换成pdf文件

        /// </summary>

        /// <param name="sourcePath">需要转换的文件路径和文件名称</param>

        /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>

        /// <returns>成功返回true,失败返回false</returns>

        public static bool WordToPdf(string sourcePath, string targetPath)

        {

           

            bool result = false;

            Microsoft.Office.Interop.Word.WdExportFormat wdExportFormatPDF = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式

            object missing = Type.Missing;

            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;

            Document document = null;

            try

            {

                applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();

                object inputfileName = sourcePath;//需要转格式的文件路径

                string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称

                WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式

                bool openAfterExport = false;//转换完成后是否打开

                WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。

                WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容(枚举)

                int from = 0;//起始页码

                int to = 0;//结束页码

                WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记

                bool includeDocProps = true;//指定是否包含新导出的文件在文档属性

                bool keepIRM = true;//

                WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。

                bool docStructureTags = true;

                bool bitmapMissingFonts = true;

                bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)

                document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                if (document != null)

                {

                    document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);

                }

                result = true;

            }

            catch

            {

                result = false;

            }

            finally

            {

                if (document != null)

                {

                    document.Close(ref missing, ref missing, ref missing);

                    document = null;

                }

                if (applicationClass != null)

                {

                    applicationClass.Quit(ref missing, ref missing, ref missing);

                    applicationClass = null;

                }

            }

            return result;

        }

调用该方法:

CommonCls.ConvertPdf.WordToPdf(sourcefilepath, targetfilepath);

(2)利用Microsoft.Office.Interop.PowerPoint实现ppt转换pdf.

首先安装office 2010或其他更高版本。

添加引用Microsoft.Office.Interop. PowerPoint:

(3)利用Microsoft.Office.Interop.*组件实现转换pdf的常见问题和解决方法。

   未加载错误

引用错误

调试正常,发布到部署服务器时无法转换

方法二:利用Aspose类库将word,ppt文档转换为Pdf。这个方法不需要在执行转换任务的服务器上安装office软件,也不会引起各种各样office权限的奇怪问题;缺点是需要收费,同时与office的后台耦合功能上还有所不足。

(1)利用Aspose. Words.dll将本地word文档转化成pdf

将Aspose. Words.dll  拷贝到bin目录。

添加引用

创建WordToPdf方法

public static bool WordToPdf(string sourcePath, string targetPath)

      {

          try

          {

              //读取doc文档

              Document doc = new Document(sourcePath);

              //保存为PDF文件,此处的SaveFormat支持很多种格式,如图片,epub,rtf 等等

              doc.Save(targetPath, SaveFormat.Pdf);

              return true;

          }

          catch

          {

              return false;

          }

      }

在项目中,将文件上传后进行调用:

CommonCls.ConvertToPDF.WordToPdf(sourcefilepath, targetfilepath);

(2)利用Aspose.Slides.dll将本地ppt文档转化成pdf

将Aspose.Slides.dll  拷贝到bin目录。

添加引用

 

C#代码中引用:

  • using System.IO;
  • using Aspose.Slides;

创建PptToPdf方法:

public static bool PptToPdf(string sourcePath, string targetPath)

      {

          try

          {

              Crack();//调用Crack方法实现Aspose.Slides软破解

              //实例化ppt文件

              Presentation pres = new Presentation(sourcePath);

              //保存

              pres.Save(targetPath, Aspose.Slides.Export.SaveFormat.Pdf);

              return true;

          }

          catch

          {

              return false;

          }

      }

代码中的Crack()方法,是用来对Aspose.Slides进行破解。

调用该方法:

CommonCls.ConvertToPDF. PptToPdf (sourcefilepath, targetfilepath);

方法三:利用Spire类库实现office文件转换pdf.

(1)利用Microsoft.Office.Interop.Word实现word转换pdf.

待续....

资源截图:

下载地址:https://download.csdn.net/download/lanhai96/86744033

 

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
C#中,你可以使用Microsoft.Office.Interop.PowerPoint库来实现PPT文档PDF文档。具体实现步骤如下: 1. 首先,你需要安装Microsoft PowerPoint软件,并且在Visual Studio中添加对Microsoft.Office.Interop.PowerPoint库的引用。 2. 在代码中创建一个PowerPoint.Application对象,并打开PPT文档。 ``` PowerPoint.Application pptApplication = new PowerPoint.Application(); PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open(@"C:\path\to\ppt\file.pptx"); ``` 3. 使用SaveAs方法PPT文档转换PDF文档。 ``` pptPresentation.SaveAs(@"C:\path\to\pdf\file.pdf", PowerPoint.PpSaveAsFileType.ppSaveAsPDF); ``` 4. 关闭PPT文档和PowerPoint.Application对象。 ``` pptPresentation.Close(); pptApplication.Quit(); ``` 完整的代码示例: ``` using System; using PowerPoint = Microsoft.Office.Interop.PowerPoint; namespace PPTtoPDF { class Program { static void Main(string[] args) { // 创建PowerPoint.Application对象 PowerPoint.Application pptApplication = new PowerPoint.Application(); // 打开PPT文档 PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open(@"C:\path\to\ppt\file.pptx"); // 将PPT文档转换PDF文档 pptPresentation.SaveAs(@"C:\path\to\pdf\file.pdf", PowerPoint.PpSaveAsFileType.ppSaveAsPDF); // 关闭PPT文档和PowerPoint.Application对象 pptPresentation.Close(); pptApplication.Quit(); } } } ``` 注意:在运行代码之前,请确保Microsoft PowerPoint已经安装在你的计算机上,并且在Visual Studio中添加了对Microsoft.Office.Interop.PowerPoint库的引用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lanhai96

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值