简体   繁体   English

C#中将Word文档转换为PDF出错

[英]Error Converting Word Document to PDF in C#

I am trying to covert Microsoft Word and Excel documents to PDF in my application.我试图在我的应用程序中将 Microsoft Word 和 Excel 文档转换为 PDF。 I know there are tools that will make it easier, but we cannot purchase them so am trying to use Microsoft.Office.Interop.我知道有一些工具可以使它变得更容易,但我们无法购买它们,所以我尝试使用 Microsoft.Office.Interop。 I added through NuGet Microsoft.Office.Interop.Word 15.0.4795.1001 and Microsoft.Office.Interop.Excel 15.0.4795.1001.我通过 NuGet Microsoft.Office.Interop.Word 15.0.4795.1001 和 Microsoft.Office.Interop.Excel 15.0.4795.1001 添加。 My code is:我的代码是:

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

When I try to run it, I get: System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.当我尝试运行它时,我得到:System.IO.FileNotFoundException:无法加载文件或程序集“office,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c”。 The system cannot find the file specified.该系统找不到指定的文件。 File name: 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'文件名:'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

I have tried removing and re adding the NuGet packages, but it is still happening.我尝试删除并重新添加 NuGet 包,但它仍在发生。 Any ideas on how to resolve this?关于如何解决这个问题的任何想法?

Edit: I have Microsoft Office 365 installed on my machine.编辑:我的机器上安装了 Microsoft Office 365。

I ran into a similar issue as well.我也遇到了类似的问题。 Where I was using the wrong office version and installed the library office version.我在哪里使用了错误的办公版本并安装了库办公版本。 after installing the correct version which matches the library and MS office it worked for me.安装与图书馆和 MS Office 相匹配的正确版本后,它对我有用。

Your machine needs Office installed.您的机器需要安装 Office。 Version 15.0.0.0 of the library should correspond to Office 2013 - which needs to be installed on your machine other versions of Office will not work.该库的版本 15.0.0.0应对应于Office 2013 - 需要在您的计算机上安装它,其他版本的 Office 将无法使用。 Try installing the relevant office version and it should work fine.尝试安装相关的 office 版本,它应该可以正常工作。

You need to have Microsoft Word (office) installed .您需要安装Microsoft Word (office)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM