Convert of word file(.docx & doc) to .pdf using c# is not working

13,915

Solution 1

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

Solution 2

It is not recommended to use the Office interop in a server environment due to the memory leaks and perfomance issues. You will have to look for a managed solution to convert PDF to word in a server, than installing the office package and the interop assemblies there. There are a few managed components which can get the work done, but they are not free.

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

Share:
13,915
Mitesh Jain
Author by

Mitesh Jain

Updated on June 27, 2022

Comments

  • Mitesh Jain
    Mitesh Jain almost 2 years

    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

    enter image description here

    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>
    
  • Mitesh Jain
    Mitesh Jain over 10 years
    i have found office msil in assembly folder
  • Alexei Levenkov
    Alexei Levenkov over 10 years
    If it is simple than running Office apps would be supported on servers... But it is not, so everyone trying it are on they own.
  • Mitesh Jain
    Mitesh Jain over 10 years
    @AlexeiLevenkov is there not an any easy way
  • Martin Zikmund
    Martin Zikmund over 10 years
    I'm afraid there is nothing easier than installing Office on the server or finding a alternative library that can achieve what you need. Both are however quite hard...
  • Mitesh Jain
    Mitesh Jain over 10 years
    my application has no need for security and has no issue to have been fast process, i just want the work to be done for free, sorry but its the requirement of my client
  • Mitesh Jain
    Mitesh Jain over 10 years
    How shall I install office on server?
  • Alexei Levenkov
    Alexei Levenkov over 10 years
    You shall not... But steps are easy - buy license, install, hope it would work for your case. Obviously there is no guarantees that it will work at all under your Web server's account or will not require effort to get at least something out of it... But since your work is free I see no problem you spending a lot of time on it.
  • Panagiotis Kanavos
    Panagiotis Kanavos over 4 years
    @MiteshJain did the client buy one Office license for every user? If not, you can't use Office interop, end of story.