Author Topic: How to convert MS Word documents to PDF  (Read 7700 times)

Offline john

  • Newbie
  • *
  • Posts: 1
    • View Profile
How to convert MS Word documents to PDF
« on: June 15, 2007, 10:58:53 PM »
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.

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Re: How to convert MS Word documents to PDF
« Reply #1 on: June 15, 2007, 11:01:18 PM »
Add a ref to the Microsoft Word 12.0 Object Library & import into the project
(using Microsoft.Office.Interop.Word;)


Code: [Select]
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();

Offline SimonDavid32

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • SugarCRM
Re: How to convert MS Word documents to PDF
« Reply #2 on: April 14, 2011, 06:20:57 PM »
good point