Page Setting

Page Setting (4)

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