convert doc to pdf in c#

13,383
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);
            }
        }

Prerequisites:

  • MS word2007 with (Primary Interoperability assembly will be installed by default).
  • plugin SaveAsPDFandXPS (free from MS Site)

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)

Share:
13,383
xorpower
Author by

xorpower

Reached 500 Repo on May 22'11 Reached 600 Repo on Jul 29'11 Reached 700 Repo on Aug 10'11 Reached 800 Repo on Sep 09'11 Reached 900 Repo on Oct 13'11 Reached (& crossed) 1000 Repo during Mar 14-19'12 Reached 1300 Repo on May 8 2013

Updated on June 04, 2022

Comments

  • xorpower
    xorpower almost 2 years

    How can i convert .doc to .pdf using asp.net c#. I cannot use any third party component.

    The code should be in

    1. C# or vb.net
    2. Compatible with VS 2005. (If not, then also please post your replies, i would then manually convert to VS 2005)

    Let me know if any query.

    Thanks!

  • Seth Battin
    Seth Battin about 7 years
    It is not clear what your answer adds that is different from the accepted answer. In general you should describe what you are posting, rather than solely entering the code, but even more so for an old question that already has an accepted solution.