Splitting PDF File Using Java iText API
Previously I wrote a tutorial about how to merge two or more PDF files. This tutorial we will learn about splitting a PDF with multiple pages into multiple PDFs using the Java iText API.
Library Required
itext-2.1.4.jar
File: SplitPDF.java
package com.simplecode.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfImportedPage; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfWriter; public class SplitPDF { // Split PDF files in Java using iText JAR public static void main(String[] args) { try { SplitPDF.splitPDF(new FileInputStream("C:\File.pdf"), new FileOutputStream("C:\Output1.pdf"), 1, 15); SplitPDF.splitPDF(new FileInputStream("C:\File.pdf"), new FileOutputStream("C:\Output2.pdf"), 15, 30); } catch (Exception e) { System.err.println(e.getMessage()); } } public static void splitPDF(FileInputStream inputStream, FileOutputStream outputStream, int fromPage, int toPage) { Document document = new Document(); try { PdfReader inputPDF = new PdfReader(inputStream); int totalPages = inputPDF.getNumberOfPages(); // Make fromPage equals to toPage if it is greater if (fromPage > toPage) { fromPage = toPage; } if (toPage > totalPages) { toPage = totalPages; } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); // Holds the PDF data PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page; while (fromPage <= toPage) { document.newPage(); page = writer.getImportedPage(inputPDF, fromPage); cb.addTemplate(page, 0, 0); fromPage++; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { System.err.println(e.getMessage()); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } } } }
Read More
Merge PDF files using java iText
Library Required
itext-2.1.4.jar
File: MergePDF.java
package com.simplecode.util; //Please include the itext-2.1.4.jar in the classpath import java.util.List; import java.util.Iterator; import java.util.ArrayList; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfImportedPage; public class MergePDF { public static void main(String[] arg) { try { List<InputStream> pdfs = new ArrayList<InputStream>(); pdfs.add(new FileInputStream("C:\File1.pdf")); pdfs.add(new FileInputStream("C:\File2.pdf")); OutputStream output = new FileOutputStream("C:\Merge.pdf"); // boolean flag that represents whether you need to include page number or not MergePDF.concatPDFs(pdfs, output, true); } catch (Exception e) { System.err.println("Exception occurs : " + e.getMessage()); } } public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) { Document document = new Document(); try { List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); // Holds the PDF data PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader,pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); cb.setFontAndSize(bf, 9); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { System.err.println("Exception : " + e.getMessage()); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { System.err.println("Exception : " + ioe.getMessage()); } } } }
Related Article
Splitting PDF File Using Java iText API