Java: Rearrange Pages in PDF

2022-01-07 08:35:00 Written by Koohji

When you receive a PDF file with pages out of order, you may need to change the page order for a better viewing experience. In this article, you will learn how to programmatically rearrange pages in a PDF file 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>11.12.16</version>
    </dependency>
</dependencies>

Rearrange Pages in PDF

The PdfDocument.getPages().reArrange() method offered by Spire.PDF for Java allows you to change the PDF page order quickly and effortlessly. The detailed steps are as follows.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.loadFromFile() method.
  • Rearrange the pages using PdfDocument.getPages().reArrange() method.
  • Save the document to another file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;

public class RearrangePages {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF file
        doc.loadFromFile("input.pdf");

        //Rearrange pages by setting a new page order
        doc.getPages().reArrange(new int[]{0, 2, 1, 3});

        //Save the document to another file
        doc.saveToFile("ChangeOrder.pdf");
        doc.close();
    }
}

Java: Rearrange Pages in PDF

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.

This article demonstrates how to compress high-resolution images of a PDF document using Spire.PDF for Java. Images in low-resolution will not be compressed anymore.

public static void main(String[] args) throws Exception {
        //Load the sample PDF document
        PdfDocument doc = new PdfDocument("C:\\Users\\Administrator\\Desktop\\Images.pdf");

        //Set IncrementalUpdate to false
        doc.getFileInfo().setIncrementalUpdate(false);

        //Declare a PdfPageBase variable
        PdfPageBase page;

        // Create the PdfImageHelper object
        PdfImageHelper imageHelper = new PdfImageHelper();

        //Loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //Get the specific page
            page = doc.getPages().get(i);
            if (page != null) {

                if(page.getImagesInfo() != null){

                    //Loop through the images in the page
                    for (PdfImageInfo info : imageHelper.getImagesInfo(page))

                        //Use tryCompressImage method the compress high-resolution images
                        info.tryCompressImage();
                    }
                }
            }
        //Save to file
        doc.saveToFile("output/Compressed.pdf");
        }

Compress High-resolution Images in PDF in Java

This article demonstrates how to detect if a PDF file is a portfolio in Java using Spire.PDF for Java.

The following is the screenshot of the input PDF:

Detect if a PDF File is a Portfolio in Java

import com.spire.pdf.PdfDocument;

public class DetectPortfolio {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument doc = new PdfDocument();
        //Load the PDF file
        doc.loadFromFile("Portfolio.pdf");

        //Detect if the PDF is a portfolio
        boolean value = doc.isPortfolio();
        if (value)
        {
            System.out.println("The document is a portfolio.");
        }
        else
        {
            System.out.println("The document is not a portfolio.");
        }
    }
}

Output:

Detect if a PDF File is a Portfolio in Java

page 27