【问题标题】:Is there any free library to covert doc to pdf without using Microsoft.Office.Interop.Word in c# environment是否有任何免费库可以将 doc 转换为 pdf 而无需在 c# 环境中使用 Microsoft.Office.Interop.Word
【发布时间】:2017-08-28 08:02:27
【问题描述】:

我遇到了一个关于如何在不使用 Microsoft.Office.Interop.Word 将 doc 转换为 pdf 的情况下使用 c# 的问题。我尝试了一些第三方解决方案,例如 spire.Doc,但它们不是免费的,而且我在 nuget 中找到了 DocX_Doc,但似乎没有关于此的教程。有没有人知道这个问题的免费解决方案或任何说明关于 DocX_Doc。非常感谢。

【问题讨论】:

  • 我看到的问题是要求一个库(这超出了 SO 的范围)。此外,我在 C# 中使用 pdf 有一些经验,我相信没有像样的免费库可以做到这一点(ofc “体面”取决于文档中的实际内容)。

标签: c# pdf itext


【解决方案1】:

您可以在 apache 2.0 https://www.libreoffice.org/ 下使用 libreOffice 是免费许可证 我已经对其进行了测试,它可以完美运行,您只需下载 soffice.exe 文件即可转换为 pdf,您也可以将 docx 转换为图像和其他类型。

这是我测试过的示例代码:

    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 @"C:\Program Files\LibreOffice\program\soffice.exe";
            default:
                throw new PlatformNotSupportedException("Your OS is not supported");
        }
    }

    static void Main(string[] args)
    {
        string libreOfficePath = getLibreOfficePath();

        ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, 
        string.Format("--convert-to pdf C:\\test.docx")); //test.docx => input path
        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);
        }
    }

希望对你有帮助。 谢谢。

【讨论】:

    【解决方案2】:

    我使用了 OfficeConvert,非常适合我...

    只需从 NuGet 下载。

    https://github.com/Sicos1977/OfficeConverter

    using (var converter = new Converter())
    {
        converter.Convert(<inputfile>, <outputfile>);
    }
    

    【讨论】:

      【解决方案3】:

      更新: 正如@saleem 上面提到的,你需要使用https://www.libreoffice.org/

      以下解决方案已过时,因为这些库不再免费:

      Convert DOC (DOCX) file to PDF file in C# - Step by Step “不免费”

      【讨论】:

      • 你引用的链接使用Interop:cathalscorner.blogspot.co.uk/2009/10/…
      • DocX 好像不能转PDF,只有付费版支持创建PDF文件...
      • DocX 从外观上看并不是免费的 PDF 转换
      • @AltF4 当时是免费的 :)
      • 可惜我错过了,你知道现在有什么免费的替代品吗?
      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2019-07-30
      • 2016-07-03
      • 2022-12-15
      • 1970-01-01
      相关资源
      最近更新 更多