Office文档为PDF

我已经看到了一些关于这个的线索,但在我的搜索中没有得到非常直接的答案。 我有一个Web应用程序需要接受doc,docx,xl​​s,xlsx文件并将它们转换为PDF。 现在我们有一个使用Microsoft.Office.Interop.Word库的进程,它打开文档,将其打印到PS文件,然后GPL GhostScript将PS文件转换为PDF。

这个过程很有效,但总体而言有几个步骤,而这最初是在几年前开发的,当时找到PDF打印驱动程序并对它进行接口更加困难。 本着更新的精神,我正在寻找一种可能更好的方法来处理这个问题。 主要原因是在我们的应用程序中,我们使用Web服务调用来执行转换过程的提升操作,使用较新的Windows服务器,特别是对于用于开发的Window 7,即使模拟也打开文件的能力导致一些问题使用Interop库。

所有这些我肯定可以理解并解决,但我想知道是否有更新更好的方法来解决这个问题。 我已经研究过PDF995,但是我找不到以编程方式进入并直接将文件打印到PDF的好方法。 他们提供的代码是用C ++编写的,我没有找到如何模仿C#中的调用。

如果你正在寻找一个“免费”的解决方案,我认为你可能有唯一可行的选择,但就像约翰所说,服务器端互操作通常不是一个好主意。 我们使用.NET Aspose组件取得了很大的成功。 这是一个纯粹的托管解决方案,无需互操作或办公。

编辑:根据John Saunders提供的文章, 服务器端自动化Office的注意事项,下面的代码不应该用于服务器端开发目的。

这是使用Interop将Docx转换为PDF的代码。 希望您可以使用此作为起点,弄清楚如何使用其他文档。

private void DocxToPdf(String sourcePath, String destPath) { //Change the path of the .docx file and filename to your file name. object paramSourceDocPath = sourcePath; object paramMissing = Type.Missing; var wordApplication = new Microsoft.Office.Interop.Word.Application(); Document wordDocument = null; //Change the path of the .pdf file and filename to your file name. string paramExportFilePath = destPath; WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF; bool paramOpenAfterExport = false; WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; WdExportRange paramExportRange = WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true; bool paramKeepIRM = true; WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; try { // Open the source document. wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); // Export it in the specified format. if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); } catch (Exception ex) { // Respond to the error System.Windows.Forms.MessageBox.Show(ex.Message); } finally { // Close and release the Document object. if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } // Quit Word and release the ApplicationClass object. if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } } 

Syncfusion Essential PDF可用于将office文档转换为PDF。 该库可以从Windows窗体,WPF,ASP.NET Webforms,ASP.NET MVC应用程序中使用

如果您符合条件,则可通过社区许可计划免费获得整套控件(商业应用程序)。 社区许可是完整的产品,没有任何限制或水印。

注意:我为Syncfusion工作。