Make a Booklet from a PDF Document in Java

·

1 min read

Booklet is a small book with just a few pages that usually contains information about a company’s products and promotional events. Sometimes, you may need to turn a PDF document to a printable booklet for better distribution. This article will share how to create a booklet from a PDF document using a free Java API.

Import Dependency

Method 1: Download the free API and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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>5.1.0</version>
    </dependency>
</dependencies>

Sample Code

The PdfDocument.createBooklet( String fileName, float width, float height, boolean doubleSide) method offered by Free Spire.PDF for Java allows you to turn a PDF document to a printable booklet. The complete sample code are shown as below.

import com.spire.pdf.*;

public class PDFBooklet{
    public static void main(String[] args) throws Exception {

        //Create a PdfDocument INSTANCE
        PdfDocument pdf = new PdfDocument();

        //Load a sample PDF document
        pdf.loadFromFile("Sample.pdf");

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

        //Set the width and height of the page
        float width = (float) page.getSize().getWidth()*2;
        float height = (float) page.getSize().getHeight();

        //Create a booklet
        pdf.createBooklet("Sample.pdf", width, height,true);

        //Save the result document
        pdf.saveToFile("Booklet.pdf");
    }
}

Booklet.jpg