Tuesday, 19 October 2021

Set Zoom Factor and Viewer Preference for PDF Documents in Java

Zoom Factor lets you zoom the display by a scale factor, and the Viewer Preference allows you to control the way the document shall be presented. This article will introduce how to set Zoom Factor and Viewer Preference for PDF documents using Java codes.

TOOL: Free Spire.PDF for Java (you can get it from the link, or directly add it to your project by using the following Maven configurations)

<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.free</artifactId>
<version>4.4.1</version>
</dependency>
</dependencies>

USING THE CODE

Set Zoom Factor

You can find the following steps to set zoom factor for a PDF document using Java codes.

l  Load a PDF sample document using PdfDocument.loadFromFile() method

l  Get the specified page of PDF using PdfDocument.getPages().get() method

l  Set PDF destination mode and action

l  Set zoom factor using PdfDocument.setZoom() method

l  Save the resulting document to a specified path using PdfDocument.saveToFile() method

Full code sample are shown as below.

import com.spire.pdf.*;
import com.spire.pdf.actions.*;
import com.spire.pdf.general.*;
import java.awt.geom.*;

public class ZoomFactor {
public static void main(String[] args) {
//Load the sample document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf");

//Get the first page of PDF
PdfPageBase page = doc.getPages().get(0);

//Set pdf destination
PdfDestination dest = new PdfDestination(page);
dest.setMode(PdfDestinationMode.Location);
dest.setLocation(new Point2D.Float(-40f, -40f));

//Set zoom factor
dest.setZoom(0.8f);

//Set action
PdfGoToAction gotoAction = new PdfGoToAction(dest);
doc.setAfterOpenAction(gotoAction);

//Save pdf document
String output = "output/setZoomFactor.pdf";
doc.saveToFile(output);
}
}

Output


Set Viewer Preference

Free Spire.PDF for Java supports setting viewer preference for a PDF document, for example, you can

specify whether to position the document’s window in the center of the screen using PdfDocument

.getViewerPreferences().setCenterWindow() method and whether the title of the document should be

displayed in the window’s title bar using PdfDocument.getViewerPreferences().setDisplayTitle()

method. The code snippets below will give you detailed information.

import com.spire.pdf.*;

public class ViewerPreference {
public static void main(String[] args) {
//Load the sample document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf");

//Set viewer reference
doc.getViewerPreferences().setCenterWindow(true);
doc.getViewerPreferences().setDisplayTitle(false);
doc.getViewerPreferences().setFitWindow(false);
doc.getViewerPreferences().setHideMenubar(true);
doc.getViewerPreferences().setHideToolbar(true);
doc.getViewerPreferences().setPageLayout(PdfPageLayout.Two_Column_Left);

//Save pdf document
String output = "output/viewerPreference.pdf";
doc.saveToFile(output);
}
}

Output



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...