8

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

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

我确信所有这些都可以弄清楚并解决,但我想知道是否有更新更好的方法来解决这个问题。我研究了 PDF995,但没有找到一种以编程方式进入并将文件直接打印到 PDF 的好方法。他们提供的代码是 C++ 中的,我没有找到如何模仿 C# 中的调用。

4

3 回答 3

7

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

于 2011-10-05T17:24:18.770 回答
4

编辑:根据 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();
        }
    }
于 2011-10-05T17:23:52.620 回答
2

Syncfusion Essential PDF可用于将办公文档转换为 PDF。该库可用于 Windows 窗体、WPF、ASP.NET Webforms、ASP.NET MVC 应用程序

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

注意:我为 Syncfusion 工作。

于 2015-11-18T14:54:19.147 回答