News Category

Java: Set PDF Viewer Preferences

2022-09-01 07:55:00 Written by  support iceblue
Rate this item
(0 votes)

Preserving and displaying documents precisely is a primary function of PDF. However, the viewing preference settings of different devices and users would still affect the display of PDF documents. To solve this problem, PDF provides the viewer preference entry in a document to control the way the PDF document presents on screen. Without it, PDF documents will display according to the current user’s preference setting. This article will show how to set PDF viewer preferences by programming using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Set Viewer Preferences of a PDF Document

Spire.PDF for Java includes several methods under PdfViewerPreferences class, which can decide whether to center the window, display title, fit the window, and hide menu bar as well as tool bar and set page layout, page mode, and scaling mode. The detailed steps of setting viewer preferences are as follows.

  • Create an object of PdfDocument class.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Get viewer preferences of the document using PdfDocument.getViewerPreferences() method.
  • Set the viewer preferences using the methods under PdfViewerPreferences object.
  • Save the file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

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

        //Create an object of PdfDocument class
        PdfDocument pdf = new PdfDocument();

        //Load a PDF file
        pdf.loadFromFile("C:/Sample3.pdf");

        //Get viewer preferences of the document
        PdfViewerPreferences preferences = pdf.getViewerPreferences();

        //Set viewer preferences
        preferences.setCenterWindow(true);
        preferences.setDisplayTitle(false);
        preferences.setFitWindow(true);
        preferences.setHideMenubar(true);
        preferences.setHideToolbar(true);
        preferences.setPageLayout(PdfPageLayout.Single_Page);
        //preferences.setPageMode(PdfPageMode.Full_Screen);
        //preferences.setPrintScaling(PrintScalingMode.App_Default);

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

Java: Set PDF Viewer Preferences

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 27 July 2023 07:04