Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
This article will use IronPDF in Java to split PDF files from a source PDF file.
IronPDF for Java is 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 the well-established IronPDF Library for .NET Framework. This makes IronPDF for Java a versatile tool for working with PDF documents in various contexts.
IronPDF offers developers methods to render PDF documents into images and extract text and content from PDFs. Additionally, IronPDF is also capable of rendering charts within PDFs, applying watermarks to PDF files, working with PDF forms, and managing digital signatures programmatically.
To make IronPDF work with PDFs 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.ironpdf</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.ironpdf</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
Another dependency required is SLF4J. Add the SLF4J dependency in the pom.xml
file.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
<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, import the IronPDF required classes in Java code. Add the following code on top of the "Main.java" file:
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;
import java.io.IOException;
import java.nio.file.Paths;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;
import java.io.IOException;
import java.nio.file.Paths;
Now, set your license key using the IronPDF setLicenseKey()
method in the Main
method:
License.setLicenseKey("Your license key");
License.setLicenseKey("Your license key");
To split a PDF file into multiple single-page files, it is necessary 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 can be used for this demonstration.
// HTML content used to create a four-page PDF
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>";
// Render HTML to a PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
// Save the PDF document
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
// HTML content used to create a four-page PDF
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>";
// Render HTML to a PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
// Save the PDF document
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
The PDF Document looks like this:
Creating New PDFs with IronPDF
As an alternative to the previous section, the next code sample uses the PdfDocument.fromFile
method to open an existing PDF file using IronPDF.
// Open the existing PDF document
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));
// Open the existing PDF document
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 straightforward. 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"));
// Take the second page
PdfDocument page2Doc = pdf.copyPage(1);
page2Doc.saveAs(Paths.get("assets/split2.pdf"));
// Take the third page
PdfDocument page3Doc = pdf.copyPage(2);
page3Doc.saveAs(Paths.get("assets/split3.pdf"));
// Take the fourth page
PdfDocument page4Doc = pdf.copyPage(3);
page4Doc.saveAs(Paths.get("assets/split4.pdf"));
// Take the first page
PdfDocument page1Doc = pdf.copyPage(0);
page1Doc.saveAs(Paths.get("assets/split1.pdf"));
// Take the second page
PdfDocument page2Doc = pdf.copyPage(1);
page2Doc.saveAs(Paths.get("assets/split2.pdf"));
// Take the third page
PdfDocument page3Doc = pdf.copyPage(2);
page3Doc.saveAs(Paths.get("assets/split3.pdf"));
// Take the fourth page
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, save each page in a separate file.
Page 1
Page 2
Page 3
Page 4
The copyPages
method can also split a PDF by a range of pages. Below, we split the sample PDF evenly in half.
// Copy the first two pages into a new PDF document
PdfDocument halfPages = pdf.copyPages(0, 1);
halfPages.saveAs(Paths.get("assets/halfPages.pdf"));
// Copy the last two pages into another PDF document
PdfDocument endPages = pdf.copyPages(2, 3);
endPages.saveAs(Paths.get("assets/endPages.pdf"));
// Copy the first two pages into a new PDF document
PdfDocument halfPages = pdf.copyPages(0, 1);
halfPages.saveAs(Paths.get("assets/halfPages.pdf"));
// Copy the last two pages into another PDF document
PdfDocument endPages = pdf.copyPages(2, 3);
endPages.saveAs(Paths.get("assets/endPages.pdf"));
Splitting a PDF into Two Halves
The complete code example is shown below:
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set the IronPDF license key
License.setLicenseKey("Your license key");
// HTML content to create a four-page PDF document
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>";
// Render HTML to a PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
// Open the existing PDF document
pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));
// Split each PDF page into separate documents
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"));
// Split the PDF into two halves
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"));
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set the IronPDF license key
License.setLicenseKey("Your license key");
// HTML content to create a four-page PDF document
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>";
// Render HTML to a PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
// Open the existing PDF document
pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));
// Split each PDF page into separate documents
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"));
// Split the PDF into two halves
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 PDF documents effortlessly.
This article explored how a Java program opens an existing PDF document and splits a PDF file into multiple PDFs 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 it can be licensed for commercial use with flexible options. The IronPDF Lite package starts at $749.
IronPDF for Java is a Java library designed to work with PDF documents, offering functionality to render, extract text and content, apply watermarks, manage digital signatures, and more. It is based on the IronPDF Library for .NET Framework.
To split a PDF file using IronPDF in Java, you can use the `copyPage` method to copy individual pages from the original document and save each page as a separate PDF file.
The prerequisites include having a current version of JDK installed, an IDE like NetBeans, Eclipse, or IntelliJ, Maven for managing project dependencies, and the IronPDF Java library.
You need to import classes such as `com.ironsoftware.ironpdf.PdfDocument` and `com.ironsoftware.ironpdf.License` in your Java code to use IronPDF functionalities.
Yes, IronPDF can open password-protected PDF files by providing the document password as a second argument to the `fromFile` method.
You can split a PDF into two halves by using the `copyPages` method to copy a range of pages and save each half as a separate PDF document.
Yes, IronPDF allows you to merge PDF documents effortlessly, providing an easy way to combine multiple PDFs into a single document.
You can set the IronPDF license key in your Java application by using the `License.setLicenseKey()` method in your main method.
IronPDF simplifies working with PDF files in Java, allowing developers to create, modify, split, and merge PDFs with minimal code, making it a powerful tool for handling various PDF-related tasks.
Yes, you can use IronPDF in production for free, and it also offers flexible licensing options for commercial use.