Open In App

Adding Pages to a PDF Document using Java

Last Updated : 28 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

PDDocument class of ‘org.apache.pdfbox.pdmodel’ package which extends ‘java.lang.Object‘. is used.

Declaration:

public class PDDocument
extends Object
implements Pageable, Closeable

Pre-requisite: Constructors

  1. PDDocument(): This constructor used to construct a new PDF document with zero pages.
  2. PDDocument(COSDocument doc): This constructor uses already existing PDF documents, and then we can add or remove pages.
  3. PDDocument(COSDocument doc, BaseParser usedParser): This constructor is similar to the above one but it has a parser in it.

Method Using: addPage() Method 

There are many methods in PDDocument class but standard and most frequently used to add anything to PDF be it image or pages, the requirement is only for addPage() method. The addPage() method is used for adding pages in the PDF document. The following code adds a page in a PDF document.

Syntax: To declare addPage() method

public void addPage(PDPage page) ;

This will add a page to the document. This is the easiest method, which will add the page to the root of the hierarchy and set the parent of the page to the root. Hence, so far by now, the page to be added to the document is defined clearly. 

Procedure:

  1. Create a document.
  2. Create a blank page.
  3. Add this page to the document.
  4. Save the document.
  5. Close the document.

Step 1: Creating a Document

An object is needed to be created of PDDocument class which will enable the creation of an empty PDF document. Currently, it does not contain any page.

Syntax: 

PDDocument doc = new PDDocument(); 

Step 2: Creating a Blank Page

PDPage is also a type of class that belongs to the same package as PDDocument which is ‘org.apache.pdfbox.pdmodel‘.

Syntax: 

PDPage page = new PDPage();

Step 3: Adding Page to Document

Here, addPage() method of PDDocument class is used to add the blank to the document which is nothing but an object of PDDocument.

Syntax: 

PDPage page = new PDPage();

Step 4: Saving the Document

After adding pages to the document you have to save that file at the desired location for that we use the save() method which takes a string as a parameter containing the path address.

Syntax: 

doc.save("path");

Step 5: Closing Document

Finally, we have to close the document by using the close() method. If we don’t close it then if another program wants to access that PDF then it will get an error.

Syntax: 

doc.close();

Implementation:

Example 1(A)

Java




// Java Program to add page to a PDF document
  
// Here a page will be created in PDF and saved only
// carried forward to next example
  
// Importing required packages
import java.io.IOException;
// Importing Apache POI modules
import org.apache.pdfbox.pdmodel.*;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Creating PDF document object
        PDDocument doc = new PDDocument();
  
        // Creating a blankpage
        PDPage page = new PDPage();
  
        // Adding the blankpage to the document
        doc.addPage(page);
  
        // Saving the document from the
        // local directory on the system
  
        // Custom directory window path here
        doc.save("F:/sample.pdf");
  
        // Closing the document
        doc.close();
    }
}


Output:

Note: By now there is only 1 page in the PDF document as shown in the markup[1/1] in the above output image. 

Example 1(B)

Java




// Java Program to add pages to PDF
// using addPage() method
  
// Carried forward from above example 
  
// Importing input output classes
import java.io.IOException;
// Importing Apache POI modules
import org.apache.pdfbox.pdmodel.PDDocument;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Step 1: Creating PDF document object
        PDDocument doc = new PDDocument();
  
        // Traversing via for loop responsible
        // for addition of blank pages
  
        // Customly adding pages say
        // number be it 7
        for (int i = 0; i < 7; i++) {
  
            // Step 2: Creating a blankpage
            // using PDPage() method
            PDPage page = new PDPage();
  
            // Step 3: Adding the blankpage to the
            // document using addPage() method
            doc.addPage(page);
        }
  
        // Step 4: Saving the document
        doc.save("F:/sample1.pdf");
  
        // Step 5: Closing the document
        doc.close();
    }
}


Output:

Note: By now, a page has been added to the above image page which is evidently seen in the markup [1/2] in the above output image. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads