package com.peter.mytool; import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.pdf.PdfCopy; import com.lowagie.text.pdf.PdfImportedPage; import com.lowagie.text.pdf.PdfReader; public class PDFTool { public static void main(String[] args) { String fileName = "I:\\iOS电子版资料\\IOS5.pdf"; int[] pages ={1,31,47,65,97,139,153,179,193,231,251, 283,299,325,347,369,389,423,445,457,479,517,541}; String titles[] = new String[]{ "00-Introduction", "01-Getting Started with iOS 5 Programming", "02-Writing Your First Hello World! Application", "03-Understanding Views, Outlets,and Actions", "04-Exploring the Different View Controllers", "05-Enabling Multi-Platform Support for the iPhone and iPad", "06-Handling Keyboard Inputs", "07-Supporting Screen Rotations", "08-Creating and Persisting Data Using the Table View", "09-Using Application Preferences", "10-File Handling", "11-Database Storage Using SQLite", "12-Programming iCloud", "13-Performing Simple Animations and Video Playback", "14-Accessing Built-In Applications", "15-Accessing the Sensors", "16-Using Web Services", "17-Bluetooth Programming", "18-Bonjour Programming", "19-Programming Remote Notifications Using Apple Push Notification Services", "20-Displaying Maps", "21-Programming Background Applications", "22-Appendix" }; //splitPDFIntoSubfiles(fileName, pages); splitPDFIntoSubfiles(fileName, pages,titles); } /** * 把pdf文件按照指定的页码进行分页 * @param pdfFile 指定dpf文件 * @param pages 指定页码 * @param titles 指定标题 */ public static void splitPDFIntoSubfiles(String pdfFile,int[] pages, String titles[]) { try { PdfReader reader = new PdfReader(pdfFile); int totalPages = reader.getNumberOfPages(); String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1); String subfileName = pdfFile.substring(pdfFile.lastIndexOf("\\")+1) ; subfileName = subfileName.substring(0, subfileName.lastIndexOf("."))+"-C"; String savepath = ""; int end = 0; for(int i=0; i<pages.length; i++){ if(i == pages.length-1){ end = totalPages; }else{ end = pages[i+1]-1; } savepath = staticpath + subfileName + titles[i] +".pdf"; createNewPDFFile(reader,pages[i],end,savepath); } System.out.println(" splitPDFIntoSubfiles is done..... "); } catch (Exception e) { e.printStackTrace(); } } /** * 把pdf文件按照指定的页码进行分页 * @param pdfFile 指定dpf文件 * @param pages 指定页码 */ public static void splitPDFIntoSubfiles(String pdfFile,int[] pages) { try { PdfReader reader = new PdfReader(pdfFile); int totalPages = reader.getNumberOfPages(); String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1); String subfileName = pdfFile.substring(pdfFile.lastIndexOf("\\")+1) ; subfileName = subfileName.substring(0, subfileName.lastIndexOf("."))+"-Chapter"; String savepath = ""; int end = 0; String ChapterNO = ""; for(int i=0; i<pages.length; i++){ if(i == pages.length-1){ end = totalPages; }else{ end = pages[i+1]-1; } if(i<10){ ChapterNO = "0"; } savepath = staticpath + subfileName + ChapterNO +i+".pdf"; createNewPDFFile(reader,pages[i],end,savepath); } } catch (Exception e) { e.printStackTrace(); } } /** * 从PdfReader的from页到end页copy一份,生成一份指定文件目录的文件savepath * @param reader PdfReader 读取的源文件 * @param from 开始页码 * @param end 结束页面 * @param savepath 保存新的路径 * @throws Exception */ private static void createNewPDFFile(PdfReader reader, int from , int end,String savepath) throws Exception{ Document document = null; PdfCopy copy = null; document = new Document(reader.getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(savepath)); document.open(); for(int j=from; j<=end; j++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } document.close(); } }
直接上程序,不加解释。因为很简单,可以直接看得懂。
至于所使用的jar见我之前的博文