Convert to PDF with advanced options

GroupDocs.Conversion provides the PdfConvertOptions class, which enables fine-grained control over the conversion process when converting documents to PDF format. In addition to common conversion options, the PdfConvertOptions class offers several advanced features, including:

OptionDescription
setFormat()Specifies the desired output document format. Supported options include Pdf, Epub, Xps, Tex, Ps, and Pcl.
setPageWidth()Define the desired page width (in pixels) of the output document after conversion.
setPageHeight()Define the desired page height (in pixels) of the output document after conversion.
setDpi()Sets the desired resolution (dots per inch) of the resulting PDF.
setPassword()Protects the output PDF with a password.
setMarginTop()Define the desired page top margin after conversion.
setMarginBottom()Define the desired page bottom margin after conversion.
setMarginLeft()Define the desired page left margin after conversion.
setMarginRight()Define the desired page right margin after conversion.
setPdfOptions()Specifies PDF-specific convert options.
setRotate()Specifies page rotation. Supported options are None, On90, On180, and On270.

The following code demonstrates how to convert a document to PDF with advanced options:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.examples.Constants;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.options.convert.Rotation;

public class ConvertToPdfWithAdvancedOptions {
    public static void convert() {
        // Initialize the converter with the source document
        try(Converter converter = new Converter("annual-review.docx")) {
            // Configure advanced PDF conversion options
            PdfConvertOptions options = new PdfConvertOptions();
            options.setPageNumber(2);
            options.setPagesCount(1);
            options.setRotate(Rotation.On180);
            options.setDpi(300);
            options.setPageWidth(1024);
            options.setPageHeight(768);

            // Perform the conversion
            converter.convert("converted_with_options.pdf", options);
        }
    }

    public static void main(String[] args){
        convert();
    }
}

annual-review.docx is sample file used in this example. Click here to download it.

converted_with_options.png is converted PDF document. Click here to download it.

PDF-Specific Options:

The PdfOptions class provides additional settings for customizing PDF output, allowing conversion to different PDF versions and optimizing PDF formatting:

OptionDescription
setPdfFormat()Sets the target PDF format (e.g., PdfA_1A, PdfA_3B, v1_4, PdfX_1A).
setRemovePdfACompliance()Removes PDF/A compliance from the output document.
setZoom()Specifies the zoom level (percentage) for the output PDF.
setLinearize()Linearizes the PDF for faster web viewing.
setGrayscale()Converts the output PDF to grayscale.
setOptimizationOptions()Specifies PDF optimization options.
setFormattingOptions()Specifies PDF formatting options.

PdfOptimizationOptions

The PdfOptimizationOptions class allows to specify options for adjusting the PDF conversion process and improving its speed.

OptionDescription
setLinkDuplicateStreams()Eliminates link duplicate streams to reduce file size.
setRemoveUnusedObjects()Removes unused objects from the document to optimize size.
setCompressImages()Re-compresses all images in the document. The compression quality is defined by setImageQuality.
setImageQuality()Specifies image quality as a percentage (100% represents no quality loss).
setUnembedFonts()Makes fonts not embedded.

PdfFormattingOptions

The PdfFormattingOptions class provides different options to change the document look.

Window Appearance:
OptionDescription
setCenterWindow()Sets the target PDF format (e.g., PdfA_1A, PdfA_3B, v1_4, PdfX_1A).
setDirection()Sets reading order of text: L2R (left to right) or R2L (right to left). Available options are: L2R, R2L.
setDisplayDocTitle()Displays the document title in the window’s title bar.
setFitWindow()Resizes the document window to fit the first displayed page.
UI Customization:
OptionDescription
setHideMenuBar()Hides the menu bar when the document is active.
setHideToolBar()Hides the toolbar when the document is active.
setHideWindowUI()Hides UI elements when the document is active.
Page Layout:
OptionDescription
setNonFullScreenPageMode()Specifies how to display the document on exiting full-screen mode. Available options are: UseNone, UseOutlines, UseThumbs, FullScreen, UseOC, UseAttachments.
setPageLayout()Specifies the layout for displaying pages. Options include SinglePage, OneColumn, TwoColumnLeft, and others.
setPageMode()Configures how the document is displayed when opened.

By leveraging these advanced options, developers can precisely tailor the PDF conversion process to meet specific requirements, ensuring optimized and secure document output.