public static void partitionPdfFile(String pdfFile, String newFile, int from, int end) { Document document = null; PdfCopy copy = null; try { PdfReader reader = new PdfReader(pdfFile); int n = reader.getNumberOfPages(); if (end == 0) { end = n; } document = new Document(reader.getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(newFile)); document.open(); for (int j = from; j <= end; j++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } document.close(); } catch (Exception e) { e.printStackTrace(); } }
取出指定頁的pdf生成新的pdf,參數說明:
pdfFile為源pdf,
newFile為需要新生成的pdf,
from為需要截取的起始頁,
end為需要截取的末頁。