3

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!

xorpower
  • 17,975
  • 51
  • 129
  • 180
  • Please don't post simple requests for someone to write your code for you. If you have a specific question, please post a question about that. – Adam Robinson Feb 05 '11 at 18:43
  • 1
    @Adam: i will keep this in mind – xorpower Feb 05 '11 at 18:45
  • 1
    possible duplicate of [How do I convert Word files to PDF programmatically?](http://stackoverflow.com/questions/607669/how-do-i-convert-word-files-to-pdf-programmatically) – Mauricio Scheffer Feb 05 '11 at 18:49
  • @Scheffer: your link worked! but the code snippet given there is useful for .doc only. Any idea to make it work it for .docx? – xorpower Feb 05 '11 at 19:50

4 Answers4

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

Bridge
  • 29,818
  • 9
  • 60
  • 82
ayush
  • 14,350
  • 11
  • 53
  • 100
0
//Add Office Library

using Word = Microsoft.Office.Interop.Word;

object str_letter_path = @"D:\DOCTEST.doc";
object outputFilePathPDF = @"D:\PDFTEST.PDF";

Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
wordApp.ScreenUpdating = false;

object oMissing = System.Reflection.Missing.Value;
object fileFormat = Word.WdSaveFormat.wdFormatPDF;

Word.Document doc = wordApp.Documents.Open(ref str_letter_path, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            doc.Activate();

            doc.SaveAs(ref outputFilePathPDF,
                            ref fileFormat, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
            if (doc != null)
                ((Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
            ((Microsoft.Office.Interop.Word._Application)wordApp).Quit(ref saveChanges, ref oMissing, ref oMissing);
  • 1
    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. – Seth Battin Mar 27 '17 at 14:49
0

You can use Microsoft.Office.Interop.Word.dll to convert Word file to PDF.

Install the package first and add a reference to it.

using Microsoft.Office.Interop.Word;

Then use the following code to Convert word document to PDF.

Application app = new Application();
Document doc = app.Documents.Open(@"D:/test.docx");
doc.SaveAs2(@"D:/test.pdf", WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit();
Console.WriteLine("Completed");
Ashin
  • 386
  • 4
  • 8
0

You can use my code,it works without any exception and doesn't keep opened COM objects on the background processes.

Application app = new Application();
                app.Visible = false;
                app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                Documents documents = app.Documents;
                Document doc = documents.Open(fileLocation);
                newPath = Path.GetDirectoryName(fileLocation);
                newPath = newPath.Replace(newPath, outLocation);
                if (!File.Exists(newPath))
                {
                    doc.SaveAs2(newPath, WdSaveFormat.wdFormatPDF);
                }

                Marshal.ReleaseComObject(documents);
                doc.Close();
                Marshal.ReleaseComObject(doc);
                app.Quit();
                Marshal.ReleaseComObject(app);