Split PDFs
This code example shows how Java developers can leverage IronPDF's PDF editing functionality to achieve a very common use case: splitting a PDF into smaller PDFs.
Using the copyPages
method, developers can quickly produce a new PDF document containing an inclusive set of pages from the source document.
The featured example uses this approach to produce two new PDF documents containing the first page and the second and third pages, respectively, of a newly-rendered PDF document. This preserves the content and structure of the source document in its original form, while copying portions of its contents to separate files—effectively "splitting" the file for all intents and purposes. This approach is useful in situations where the developer must retain the source file in its original form for backup purposes.
A slightly different approach to splitting a PDF document (one that doesn't keep the original file) can be accomplished with the copyPages
method in conjunction with the removePages
method.
How to Split PDF Files in Java
- Install IronPDF Java library to split PDF documents
- Import existing PDF or render a new PDF in Java
- Split PDF by using
copyPages
method to copy the PDF pages to anotherPdfDocument
object - Export the
PdfDocument
withsaveAs
method - Perform step 3 in just 1 line of Java code
For more information on utilizing IronPDF's extensive PDF manipulation features, visit the IronPDF PDF Library for Java Documentation.
// Example code for splitting a PDF using IronPDF in Java
// Import IronPDF library classes
import com.ironsoftware.IronPdf.PdfDocument;
public class SplitPdfExample {
public static void main(String[] args) {
// Load the existing PDF document
PdfDocument sourcePdf = PdfDocument.fromFile("source-document.pdf");
// Split the PDF by copying the first page to a new document
PdfDocument firstPagePdf = sourcePdf.copyPages(0, 0);
firstPagePdf.saveAs("first-page.pdf");
// Split the PDF by copying the second and third pages to another document
PdfDocument secondAndThirdPagesPdf = sourcePdf.copyPages(1, 2);
secondAndThirdPagesPdf.saveAs("second-and-third-pages.pdf");
}
}
// Example code for splitting a PDF using IronPDF in Java
// Import IronPDF library classes
import com.ironsoftware.IronPdf.PdfDocument;
public class SplitPdfExample {
public static void main(String[] args) {
// Load the existing PDF document
PdfDocument sourcePdf = PdfDocument.fromFile("source-document.pdf");
// Split the PDF by copying the first page to a new document
PdfDocument firstPagePdf = sourcePdf.copyPages(0, 0);
firstPagePdf.saveAs("first-page.pdf");
// Split the PDF by copying the second and third pages to another document
PdfDocument secondAndThirdPagesPdf = sourcePdf.copyPages(1, 2);
secondAndThirdPagesPdf.saveAs("second-and-third-pages.pdf");
}
}
- Comments:
- The
PdfDocument.fromFile()
method loads an existing PDF from the file path specified. - The
copyPages(int fromPage, int toPage)
method copies pages from the specified start to end indices (inclusive) from the source document to a new document. - The
saveAs(String path)
method exports the modified PdfDocument to a file at the specified path.
- The
In this example, the PDF is split into two separate documents: one containing the first page and another containing the second and third pages. This method retains the original PDF file in its entirety.