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

我正在使用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似乎是

                                          

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

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

我一直在寻找这些问题的解决方案,最后我不得不使用第三方。 我建议你使用像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"); 

您还需要在服务器上的全局程序集缓存中安装“office.dll”库才能使其正常工作。 看看C:\ Windows \ assembly文件夹,你会发现office.dll。

编辑:似乎这不会像它看起来那么简单。 一些开发人员在此提到Office必须安装在服务器上,否则您将需要手动安装更多库。