1

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...

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Saravanan
  • 11,372
  • 43
  • 143
  • 213
  • 1
    Is PDF extension for office installed on the system? without this word will not save it. – Maxim Zabolotskikh Nov 23 '12 at 09:13
  • 1
    @MaximZabolotskikh : Yes, When i opening a file using WORD 2007 manually,there is a option called save As.So, that i can able to select pdf.And then the document saved as pdf file successfully – Saravanan Nov 23 '12 at 09:16
  • 1
    It is a horrible idea to use Office Interop from ASP.NET or another server technology. These APIs were written for use in a desktop application, for automating Office (a suite of desktop applications). Server applications are different in many ways that make it a very, very bad idea to use Office Interop in them. It's also unsupported by Microsoft, and may violate your Office license. See [Considerations for server-side Automation of Office](http://support.microsoft.com/kb/257757) – John Saunders Jan 06 '14 at 01:55

2 Answers2

1

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);
            }
        }
Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • 1
    Thanks... If My web application is not configured in IIS(5.1).So, i just run the application using vs-2010 deployment server,then it is run perfectly. Suppose if i configure my application in IIS(5.1) then it is not working...It keeps on loading... loading... no error ... no output... – Saravanan Nov 23 '12 at 09:45
  • 1
    Do i need to change any settings on IIS 5.1? – Saravanan Nov 23 '12 at 09:51
  • 1
    Check this: 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) – Carlos Landeras Nov 23 '12 at 10:44
  • 1
    :I have installed MS OFFICE-2007,reference given to Word.12 object library.I am using VS-2010 – Saravanan Nov 23 '12 at 10:50
  • -1 for providing false hope that this will work reliably from ASP.NET. – John Saunders Jan 06 '14 at 01:56
0
Object missing = Type.Missing;

Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(@"D:\MyPDF.pdf", 

Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, false, 

Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint, 

Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, 

Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentWithMarkup, true, true, 

Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, 

true, true, false,ref missing);
Serkan Hekimoglu
  • 4,234
  • 5
  • 40
  • 64
V K
  • 1,645
  • 3
  • 26
  • 57