none
Convert word doc to pdf

    Question

  • Hi,

     I need to convert word document to pdf file using C#.2

     Iam having

    1. MS visual studio 2005 professional (and also MS VS.2003 enterprise ed)
    2. MS Office 2007
    3. I have downloaded SaveAsPDFandXPS (used to convert doc to pdf)
    4. Microsoft Visual Studio 2005 Tools for Office Second Edition Runtime (VSTO 2005 SE)

    My question is

    Do i need "MS visual studio 2005 for office tools" version to work with ms word, so that i can convert word to pdf. 

     

    Wednesday, January 10, 2007 11:51 AM

Answers

  • hello everybody

    I myself find out answer for this

    Add reference to MS.Word

      private Microsoft.Office.Interop.Word.ApplicationClass MSdoc;       
           
            //Use for the parameter whose type are not known or say Missing
            object Unknown = Type.Missing;

      private void word2PDF(object Source, object Target)
            {
       //Creating the instance of Word Application          
           if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();
               
                try
                {  
                    MSdoc.Visible = false;               
                    MSdoc.Documents.Open(ref Source, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                    MSdoc.Application.Visible = false;
                    MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;               
                                                   
                    object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
                                   
                    MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                           ref Unknown, ref Unknown);

                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                finally
                {
                    if (MSdoc != null)
                    {
                        MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                        //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
                    }               
                    // for closing the application
                    WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown);
                }
            }

     

    Prerequisite:

    MS word2007 with (Primary Interoperability assembly  will be installed by default).

    plugin SaveAsPDFandXPS (free from MS Site)

     

    Karthik

    Friday, January 12, 2007 11:26 AM
  • Hi "My Ruf Account"

    There is no VSTO interface for VS 2003 that you can use with Office 2007. The dll discussed above is VS 2005, only. The most you could do with VS 2003 is to automate Word 2007 from, for example, a Windows Forms application.

    Sunday, January 21, 2007 6:43 AM

All replies

  • Hi Chennai

    No, VSTO is not required to work automate the Word application. I've never tried it, but I'm told the ExportAsFixedFormat method should convert a Word 2007 document to pdf.

    If by "list out the namespaces required to work with the document" you mean the Help for the object model: The best way to access this is to start Word, press Alt+F11 to go into its IDE, then use the Help menu. This will take you to an online source.

    You can also press F2 in the IDE to bring up its object browser.

    Wednesday, January 10, 2007 5:20 PM
  • hello everybody

    I myself find out answer for this

    Add reference to MS.Word

      private Microsoft.Office.Interop.Word.ApplicationClass MSdoc;       
           
            //Use for the parameter whose type are not known or say Missing
            object Unknown = Type.Missing;

      private void word2PDF(object Source, object Target)
            {
       //Creating the instance of Word Application          
           if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();
               
                try
                {  
                    MSdoc.Visible = false;               
                    MSdoc.Documents.Open(ref Source, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                    MSdoc.Application.Visible = false;
                    MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;               
                                                   
                    object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
                                   
                    MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                           ref Unknown, ref Unknown);

                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                finally
                {
                    if (MSdoc != null)
                    {
                        MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                        //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
                    }               
                    // for closing the application
                    WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown);
                }
            }

     

    Prerequisite:

    MS word2007 with (Primary Interoperability assembly  will be installed by default).

    plugin SaveAsPDFandXPS (free from MS Site)

     

    Karthik

    Friday, January 12, 2007 11:26 AM
  • hello

    i like what you written it much much close to my style.

    but where can i find Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

    it's not there how can i add it.

    kindly can you describe it very clearly

     

    Yours

    A/\/\RJX

    Sunday, January 14, 2007 11:51 AM
  •  

    Make sure you have reference to Word.12.

    It will automatically add Microsoft.Office.interop.word to your reference. Follow these  for other office application.

    (Note: you should have installed VS 2005 Tools for Office 2nd Ed. Runtime (VSTO 2005 SE) (x86)

    For PDF conversion Plugin- SaveAsPDFandXPS)

    PDF conversion only works with Lastest MS Office 2007 only

     

    Wednesday, January 17, 2007 7:47 AM
  • Hello Friends,

    Have you some stable solution to convert Word to Pdf.
    You r talking about VS2005 has one dll for that or class for that.

    But what for VS2003.
    Have you some solution for that, Have you got dll file for that.

    Also please can you find out that the dll used in VS2005 (which you use) is also usable for VS2003.
    and working Ok!!.

    If you got some other dll or any solution please reply.


    Saturday, January 20, 2007 7:36 AM
  • Hi "My Ruf Account"

    There is no VSTO interface for VS 2003 that you can use with Office 2007. The dll discussed above is VS 2005, only. The most you could do with VS 2003 is to automate Word 2007 from, for example, a Windows Forms application.

    Sunday, January 21, 2007 6:43 AM
  • Hello Friends,

    First of all Thanks for Reply.
    Now the issue is,
    I want to this for my web project...
    here We uses VS2003.
    So Is there any alternative Solution for that?....

    Please If You have some idea then Reply....
    Monday, January 22, 2007 8:31 AM
  • If you're targeting Office 2003, then make sure the documents are saved in Word 2003 XML file format. You can then transform the result to the PDF XML format. There are already third-party solutions available; searching the Internet should turn them up.

    This approach is really the only viable one for a server- or web-interface as it doesn't involve running the Word application.

    Monday, January 22, 2007 7:13 PM
  • Hi Friends,

     

    I am also coverting word document into pdf and I am having Office 2007 and MS visual studio 2005

    I did the same as Karthik have mensioned above. But it gives me a exception below.

     

    System.Runtime.InteropServices.COMException (0x800A1066): Command failed

       at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Objec

    t& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles,

     Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts,

     Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELette

    r, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Objec

    t& LineEnding, Object& AddBiDiMarks)

       at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settin

    gs\neeleshk\Desktop\test11\ConsoleApplication2\ConsoleApplication2\Program.cs:li

    ne 3

     

    Saturday, October 13, 2007 12:44 PM
  • The PDF file converter is not installed as part of Office, due to licensing issues raised by Adobe. It must be downloaded and installed separately. Are you sure the file converter is correctly installed on the machine where this error is being raised?

     

    Saturday, October 13, 2007 5:59 PM
  • Hi Cindy,

     

    I am not using any kind of PDF file convertor rather i am trying to do this with C# code. and that code convert .doc to .rtf & .html without any error. and when i am trying it for PDF then it gives me exception like

    "The remote procedure call failed and did not execute. (Exception from HRESULT: 0x800706BF)"

     

    My steps are

    -          First I have converted  .doc to .rtf (converted successfully).

    -          Then I have tried .rtf to .pdf (gives exception as messioned above) and I have also tried to convert .doc to .pdf then it give me exceptions.

     

    Thanks,

    Neelesh

     

    Monday, October 15, 2007 8:45 AM
  • Hi Neelesh

     

    When Word saves a document to any file format other than its native *.doc format, it uses a file converter. This is true for RTF, for HTML, or for saving as plain text (just as examples). The difference between these file formats and PDF is that their file converters are installed by default when Office is installed. The PDF file converter is not part of the Office package. Originally, it was supposed to be, but Adobe threatened a lawsuit.

     

    Therefore, the PDF file converter must be downloaded and installed separately before you can use it. It doesn't matter whether you're automating Office or working as an end-user.

     

    • Proposed as answer by Robert Taylor Friday, March 06, 2009 5:34 PM
    Monday, October 15, 2007 2:48 PM
  •  

    I installed office 2003 on server and client, and use Microsoft.Office.Interop.Word to handle word by C#.But I can't start word on client.I'm not sure what's wrong with my codes? They just like:

    m_appWord = new Microsoft.Office.Interop.Word.Application();

    m_appWord.Visible = true;

    m_appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone;

    m_bStartupApp = true;

    bResult = true;

     

    and so on...

    Please give me your suggestions.

    Thanks a lot

    Thursday, December 27, 2007 3:50 AM
  • Could you give some insight on how to call this?

    i.e.  Should this work?  word2PDF("filename.doc", "filename.pdf");

    Thank you.
    Thursday, February 14, 2008 9:59 PM
  • Thanks for the help so far but I am getting an error when I call the line of code to save the file as pdf.
    The error says that the application is busy. Any thoughts?

    {"The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"}
    Friday, March 07, 2008 9:52 PM
  • Hi Guys!

    I advice you to try the PDF Metamorphosis .Net, it able to convert RTF to PDF:
    http://www.sautinsoft.com/components/pdf_metamorphosis_net.zip

    So, at first you may convert DOC to RTF by MS Office and next RTF to PDF by Metamorphosis.

    This is a sample code on VB.Net:

    Dim p As New SautinSoft.PdfMetamorphosis
    p.RtfToPdfConvertFile("D:\Example.rtf", "D:\File.pdf")


    C# sample:

    SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();
    p.RtfToPdfConvertFile("D:\Example.rtf", "D:\File.pdf");


    Best wishes,
    Max

    • Proposed as answer by LaxmiRanBanned Wednesday, October 26, 2011 10:38 AM
    Wednesday, March 19, 2008 12:12 PM
  • Not sure if this helps anyone but I recently had to convert Word 2003 docs to pdf. You basically print the document out as a postscript file, then take that file and run it through Adobe Acrobat Distiller.

     

    added a reference to COM lib "ACRODISTXLib" which is the Adobe Acrobat Distiller.

     

    private Word.Document _myWordDoc;

    private Word.Application _myWordApp;

    private string filename_template = "C:\\doc1.doc";

    private object filename_obj = (object)filename_template;

     

    private object missing = System.Reflection.Missing.Value; // our 'void' value

    private object printrange_obj = Word.WdPrintOutPages.wdPrintAllPages;

    private object true_obj = true;

     

    ...

     

    _myWordDoc= _myWordApp.Documents.Open( // load the template into a document workspace

    ref filename_obj, // and reference it through our myWordDoc

    ref missing,

    ref true_obj, // ReadOnly

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

     

    string destination_ps = "C:\\doc1.ps";

    object destination_ps_obj = destination_ps;

    string destination_doc = "C:\\doc1.pdf";

     

    //save as pdf

    _myWordDoc.PrintOut(ref false_obj, ref printrange_obj, ref missing, ref destination_ps_obj,

    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref true_obj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

     

    PdfDistillerClass distiller = new PdfDistillerClass();

    distiller.FileToPDF(destination_ps, destination_pdf, "");

    Tuesday, August 12, 2008 7:27 PM
  • Hi all,
    great post.

    I write my console application and it work fine on Xp and Vista.


    I also try to include this code in  a windows services application but it work fine in Xp and NOT in Vista.

     

    I try to go in debug mode and to analize object after "open" method, first to define assign document format:


     

    Code Snippet

     

    object Unknown = Type.Missing;

     

    Word.Document ObjDoc = newApp.Documents.Open(ref _Sources, ref Unknown,

    ref Unknown, ref Unknown, ref Unknown,

    ref Unknown, ref Unknown, ref Unknown,

    ref Unknown, ref Unknown, ref Unknown,

    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

     

    // Specifying the format in which you want the output file

    object format = Word.WdSaveFormat.wdFormatPDF;

     

     

    It seam me:

    Code Snippet
    "Il comando non è disponibile perché non c'è alcun documentoaperto" System.Runtime.InteropServices.ExternalException {System.Runtime.InteropServices.COMException}


    "Command not avaible because there isn't open document" System.Runtime.InteropServices.ExternalException {System.Runtime.InteropServices.COMException}

     

    ErrorCode -2146824040 


    There is someone that say me because on Vista it work as console application and not as windows services?

    thank's

    Wednesday, September 17, 2008 2:21 PM
  • You need to install the word converter. If you load Word, click 'SaveAs' and see an option to save  as XPS then you're ok. For Word 2007 go to this link...

     http://www.microsoft.com/downloads/thankyou.aspx?familyId=4d951911-3e7e-4ae6-b059-a2e79ed87041&displayLang=en
    Robert
    Friday, March 06, 2009 5:36 PM
  • Thanks Robert, your solution helped me.
    Saturday, April 04, 2009 6:19 PM
  • i appeared to me message say value out of range?

    what i do in this  message?

    • Proposed as answer by Kareem Koki Tuesday, July 14, 2009 2:41 PM
    Tuesday, July 14, 2009 2:37 PM
  • Hi "My Ruf Account" ?

    I get message for this code and say value Out of range ?

    What do i do in this message ?

    replay to me  fast ?

    Tuesday, July 14, 2009 2:46 PM
  • Segue link para gerar PDF utilizando C# e VSTO,

    http://rodrigo-romano.spaces.live.com/blog/cns!9E20A90BA8FC57D3!280.entry









    Rodrigo Alves Romano
    New Cocnept - Soluções Inovadoras
    http://www.nconcept.net
    Blog Técnico: http://rodrigo-romano.spaces.live.com
    Wednesday, October 07, 2009 6:21 PM
  • It just work fine for me. Thanks a lot for this article.
    Wednesday, January 20, 2010 7:14 PM
  • Error occurred while exporting to PDF as  "The export failed because this feature is not installed. "


    Wednesday, January 27, 2010 11:31 AM
  • Is it mandatory Microsoft word to be installed on the system to convert Word Doc to PDF?

    Thursday, February 18, 2010 3:00 PM
  • mrleokarthik , thank you very much for your code. It works perfectly! Much appreciated.
    Best regards, Sanasy
    Monday, March 01, 2010 3:51 PM
  • mrleokarthik , thank you very much for your code. It works perfectly! Much appreciated.
    Best regards, Sanasy

    i want open source solution to convert word(2003 r 2007) to pdf in any environment.. please help me..... 
    Tuesday, August 31, 2010 5:53 AM
  • i want open source solution to convert word(2003 r 2007) to pdf in any environment.. please help me..... in .net 2008
    Tuesday, August 31, 2010 5:54 AM
  • Thanks! Worked fist try.

    Old post, but I am happy to report that this approach worked first try using:

    ·          VS2010

    ·          Microsoft.Office.Interop.Word 12

    ·          Office 2010 Professional

    No additional plugin required with current versions.

    Eric

     

    Thursday, January 20, 2011 12:03 PM
  • Hi "My Ruf Account"

    There is no VSTO interface for VS 2003 that you can use with Office 2007. The dll discussed above is VS 2005, only. The most you could do with VS 2003 is to automate Word 2007 from, for example, a Windows Forms application.

    It really make sense.
    Never too old to learn
    Wednesday, February 16, 2011 3:06 AM
  • this what i was looking for. I have following questions if someone can answer. I'm using VS 2010, Office 2010 PIA

    1>Does 2010 Office PIA has "Microsoft.Office.Interop.Word 12" or is it part of 2007 PIA?

    2>Do i also need to install SaveAsPDFandXPS or is it already included in 2010 Office PIA?

    3>Is VS 2010, 2010 Office PIA Sufficient or do i need to install Visual Studio 2010 Tools for Office Runtime?

    4>Do i need to install actual copy of office on server or PIA is fine?

    5>Can i run this code on web server? the conversion should happen on Web Server.

    Tuesday, May 10, 2011 12:59 AM
  • Hi lax4u (all)

    I think this question is being handled in other, parallel discussions...


    Cindy Meister, VSTO/Word MVP
    Tuesday, May 10, 2011 4:29 PM
  • Have a try to use Spire.Doc,

    using System;
    using Spire.Doc;
    using Spire.Doc.Documents;
    namespace WordToPDF
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Create a new document
    Document document = new Document();
    //Load word 2007 file from disk.
    document.LoadFromFile("WordFile.docx");
    //Save doc file to pdf.
    document.SaveToFile("Sample.pdf", FileFormat.PDF);
    }
    }
    }
    • Proposed as answer by LaxmiRanBanned Wednesday, October 26, 2011 10:37 AM
    Tuesday, September 20, 2011 2:23 PM
  • Hi,

    I am using .net2005 and I am converting Word documents PDF using c#.net by using MS Word dll. My problem is after convertion when i check the PDF file all he fonts are embedded, but i don't want to embedded thr fonts. so how can i set the parameter for this while converting.

    Please do me needful.

     

    Regards,

    Sarath.V

    Thursday, February 02, 2012 6:12 PM
  • Hi,

    The UseOffice .Net library enables to any .Net application to convert word documents to pdf in C#:

    SautinSoft.UseOffice u = new SautinSoft.UseOffice();
    string wordPath = "c:\Banana.docx";
    string pdfPath = "c:\Banana.pdf";
    
    int ret = u.InitWord();			
    if (ret==1)
      return;
    			
    u.ConvertFile(inputFilePath, outputFilePath, SautinSoft.UseOffice.eDirection.DOC_to_PDF);
    u.CloseWord();
    Cheers,
    Max

    • Edited by Max Sautin Thursday, May 31, 2012 7:30 AM
    Thursday, May 31, 2012 7:30 AM