我正在使用 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);
我的 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>
最佳答案
由于内存泄漏和性能问题,不建议在服务器环境中使用 Office 互操作。您将不得不寻找一个托管解决方案来在服务器中将 PDF 转换为 word,而不是在那里安装 office 包和互操作程序集。有一些托管组件可以完成工作,但它们不是免费的。
http://www.aspose.com/.net/pdf-component.aspx http://www.websupergoo.com/wordglue-1.htm
关于c# - 使用 c# 将 word 文件(.docx 和 doc)转换为 .pdf 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21274953/