使用c#将word文件(.docx&;doc)转换为.pdf无效

使用c#将word文件(.docx&;doc)转换为.pdf无效,c#,asp.net,pdf,ms-word,C#,Asp.net,Pdf,Ms Word,我正在使用visualstudio 2010,我正在尝试将word.docx文件转换为.pdf文件,并且在本地工作正常,但在服务器上运行时,它显示错误为 Could not load file or assembly 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file spec

我正在使用visualstudio 2010,我正在尝试将word.docx文件转换为.pdf文件,并且在本地工作正常,但在服务器上运行时,它显示错误为

Could not load file or assembly 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.
我所尝试的是添加引用并放入bin文件夹

Microsoft.Office.Interop.Word.dll
Microsoft.Office.Interop.Word.xml
如图所示

我的.cs代码如下

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

 wordApp.Visible = false;

            // file from
 object filename =Server.MapPath("word.docx"); // input

    // file to
 object newFileName = Server.MapPath("pdf/document_to_read_tomorrow.pdf"); // output
 object missing = System.Type.Missing;

            // open document
 Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);

            // formt to save the file, this case PDF
 object formatoArquivo = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

            // changes in paper size

 doc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;

            // changes orietation paper
 doc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;

            // other changes
 doc.PageSetup.LeftMargin = 20;
 doc.PageSetup.RightMargin = 0;


            // save file
 doc.SaveAs(ref newFileName, ref formatoArquivo, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

 doc.Close(ref missing, ref missing, ref missing);

 wordApp.Quit(ref missing, ref missing, ref missing);
我的网络配置看起来像

<?xml version="1.0"?>
<configuration>
<connectionStrings>
    <add name="xyz" connectionString="Data Source=xyz;Initial Catalog=xyz;Persist Security Info=True;User ID=xyz;Password=xyz" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
    <httpHandlers>
        <add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>
        <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/>
    </httpHandlers>
    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/></assemblies>
        <buildProviders>
            <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </buildProviders>
    </compilation>
    <customErrors mode="Off">
    </customErrors>
    <sessionState timeout="720">
    </sessionState>
    <pages enableViewStateMac="false"/>
</system.web>
<system.webServer>
    <defaultDocument>
        <files>
            <clear/>
            <add value="default.aspx"/>
        </files>
    </defaultDocument>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
        <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </handlers>
</system.webServer>
</configuration>

您也需要在服务器上的全局程序集缓存中安装“office.dll”库才能执行此操作。检查C:\Windows\assembly文件夹,您将在那里找到office.dll

编辑:
看来这并不像看上去那么简单。一些开发人员提到,必须在服务器上安装Office,否则需要手动安装更多的库

由于内存泄漏和性能问题,不建议在服务器环境中使用Office interop。除了在服务器上安装office软件包和互操作程序集之外,您还需要寻找一种托管解决方案来将PDF转换为word。有一些托管组件可以完成工作,但它们不是免费的


我一直在寻找解决这些问题的方法,最后我不得不使用第三方。我建议你使用像Aspose这样的第三方

Aspose.Words for.NET是一个高级的.NET类库,使您能够直接在.NET应用程序中执行各种文档处理任务

使用Aspose.Words,您可以生成、修改、转换、渲染和打印文字 未使用Microsoft Word的文档

NET支持文档、OOXML、RTF、HTML、OpenDocument、, PDF、XPS、EPUB和许多其他格式

这个组件非常适合我,使用非常简单。 这是将word转换为pdf代码的示例:

Document doc = new Document(getMyDir() + "Document.doc");
doc.save(getMyDir() + "Document.Doc2PdfSave Out.pdf");

我在汇编文件夹中找到了office msil,如果它比在服务器上运行office应用程序简单。。。但事实并非如此,所以每个人都在自己尝试。@AlexeiLevenkov没有一个简单的方法吗?恐怕没有什么比在服务器上安装Office或找到一个可以满足您需要的替代库更容易的了。然而这两个都很难…我应该如何在服务器上安装office?我的应用程序不需要安全性,也没有问题是快速处理,我只想免费完成这项工作,对不起,但这是我的要求client@MiteshJain客户是否为每个用户购买了一个Office许可证?否则,就不能使用Office互操作,到此为止。