Word文件格式(.docx&安培; DOC)的转换使用C#为pdf格式不工作 [英] Convert of word file(.docx & doc) to .pdf using c# is not working
问题描述
我使用的VisualStudio 2010年我尝试转换word.docx文件.pdf文件,目前正在地方,但是,在服务器上它显示误差
运行时间精 无法加载文件或程序集办公,版本= 14.0.0.0,文化=中性公钥= 71e9bce111e9429c'或它的一个依赖。该系统找不到指定的文件。
我曾尝试加入参考,并放在bin文件夹
Microsoft.Office.Interop.Word.dll
Microsoft.Office.Interop.Word.xml
如图
我的.cs code是为
Microsoft.Office.Interop.Word.Application wordApp =新Microsoft.Office.Interop.Word.Application(); wordApp.Visible = FALSE; //从文件
目标文件名=使用Server.Mappath(word.docx); //输入 //文件
反对newFileName =使用Server.Mappath(PDF / document_to_read_tomorrow.pdf); //输出
反对失踪= System.Type.Missing; //打开的文档
Microsoft.Office.Interop.Word.Document DOC = wordApp.Documents.Open(REF文件名,REF失踪,失踪参考,参考失踪,失踪参考,参考失踪,
裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,
裁判失踪,失踪参考,参考失踪); // formt来保存文件,这种情况下,PDF
反对formatoArquivo = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; //纸张大小的变化 doc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4; //改变orietation纸
doc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait; //其他变化
doc.PageSetup.LeftMargin = 20;
doc.PageSetup.RightMargin = 0;
// 保存存档
doc.SaveAs(REF newFileName,裁判formatoArquivo,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,
裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪); doc.Close(参考失踪,失踪参考,参考失踪); wordApp.Quit(参考失踪,失踪参考,参考失踪);
我的WebConfig似乎
<?XML版本=1.0&GT?;
<结构>
<&是connectionStrings GT;
<添加名称=XYZ的connectionString =数据源= XYZ;初始目录= XYZ;坚持安全信息= TRUE;用户ID = XYZ;密码= XYZ的providerName =System.Data.SqlClient的/>
< /&是connectionStrings GT;
<&的System.Web GT;
<&HttpHandlers的GT;
<添加动词=GET路径=CaptchaImage.axdTYPE =MSCaptcha.CaptchaImageHandler,MSCaptcha/>
<添加路径=Reserved.ReportViewerWebControl.axd动词=*TYPE =Microsoft.Reporting.WebForms.HttpHandler,Microsoft.ReportViewer.WebForms,版本= 10.0.0.0,文化=中性公钥= b03f5f7f11d50a3a验证= 假/>
< / HttpHandlers的>
<编译调试=真targetFramework =4.0>
<&集会GT;
<添加组件=System.Data.Linq程序,版本= 4.0.0.0,文化=中性公钥= B77A5C561934E089/>
<添加组件=Microsoft.ReportViewer.WebForms,版本= 10.0.0.0,文化=中性公钥= B03F5F7F11D50A3A/>
<添加组件=Microsoft.ReportViewer.Common,版本= 10.0.0.0,文化=中性公钥= B03F5F7F11D50A3A/>
<添加组件=Microsoft.Build.Framework,版本= 4.0.0.0,文化=中性公钥= B03F5F7F11D50A3A/>
<添加组件=System.Management,版本= 4.0.0.0,文化=中性公钥= B03F5F7F11D50A3A/>
<添加组件=的Microsoft.Office.Interop.Word,版本= 14.0.0.0,文化=中性公钥= 71E9BCE111E9429C/>< /组件>
< buildProviders>
<添加扩展= TYPE =Microsoft.Reporting.RdlBuildProvider,Microsoft.ReportViewer.WebForms,版本= 10.0.0.0,文化=中性公钥= b03f5f7f11d50a3a/&GTRDLC。
< / buildProviders>
< /编译>
<的customErrors模式=关>
< /&的customErrors GT;
<的sessionState超时=720>
< /&的sessionState GT;
<页面enableViewStateMac =FALSE/>
< /system.web>
< system.webServer>
< defaultDocument>
<&文件GT;
<清/>
<增加价值=的Default.aspx/>
< /文件>
< / defaultDocument>
<验证validateIntegratedModeConfiguration =FALSE/>
<&处理GT;
<添加名称=ReportViewerWebControlHandlerpreCondition =integratedMode动词=*路径=Reserved.ReportViewerWebControl.axdTYPE =Microsoft.Reporting.WebForms.HttpHandler,Microsoft.ReportViewer.WebForms,版本= 10.0 .0.0,文化=中性公钥= b03f5f7f11d50a3a/>
< /处理器>
< /system.webServer>
< /结构>
我早就一直在寻找一个解决这些问题,并在最后,我不得不使用第三方。我建议你使用一个第三方喜欢阅读Aspose
Aspose.Total为.NET
Aspose.Words的.NET是.NET先进的类库,使您可以直接在你的.NET应用程序执行各种文档处理任务。
使用Aspose.Words您可以生成,修改,转换,渲染和打印
文档,而无需使用Microsoft Word。
Aspose.Words的.NET支持DOC,OOXML,RTF,HTML,OpenDocument格式,
PDF,XPS,EPUB等多种格式。
块引用>这对我来说分量的工作完美,使用起来非常简单。
这是转换词来PDF code的例子:文档的文档=新的文件(getMyDir()+Document.doc);
doc.save(getMyDir()+Document.Doc2PdfSave Out.pdf);I am using visualstudio 2010 and I am trying to convert word.docx file to .pdf file and is working fine in local but on run time on server it is showing error as
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.
What I have tried is added reference and put in bin folder
Microsoft.Office.Interop.Word.dll Microsoft.Office.Interop.Word.xml
as shown in figure
my .cs code is as
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);
My WebConfig seems as
<?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>
解决方案I had long been looking for a solution to these issues and in the end I had to use a third party. I suggest you use a third party like Aspose Aspose.Total for .NET
Aspose.Words for .NET is an advanced class library for .NET that enables you to perform a wide range of document processing tasks directly within your .NET applications.
With Aspose.Words you can generate, modify, convert, render and print documents without using Microsoft Word.
Aspose.Words for .NET supports DOC, OOXML, RTF, HTML, OpenDocument, PDF, XPS, EPUB and many other formats.
This component work perfectly for me and very simple to use. this is an example of convert word to pdf code :
Document doc = new Document(getMyDir() + "Document.doc"); doc.save(getMyDir() + "Document.Doc2PdfSave Out.pdf");
这篇关于Word文件格式(.docx&安培; DOC)的转换使用C#为pdf格式不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!