import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.StringTokenizer; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.parser.PdfTextExtractor; public class SplitPDF { public static final String pdfDir = "c:\\Mypdf\\"; public static void splitPDF(PdfReader inputPDF, OutputStream 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(); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data PdfImportedPage page; while (fromPage <= toPage) { document.newPage(); page = writer.getImportedPage(inputPDF, fromPage); // page.getPdfDocument(). cb.addTemplate(page, 0, 0); fromPage++; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } public static void splitPDFByBookmarks() { try { InputStream input = new FileInputStream("c:\\Mypdf\\1.pdf"); PdfReader reader = new PdfReader(input); int totalPages = reader.getNumberOfPages(); int noOfPages = 1; int firstToken = 1; int fourthToken = 4; for (int pageNum = 1; pageNum < totalPages; pageNum++) { String str = PdfTextExtractor.getTextFromPage(reader, pageNum); StringTokenizer strTokens = new StringTokenizer(str, "\n"); int tokenCount = 0; while (strTokens.hasMoreTokens()) { tokenCount++; String token = strTokens.nextToken(); if (firstToken == tokenCount) { String pageDetails = token.substring(token .indexOf("page") + 5); StringTokenizer strT = new StringTokenizer(pageDetails, "/"); strT.nextElement(); noOfPages = Integer.parseInt(strT.nextToken().trim()); } else if (tokenCount == fourthToken) { String pdfDocumentName = "c:\\Mypdf\\" + token + ".pdf"; OutputStream output = new FileOutputStream( pdfDocumentName); int endPage = pageNum + noOfPages - 1; SplitPDF.splitPDF(reader, output, pageNum, endPage); pageNum = endPage; break; } } } } catch (IOException e) { System.out.println(e.getMessage()); } } private static void extractBookmarkToPDF(PdfReader reader, int pageFrom, int pageTo, String outputName, String outputFolder) { Document document = new Document(); OutputStream os = null; try { os = new FileOutputStream(outputFolder + outputName); // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, os); document.open(); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data PdfImportedPage page; while (pageFrom < pageTo) { document.newPage(); page = writer.getImportedPage(reader, pageFrom); cb.addTemplate(page, 0, 0); pageFrom++; } os.flush(); document.close(); os.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { if (document.isOpen()) document.close(); try { if (os != null) os.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } } } }