News:

To still receiving newsletters from us please subscribe to our Newsletters:
http://tech.groups.yahoo.com/group/developers-Heaven/

Main Menu

How to convert MS Word documents to PDF

Started by john, June 15, 2007, 04:58:53 PM

Previous topic - Next topic

john

Hi All.

Would you please help me. I'm writing an application for an Insurance company,the application create a word document and attaches it to an email and send the email to a client.

The company wants that word document to be programmatically converted to PDF and get save in the local folder. How do I convert MS Word document to PDF in C# or VB.Net.

admin

Add a ref to the Microsoft Word 12.0 Object Library & import into the project
(using Microsoft.Office.Interop.Word;)


ApplicationClass wordApp = new ApplicationClass();

Document wordDoc = null;

string paramExportFilePath = @"C:\\Test.xps";

WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;

bool paramOpenAfterExport = false;

WdExportOptimizeFor paramExportOptimizeFor =

WdExportOptimizeFor.wdExportOptimizeForPrint;

WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

bool paramIncludeDocProps = true;

bool paramKeepIRM = true;

WdExportCreateBookmarks paramCreateBookmarks
= WdExportCreateBookmarks.wdExportCreateWordBookmarks;

bool paramDocStructureTags = true;

bool paramBitmapMissingFonts = true;

bool paramUseISO19005_1 = false;

try

{

// Open the source document.

wordDoc = wordApp.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 (wordDoc != null)

wordDoc.ExportAsFixedFormat(paramExportFilePath,

paramExportFormat, paramOpenAfterExport,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, paramIncludeDocProps,

paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

paramBitmapMissingFonts, paramUseISO19005_1,

ref paramMissing);

}

catch (Exception ex)

{

// Respond to the error

}

finally

{

// Close and release the Document object.

if (wordDoc != null)

{

wordDoc.Close(ref paramMissing, ref paramMissing,

ref paramMissing);

wordDoc = null;

}

// Quit Word and release the ApplicationClass object.

if (wordApp != null)

{

wordApp.Quit(ref paramMissing, ref paramMissing,

ref paramMissing);

wordApp = null;

}

GC.Collect();

GC.WaitForPendingFinalizers();

GC.Collect();

GC.WaitForPendingFinalizers();