Saving Documents as PDF in Aspose.Words for Java

Introduction to Saving Documents as PDF in Aspose.Words for Java

In this step-by-step guide, we’ll explore how to save documents as PDF using Aspose.Words for Java. We’ll cover various aspects of PDF conversion and provide code examples to make the process easier.

Prerequisites

Before we begin, ensure that you have the following prerequisites in place:

  • Java Development Kit (JDK) installed on your system.
  • Aspose.Words for Java library. You can download it from here.

Converting a Document to PDF

To convert a Word document to PDF, you can use the following code snippet:

Document doc = new Document("input.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions();
doc.save("output.pdf", saveOptions);

Replace "input.docx" with the path to your Word document and "output.pdf" with the desired output PDF file path.

Controlling PDF Save Options

You can control various PDF save options using the PdfSaveOptions class. For example, you can set the display title for the PDF document as follows:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setDisplayDocTitle(true);
doc.save("output.pdf", saveOptions);

Embedding Fonts in PDF

To embed fonts in the generated PDF, use the following code:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setEmbedFullFonts(true);
doc.save("output.pdf", saveOptions);

Customizing Document Properties

You can customize document properties in the generated PDF. For example:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setCustomPropertiesExport(PdfCustomPropertiesExport.STANDARD);
doc.save("output.pdf", saveOptions);

Exporting Document Structure

To export the document structure, set the exportDocumentStructure option to true:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setExportDocumentStructure(true);
doc.save("output.pdf", saveOptions);

Image Compression

You can control image compression using the following code:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setImageCompression(PdfImageCompression.JPEG);
doc.save("output.pdf", saveOptions);

Updating Last Printed Property

To update the “Last Printed” property in the PDF, use:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setUpdateLastPrintedProperty(true);
doc.save("output.pdf", saveOptions);

Rendering DML 3D Effects

For advanced rendering of DML 3D effects, set the rendering mode:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setDml3DEffectsRenderingMode(Dml3DEffectsRenderingMode.ADVANCED);
doc.save("output.pdf", saveOptions);

Interpolating Images

You can enable image interpolation to improve image quality:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setInterpolateImages(true);
doc.save("output.pdf", saveOptions);

Conclusion

Aspose.Words for Java provides comprehensive capabilities for converting Word documents to PDF format with flexibility and customization options. You can control various aspects of the PDF output, including fonts, document properties, image compression, and more.

FAQ’s

How do I convert a Word document to PDF using Aspose.Words for Java?

To convert a Word document to PDF, use the following code:

Document doc = new Document("input.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions();
doc.save("output.pdf", saveOptions);

Replace "input.docx" with the path to your Word document and "output.pdf" with the desired output PDF file path.

Can I embed fonts in the PDF generated by Aspose.Words for Java?

Yes, you can embed fonts in the PDF by setting the setEmbedFullFonts option to true in PdfSaveOptions. Here’s an example:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setEmbedFullFonts(true);
doc.save("output.pdf", saveOptions);

How can I customize document properties in the generated PDF?

You can customize document properties in the PDF using the setCustomPropertiesExport option in PdfSaveOptions. For example:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setCustomPropertiesExport(PdfCustomPropertiesExport.STANDARD);
doc.save("output.pdf", saveOptions);

What is the purpose of image compression in Aspose.Words for Java?

Image compression allows you to control the quality and size of images in the generated PDF. You can set the image compression mode using setImageCompression in PdfSaveOptions.

How do I update the “Last Printed” property in the PDF?

You can update the “Last Printed” property in the PDF by setting setUpdateLastPrintedProperty to true in PdfSaveOptions. This will reflect the last printed date in the PDF metadata.

How can I improve image quality when converting to PDF?

To improve image quality, enable image interpolation by setting setInterpolateImages to true in PdfSaveOptions. This will result in smoother and higher-quality images in the PDF.