本文介绍了使用 c# 将 word 文件(.docx & doc)转换为 .pdf 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visualstudio 2010,我正在尝试将word.docx 文件转换为.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.

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

What I have tried is added reference and put in bin folder

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

如图所示

我的 .cs 代码如下

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);

我的 WebConfig 看起来像

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>

推荐答案

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

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 AsposeAspose.Total for .NET

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

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

With Aspose.Words you can generate, modify, convert, render and print documents without using Microsoft Word.

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

Aspose.Words for .NET supports DOC, OOXML, RTF, HTML, OpenDocument, PDF, XPS, EPUB and many other formats.

这个组件非常适合我并且使用起来非常简单.这是将 word 转换为 pdf 代码的示例:

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");

这篇关于使用 c# 将 word 文件(.docx &amp; doc)转换为 .pdf 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 18:36