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

时间:2014-01-22 05:30:43

标签: 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 specified.

我试过的是添加引用并放入bin文件夹

Microsoft.Office.Interop.Word.dll
Microsoft.Office.Interop.Word.xml

如图

所示

enter image description here

我的.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);

我的WebConfig似乎是

<?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>

3 个答案:

答案 0 :(得分:2)

由于内存泄漏和性能问题,建议不要在服务器环境中使用Office互操作。您必须寻找一个托管解决方案,将PDF转换为服务器中的单词,而不是在那里安装Office软件包和互操作程序集。有一些托管组件可以完成工作,但它们不是免费的。

http://www.aspose.com/.net/pdf-component.aspx http://www.websupergoo.com/wordglue-1.htm

答案 1 :(得分:1)

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

  

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

     

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

     

Aspose.Words for .NET支持DOC,OOXML,RTF,HTML,OpenDocument,   PDF,XPS,EPUB和许多其他格式。

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

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

答案 2 :(得分:0)

您还需要在服务器上的Global Assembly Cache中安装“office.dll”库才能使其正常工作。查看C:\ Windows \ assembly文件夹,你会在那里找到office.dll。

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