How to Set PDF Viewer Preference in Java

·

3 min read

PDF viewer preference allows users to view PDF documents with their viewing habits. They can set the page layout of the PDF document and choose whether to display it in full screen, whether to hide the menu bar/toolbar, etc. This article will introduce how to set the viewer preference in a PDF document by using Free Spire.PDF for Java.

Installation

Method 1: Download the Free Spire.PDF for Java and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>2.6.3</version>
    </dependency>
</dependencies>

Set Viewer Preference:

import com.spire.pdf.*;

public class ViewerPreference {
    public static void main(String[] args) {

        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Walt Whitman.pdf");

        //Center the window
        pdf.getViewerPreferences().setCenterWindow(true);
        //Hide title
        pdf.getViewerPreferences().setDisplayTitle(false);
        //Fit the window size
        pdf.getViewerPreferences().setFitWindow(true);
        //Hide menu bar
        pdf.getViewerPreferences().setHideMenubar(true);
        //Hide tool bar
        pdf.getViewerPreferences().setHideToolbar(true);
        //Set two-column page layout
        pdf.getViewerPreferences().setPageLayout(PdfPageLayout.Two_Column_Left);
        //Set full screen display
        //pdf.getViewerPreferences().setPageMode(PdfPageMode.Full_Screen);
        //Set print scaling
        //pdf.getViewerPreferences().setPrintScaling(PrintScalingMode.App_Default);

        //Save the file
        pdf.saveToFile("Preference.pdf");
        //Close
        pdf.close();
    }
}

Output

vp.png