【问题标题】:Error Converting Word Document to PDF in C#在 C# 中将 Word 文档转换为 PDF 时出错
【发布时间】:2022-11-30 23:26:08
【问题描述】:

我试图在我的应用程序中将 Microsoft Word 和 Excel 文档转换为 PDF。我知道有一些工具可以使它变得更容易,但我们无法购买它们,所以我尝试使用 Microsoft.Office.Interop。我通过 NuGet Microsoft.Office.Interop.Word 15.0.4795.1001 和 Microsoft.Office.Interop.Excel 15.0.4795.1001 添加。我的代码是:

    using Microsoft.Office.Interop.Word;

    [HttpPost("getsupportingfilepdf")]
    [ProducesResponseType(typeof(SupportFileResponse), StatusCodes.Status200OK)]
    public ActionResult GetSupportingFilePDF()
    {
            Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
            if (appWord.Documents != null)
            {
                //yourDoc is your word document
                Microsoft.Office.Interop.Word.Document wordDocument = appWord.Documents.Open(@"C:\workspace\DatabaseSRS.docx");
                string pdfDocName = @"C:\workspace\DatabaseSRS.pdf";
                if (wordDocument != null)
                {
                    wordDocument.ExportAsFixedFormat(pdfDocName, WdExportFormat.wdExportFormatPDF);
                    wordDocument.Close();
                }
                appWord.Quit();
            }

当我尝试运行它时,我得到: System.IO.FileNotFoundException:无法加载文件或程序集“office,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c”。该系统找不到指定的文件。 文件名:'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

我尝试删除并重新添加 NuGet 包,但它仍在发生。关于如何解决这个问题的任何想法?

编辑:我的机器上安装了 Microsoft Office 365。

【问题讨论】:

标签: c#


【解决方案1】:

我也遇到了类似的问题。我在哪里使用了错误的办公版本并安装了库办公版本。安装与图书馆和 MS Office 相匹配的正确版本后,它对我有用。

您的机器需要安装 Office。版本 15.0.0.0库的应该对应于办公室 2013- 这需要在您的机器上安装其他版本的 Office 将无法使用。尝试安装相关的 office 版本,它应该可以正常工作。

【讨论】:

    【解决方案2】:

    您需要有 Microsoft Word (office) installed。

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2011-11-10
      • 2013-09-25
      • 2010-09-14
      • 2011-03-02
      相关资源
      最近更新 更多