Page Setting

Page Setting (5)

This article will demonstrate how to set the zoom factor/percentage (such as default, 100 percent or any other zoom factors as required) and the viewer preference by using Spire.PDF for Java in Java applications.

Set the zoom factor

import com.spire.pdf.*;
import com.spire.pdf.actions.*;
import com.spire.pdf.general.*;

import java.awt.geom.*;

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

        //Load the sample document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("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:

Java set the viewer preference and zoom factor for PDF

Set the viewer preference

import com.spire.pdf.*;

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

        //Load the sample document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("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:

Java set the viewer preference and zoom factor for PDF

Saturday, 12 October 2019 09:41

Detect PDF Page Orientation in Java

Written by support iceblue

Spire.PDF for Java supports detecting the orientation of a PDF page by comparing the value of page width and page height. This article shows how to use Spire.PDF for Java to accomplish this function.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class DetectPageOrientation {
    public static void main(String[] args){
        //Load PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Fields.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Compare the value of page width and height
        if (page.getSize().getWidth()> page.getSize().getHeight()){
            System.out.println("The page orientation is Landscape");
        }
        else{
            System.out.println("The page orientation is Portrait");
        }
    }
}

Output

Detect PDF Page Orientation in Java

Monday, 15 April 2019 08:36

Rotate a PDF page in Java

Written by support iceblue

This article demonstrates how to rotate a PDF page using Spire.PDF for Java.

Below is the screenshot of the input.pdf file:

Rotate a PDF page in Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageRotateAngle;

import java.io.IOException;

public class RotatePDFPage {
    public static void main(String[] args) throws IOException {
        //Load the PDF document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Input.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Get the original rotation angle of the page
        int rotateAngle = page.getRotation().getValue();

        //Rotate the PDF page 90 degrees clockwise based on the original rotation angle
        rotateAngle += PdfPageRotateAngle.Rotate_Angle_90.getValue();
        page.setRotation((PdfPageRotateAngle.fromValue(rotateAngle)));

        //Save the PDF document
        pdf.saveToFile("Rotated.pdf");
    }
}

Output:

Rotate a PDF page in Java

This article is going to demonstrate how to set background color and background image for a PDF file using Spire.PDF for Java.

The original PDF file:

Set Background Color and Background Image for PDF in Java

Set background color

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

import java.awt.*;

public class SetBackgroundColor {
    public static void main(String[] args) {
        
        //Load PDF file
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("original.pdf");

        PdfPageBase page;
        int pageCount = doc.getPages().getCount();

        //Set Background color
        for(int i = 0; i < pageCount; i ++) {
            page = doc.getPages().get(i);
            page.setBackgroundColor(Color.yellow);
        }

        //Save the file
        doc.saveToFile("BackgroundColor.pdf");
    }
}

Output:

Set Background Color and Background Image for PDF in Java

Set background image

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

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

        //Load PDF file
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("original.pdf");
        
        PdfPageBase page;
        int pageCount = doc.getPages().getCount();

        //Set background image
        for(int i = 0; i < pageCount; i ++) {
            page = doc.getPages().get(i);
            page.setBackgroundImage("Background.jpg");
        }

        //Save the file
        doc.saveToFile("BackgroundImage.pdf");
    }
}

Output:

Set Background Color and Background Image for PDF in Java

Tuesday, 09 October 2018 06:47

Get the Number of Pages in a PDF File in Java

Written by support iceblue

This article demonstrates how to determine the number of pages in a PDF file using Spire.PDF for JAVA.

The following screenshot shows a PDF file which contains 73 pages:

Get the Number of Pages in a PDF File in Java

Code snippets:

import com.spire.pdf.PdfDocument;

		public class CountPagesOfPDF {

		public static void main(String[] args) {
		
		//Instantiate a PdfDocument object
		PdfDocument doc=new PdfDocument();
		//Load the PDF file
		doc.loadFromFile("Test.pdf");

		//Get the number of pages in the PDF file        
		int pageCount = doc.getPages().getCount();
        
		System.out.print(pageCount);

	}
}

Outout:

Get the Number of Pages in a PDF File in Java