Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
PDF (Portable Document Format) is the most popular format for sending and receiving data online. This digital format allows you to preserve the formatting of the data sent which also helps in printing the document. PDF files provide the facility to integrate various types of content in your document such as text, images, 3d graphics, videos, audio files, editable fields, buttons, and hyperlinks.
As a developer, in Java working with PDF's is common, either to store data or retrieve data. At times it can be difficult while performing these tasks but thanks to IronPDF - A Java Library, it is now very easy to work with PDF files. We can merge multiple single page files into one PDF file. Similarly, a single PDF file can be split into multiple PDF files. In this article, I will use IronPDF in Java to split PDF files from a source PDF file.
IronPDF a Java Library that prioritizes accuracy, ease of use, and speed. It is especially designed for Java and is easy to use when working with PDF documents. It inherits all its functionality from a well established IronPDF Library for .NET Framework. This makes IronPDF for Java a versatile tool for working with PDF document in various contexts. It is a complete package for generating PDF's, formatting PDF's, and editing PDF's.
To make IronPDF work with PDF's in a Java Maven project, you will need to make sure that you have the following prerequisites:
IronPDF Java Library: Now you require the IronPDF Java library. This can be done by adding the following dependency to your project's pom.xml file. Maven will automatically download and install it in the project.
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>com.ironsoftware</artifactId>
<version>2024.8.1</version>
</dependency>
Another dependency required is SLF4J. Add Slf4j dependency in the pom.xml file.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
Once everything is downloaded and installed in your split PDF file Java program, you are ready to use the IronPDF library.
Firstly, we need to import the IronPDF required classes in Java code. Add the following code on top of "Main.java" file:
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;
Now, set your license key using the IronPDF setLicenseKey() method in Main method:
License.setLicenseKey("Your license key");
To split a PDF file into multiple single page files we first need to either create a PDF file with multiple pages or open a PDF file with multiple pages.
The next code sample will create a four-page PDF that we can use for our demonstration.
String html = "<p> [PDF With Multiple Pages] </p>"
+ "<p> 1st Page </p>"
+ "<div style = 'page-break-after: always;' ></div>"
+ "<p> 2nd Page</p>"
+ "<div style = 'page-break-after: always;' ></div>"
+ "<p> 3rd Page</p>"
+ "<div style = 'page-break-after: always;' ></div>"
+ "<p> 4th Page</p>";
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
The PDF Document looks like this:
As an alternative to the previous section, the next code sample uses the PdfDocument.fromFile
method to open how to open an existing PDF file using IronPDF.
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));
You can also open a password protected file by providing a document password as a second argument to the fromFile
method.
To split a PDF file the code is very easy and simple. We can simply copy several pages from the original document using the copyPage
method, as shown below:
//take the first page
PdfDocument page1Doc = pdf.copyPage(0);
page1Doc.saveAs(Paths.get("assets/split1.pdf"));
PdfDocument page2Doc = pdf.copyPage(1);
page2Doc.saveAs(Paths.get("assets/split2.pdf"));
PdfDocument page3Doc = pdf.copyPage(2);
page3Doc.saveAs(Paths.get("assets/split3.pdf"));
PdfDocument page4Doc = pdf.copyPage(3);
page4Doc.saveAs(Paths.get("assets/split4.pdf"));
The PDF file is split by passing the index number as an argument to the copyPage
method. Then, we save each page in a separate file.
Page 1
Page 2
Page 3
Page 4
The copyPages
can also split a PDF by a range of pages. Below, we split the sample PDF evenly in half.
PdfDocument halfPages = pdf.copyPages(0, 1);
halfPages.saveAs(Paths.get("assets/halfPages.pdf"));
PdfDocument endPages = pdf.copyPages(2, 3);
endPages.saveAs(Paths.get("assets/endPages.pdf"));
The complete code example is shown below:
public class Main {
public static void main(String [] args) throws IOException {
String html = "<p> [PDF With Multiple Pages] </p>"
+ "<p> 1st Page </p>"
+ "<div style = 'page-break-after: always;' ></div>"
+ "<p> 2nd Page</p>"
+ "<div style = 'page-break-after: always;' ></div>"
+ "<p> 3rd Page</p>"
+ "<div style = 'page-break-after: always;' ></div>"
+ "<p> 4th Page</p>";
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));
//take the first page
PdfDocument page1Doc = pdf.copyPage(0);
page1Doc.saveAs(Paths.get("assets/split1.pdf"));
PdfDocument page2Doc = pdf.copyPage(1);
page2Doc.saveAs(Paths.get("assets/split2.pdf"));
PdfDocument page3Doc = pdf.copyPage(2);
page3Doc.saveAs(Paths.get("assets/split3.pdf"));
PdfDocument page4Doc = pdf.copyPage(3);
page4Doc.saveAs(Paths.get("assets/split4.pdf"));
PdfDocument halfPages = pdf.copyPages(0, 1);
halfPages.saveAs(Paths.get("assets/halfPages.pdf"));
PdfDocument endPages = pdf.copyPages(2, 3);
endPages.saveAs(Paths.get("assets/endPages.pdf"));
}
}
IronPDF can also merge any number of PDF documents.
In this article, we looked at how Java opens an existing PDF document and splits a PDF file into multiple PDF's using the IronPDF Library.
IronPDF makes the life of a developer a lot easier while working with PDF files in Java. Whether you want to create a new document or work with existing PDF documents, IronPDF helps to achieve all PDF related tasks with almost a single line of code.
You can use IronPDF in production for free, and can be licensed for commercial use. The IronPDF Lite package starts at $749.
9 .NET API products for your office documents