Tuesday, January 4, 2011

MS Word to PDF converstion in C sharp

Ms Word to PDF conversion in C sharp
• 2007 Microsoft Office Add-in can be used to add the save as pdf feature to a word document.
• I used Microsoft Word 12.0 Object Library to access the word document and programmatically converted in to PDF.
• I used the bellow code to convert a MS word document to pdf.
• Related links
1. http://msdn.microsoft.com/en-us/library/bb412305.aspx
2. http://blogs.msdn.com/b/pranab/archive/2008/09/24/convert-office-documents-docx-pptx-pub-into-pdf-programmatically.aspx?CommentPosted=true#commentmessage

For this code to work you must have to add a reference to Microsoft.Office.Interop.World.dll (in Visual Studio 2008, There will be two versions.)

1. Version 11.0
2. Version 12.0

I used version 11.0 here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace LK_IGen
class PdfConverter
private string ToSend;
public void wordToPdf(string sourceFile,string dFile)
this.ToSend = System.Configuration.ConfigurationSettings.AppSettings["SentPDF"].ToString();
String destFile=this.ToSend + dFile+".pdf";
ApplicationClass wordApplication = new ApplicationClass();

Document wordDocument = null;

object paramSourceDocPath = @sourceFile;

object paramMissing = Type.Missing;

string paramExportFilePath = destFile;
// string paramExportFilePath = @"E:\Test.pdf";

WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

bool paramOpenAfterExport = false;

WdExportOptimizeFor paramExportOptimizeFor =


WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

bool paramIncludeDocProps = true;

bool paramKeepIRM = true;

WdExportCreateBookmarks paramCreateBookmarks =


bool paramDocStructureTags = true;

bool paramBitmapMissingFonts = true;

bool paramUseISO19005_1 = false;


// Open the source document.

wordDocument = wordApplication.Documents.Open(

ref paramSourceDocPath, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing);

// Export it in the specified format.

if (wordDocument != null)


paramExportFormat, paramOpenAfterExport,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, paramIncludeDocProps,

paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

paramBitmapMissingFonts, paramUseISO19005_1,

ref paramMissing);


catch (Exception ex)

// Respond to the error



// Close and release the Document object.

if (wordDocument != null)

wordDocument.Close(ref paramMissing, ref paramMissing,

ref paramMissing);

wordDocument = null;


// Quit Word and release the ApplicationClass object.

if (wordApplication != null)

wordApplication.Quit(ref paramMissing, ref paramMissing,

ref paramMissing);

wordApplication = null;









1 comment:

Mark Henry said...

I want you to try this JPG to PDF . You don’t have to pay anything for JPG to PDF conversion and it works super smooth.