是否有任何免费的库可以在不使用 Microsoft.Office.Interop.Word 的情况下将文档转换为 pdf



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

。 非常感谢。

您可以使用libreOffice是Apache 2.0下的免费许可证 https://www.libreoffice.org/我已经测试过它,它工作得很好,只是你需要下载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 FilesLibreOfficeprogramsoffice.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);
}
}

希望对您有所帮助。 谢谢。

更新: 如上所述,@saleem您需要使用 https://www.libreoffice.org/

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

对于 DocX
  • 库 DocX,这里有一个关于如何将 word 转换为 PDF 的示例 将.docx转换为(.doc、.pdf、.html)"免费">

  • 您也可以检查此DLL,并在此处输入链接说明

  • 我使用SautinSoft.UseOffice来做到这一点,它简单易用,但成本约为350美元。以下是完整教程的链接:

在 C# 中将 DOC (DOCX) 文件转换为 PDF 文件 - 逐步"不免费">

相关内容

最新更新