Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need to convert a msword 2003 or 07 doc files into pdf using C#.

please help me
Posted
Updated 20-Mar-11 20:11pm
v2

1 solution

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


MS word2007 with (Primary Interoperability assembly will be installed by default).
 
Share this answer
 
Comments
Zia Ullah Khan 21-Mar-11 2:22am    
i need it to convert it in web page, and the word file is generated previously, just read the file from my harddisk and then convert it into pdf , in a web site . ..
kumarbl 11-Apr-12 6:28am    
Hi ramlinga koushik,
I have copy the same as your code, if i tried to run it showing the below error:


Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
kumarbl 11-Apr-12 6:29am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace SaveWordAsPDF
{
class Program
{
static void Main(string[] args)
{
object visible = true;
object readOnly = true;
object sourceFilename;
string targetFilename;
Application wordApp = new Application();
sourceFilename = "D:\\dartword\\MyDocument2.docx";
targetFilename = "D:\\dartword\\MyDocument.pdf";
Document doc = wordApp.Documents.Open(ref sourceFilename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref visible, ref missing, ref missing,
ref missing, ref missing);

doc.ExportAsFixedFormat(targetFilename, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForOnScreen,
WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, false, false,
WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, false, false, ref missing);

object saveChanges = false;
doc.Close(ref saveChanges, ref missing, ref missing);
wordApp.Quit(ref saveChanges, ref missing, ref missing);
}

static object missing = Type.Missing;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900