没有使用 Office 自动化的开源解决方案将办公文档转换为 PDF

6

有没有开源的sdk可以在ASP.Net应用程序中使用,将任何办公文档转换为pdf(我特别需要将DOCX转换为PDF,但也希望能够转换Excel和PowerPoint文件)。

我知道我可以使用下面显示的代码使用Office自动化,但我不想使用Office自动化,因为它不建议在非交互式应用程序中使用KB257757

我发现Aspose有一个组件可用于此(付费解决方案),但我想知道是否有任何开源解决方案。

//reference: Microsoft.Office.Interop.Word.dll
//using Word = Microsoft.Office.Interop.Word;
public static void Convert(string documentFilePath, string outputPath)
    {
        var ap = new Word.Application {Visible = false};

        var document = ap.Documents.Open(documentFilePath);

        document.ExportAsFixedFormat(outputPath,
                       WdExportFormat.wdExportFormatPDF,
                       OptimizeFor: WdExportOptimizeFor.wdExportOptimizeForPrint,
                       BitmapMissingFonts: true, DocStructureTags: false);

        document.Close();
    }

注意:我看到有些人建议使用OpenXML来实现这个功能。但是OpenXML并没有提供将Office文档转换为PDF文档的方法。


Aspose还提供SaaSpose,根据您的需求免费或非常便宜。 - Dai
DAI - 我正在寻找一个开源解决方案。但感谢您提供的链接,我不知道Aspose有SAAS服务。 - Raj Rao
我已经编辑了你的标题。请参考“问题的标题应该包含“标签”吗?”,在那里达成共识是“不应该”。 - John Saunders
你有看过LibreOffice吗?显然,任何第三方工具转换质量都不会像Microsoft Office那样高,但是LibreOffice中用于docx格式的输入转换器已经相当不错了。 - Dirk Vollmar
3个回答

4
你可以使用免费授权的LibreOffice(Apache 2.0)进行操作,我举个例子来将docx转换为ppt,这个方法非常有效,并且你还可以将其转换成许多其他类型,例如pdf。
以下是我的示例:
    static string getLibreOfficePath()
    {
        switch (Environment.OSVersion.Platform)
        {
            case PlatformID.Unix:
                return "/usr/bin/soffice";
            case PlatformID.Win32NT:
                string binaryDirectory = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                //return binaryDirectory + "\\Windows\\program\\soffice.exe";
                return @"C:\Program Files\LibreOffice\program\soffice.exe";
            default:
                throw new PlatformNotSupportedException("Your OS is not supported");
        }
    }

    static void Main(string[] args)
    {
        string libreOfficePath = getLibreOfficePath();
        //to convert docx to pdf just change input file to docx
        ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, 
        string.Format("--convert-to pdf  C:\\test.ppt"));
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        procStartInfo.WorkingDirectory = Environment.CurrentDirectory;

        Process process = new Process() { StartInfo = procStartInfo, };
        process.Start();
        process.WaitForExit();

        // Check for failed exit code.
        if (process.ExitCode != 0)
        {
            throw new LibreOfficeFailedException(process.ExitCode);
        }
    }

感谢您,我希望这对您有所帮助。


0

看一下JODConverter。它是免费且开源的,能够很好地转换Doc->PDF,对于您的文档,转换DocX的效果也相当不错。您可能需要查看ODFCoverter以获得出色的DocX->ODT转换路径,然后JODConverter就可以完成ODT->PDF部分。


-1

虽然它只是一个库,但你可以将NPoiiTextSharp库结合起来完成你的任务。两者都是它们流行且强大的Java对应库的.Net端口。


虽然iTextSharp是一个开源解决方案,但它是在AGPL下许可的。在Raj的情况下,很可能需要购买iTextSharp的商业许可证。 - Bobrovsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接