Adding Pages to a PDF Document using Java
Last Updated :
28 Jan, 2021
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
- PDDocument(): This constructor used to construct a new PDF document with zero pages.
- PDDocument(COSDocument doc): This constructor uses already existing PDF documents, and then we can add or remove pages.
- 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:
- Create a document.
- Create a blank page.
- Add this page to the document.
- Save the document.
- 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.
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc.An Exception is an unwanted or unexpected event that occurs during the execution of a program (i.e., at runti
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read