Convert Microsoft WORD TO PDF

Download Developer API Guide

WordToPdf Object Description

This object is used to convert ms-word document to pdf. This object contains many ms-word related features. WordToPdf only works with Word 2007 or higher version. Only Word 2007 requires the free "Save as PDF or XPS" add-in for Office 2007 to be installed. This add-in is available from Microsoft.

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");


Properties


PdfExportOptimizeFor Property

Specifies the resolution and quality of the PDF document for Screen or Print.

Default its set to PdfExportOptimizeFor.PDF_EXPORT_OPTIMIZE_FORPRINT for Print.

Type

PdfExportOptimizeFor

PDF_EXPORT_OPTIMIZE_FOR_ONSCREEN = 1 Export for screen, which is a lower quality and results in a smaller file size.
PDF_EXPORT_OPTIMIZE_FORPRINT = 0 Export for print, which is higher quailty and results in a larger file size.

Usage

Setting Resolution and Quality for print.

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
// Setting Resolution and Quality for print.
wordToPdf.setPdfExportOptimizeFor(PdfExportOptimizeFor.PDF_EXPORT_OPTIMIZE_FORPRINT);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

PdfExportRange Property

Specifies how much of the document to convert to pdf. Default set to PdfExportRange.PDF_EXPORT_ALL_DOCUMENT means converting the full document to pdf.

Type

PdfExportRange

PDF_EXPORT_ALL_DOCUMENT = 0 Convert the entire document to pdf.
PDF_EXPORT_SELECTION = 1 Convert the contents of the current selection to pdf.
PDF_EXPORT_CURRENT_PAGE = 2 Convert the current page to pdf.
PDF_EXPORT_FROM_TO = 3 Convert the contents of a range using the starting and ending positions to pdf.

Usage

Example 1

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
// Convert the entire document to pdf.
wordToPdf.setPdfExportRange(PdfExportRange.PDF_EXPORT_ALL_DOCUMENT);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

Example 2

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
// Convert the contents of a range using the starting and ending positions to pdf.
wordToPdf.setPdfExportRange(PdfExportRange. PDF_EXPORT_FROM_TO);
//Set the start page
wordToPdf.setStartPage(2);
//Set the end page
wordToPdf.setEndPage(6);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

DocumentMarkups Property

Specifies whether to export the pdf with markup. Default value is PDFMarkups.PDF_EXPORT_WITHOUT_MARKUP means document markups are omitted.

Type

PDFMarkups

PDF_EXPORT_WITHOUT_MARKUP = 0 Exports the pdf without markup.
PDF_EXPORT_WITH_MARKUP = 7 Exports the pdf with markup.

Usage

Example

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
//Exports the pdf with markup.
wordToPdf.setDocumentMarkups(PDFMarkups.PDF_EXPORT_WITH_MARKUP);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

IncludeDocProps Property

To include document properties in pdf file. Default is set to false. Set it to True to include document properties from the source document in the PDF.

Type

Boolean

Usage

Example

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
wordToPdf.setIncludeDocProps(true);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

CreateBookmarks Property

Specifies whether to export bookmarks and the type of bookmarks to export into pdf. Default value is set to PdfCreateBookmark.PDF_CREATE_NO_BOOKMARK means there will be no bookmarks in the PDF.

Type

PdfCreateBookmark

PdfCreateBookmark.PDF_CREATE_NO_BOOKMARK = 0 Do not create bookmarks in the exported pdf document.
PdfCreateBookmark.PDF_CREATE_HEADINGBOOKMARKS = 1 Create a bookmark in the exported pdf document for each Microsoft Office Word heading, which includes only headings within the main document and text boxes not within headers, footers, endnotes, footnotes, or comments.
PdfCreateBookmark .PDF_CREATE_WORDBOOKMARKS = 2 Create a bookmark in the exported pdf document for each Word bookmark, which includes all bookmarks except those contained within headers and footers.

Usage

Example

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
wordToPdf.setCreateBookmarks(PdfCreateBookmark.PDF_CREATE_WORDBOOKMARKS);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

DocStructureTags Property

This is used to include extra data into generated pdf to help screen readers, such as information about the flow and logical organization of the content ,The default is true.

Type

Boolean

Usage

Example

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
wordToPdf.setDocStructureTags(false);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

BitmapMissingFonts Property

This is used to include a bitmap of the text or reference the text font. The default is true means non-embeddable fonts will be converted to bitmaps. When this property is False, the font is referenced but not embedded in the PDF.

Type

Boolean

Usage

Example

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
wordToPdf.setBitmapMissingFonts(false);
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");

ConversionTimeOut Property

Set the file coversion time out property. Default is 5 min.

Type

Int (minute)

Usage

Example

// Create an Object
WordToPdf wordToPdf = new WordToPdf();
wordToPdf. setConversionTimeOut (10); //10 minute
// Covert to Pdf
wordToPdf.convert("C:\\Users\\Desktop\\input.docx","C:\\Users\\Desktop\\output.pdf");