Search code examples
c#asp.netc#-4.0ms-wordoffice-interop

how to programatically save a Word 2007 document as a PDF document in asp.net?


In my Asp.net web application, i have to convert one docx file into pdf file programatically.I have used microsoft interoperability word package and using the method saveAs() method.

This is my C# code...

var TheDocument = wdApp.Documents.Open("sample.docx"); //control stopped here...

TheDocument.ExportAsFixedFormat("sample.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF,
               OptimizeFor: Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen,
               BitmapMissingFonts: true, DocStructureTags: false);

((Microsoft.Office.Interop.Word._Document)TheDocument).Close();

But when executing this line, var TheDocument = wdApp.Documents.Open("sample.docx"); the control stopped on this line and there is no further response.Browser symbol seems like loading, loading...

I dont know what is the issue here...

Please guide me to get out of this issue...


Solution

  • Check this link -> convert doc to pdf in c#

    It uses the Microsoft.Office.Interop.

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