5

Is there any open-source sdk that I can use in an ASP.Net application to convert any office document to pdf. (I specifically need to convert a DOCX to PDF, but would like the ability to convert Excel and powerpoint files too).

I know that I could use Office automation using code shown below, but I don't want to use Office automation, as its not recommended for use in non-interactive applicationsKB257757

I have found that Aspose has a component that can be used for this (paid solution), but I was wondering if there were any open-source solutions out there.

//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();
    }

NOTE: I have seen some people recommend using OpenXML for this. But OpenXML does not provide you any method to convert an Office document to a PDF document.

4

3 回答 3

2

你可以在 apache 2.0 下使用 libreOffice 是免费许可证我做了一个将 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);
        }
    }

谢谢,希望对你有帮助。

于 2020-06-21T08:35:15.113 回答
1

看看JODConverter。这是免费和开源的,可以很好地完成 Doc->PDF,DocX 取决于您的文档。您可能想查看 ODFoverter 以获得一个很棒的 DocX->ODT 路径,然后 JODConverter 可以执行 ODT->PDF 部分。

于 2013-03-20T03:33:15.240 回答
0

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

于 2013-03-18T22:12:42.853 回答