书签合并拆分pdf样例"/>
java带书签合并拆分pdf样例
java带书签合并拆分pdf样例
本人最近做民航维修手册发布任务时遇到合并拆分pdf并且插入附件丢失书签问题
//封面public static final String COVERS_PAGE = "01_covers";//批准页public static final String APPROVE_PAGE = "02_approve";//维修总裁声明public static final String DECLARE_PAGE = "03_declare";//总目录public static final String CATALOG_PAGE = "04_catalog";//批准有效页清单public static final String APPROVE_VALID_PAGE = "05_approvevalid";//有效页清单public static final String VALID_PAGE = "06_valid";//改版记录public static final String REVISION_PAGE = "07_revision";//插换页记录public static final String PLUGCHG_PAGE = "08_plugchg";//第一部分修订说明public static final String AMEND_PAGE = "09_first";//第二部分前言Introductionpublic static final String INSTRODUCTION_PAGE = "10_second";//第三部分定期维修项目public static final String THIRD_PAGE = "11_third";//第四部分部件维修控制public static final String FOUR_PAGE = "12_four";//第五部分public static final String FIVE_PAGE = "13_five";//第六部分public static final String SIX_PAGE = "14_six";public static final String DIRECTION_START = "start";public static final String DIRECTION_END = "end";public static void main(String args[]) throws InvalidPasswordException, IOException {ArrayList flist = new ArrayList();flist.add("C:/Users/Xu1fw/Desktop/qdal/start2.pdf");flist.add("C:/Users/Xu1fw/Desktop/qdal/six.pdf");
// flist.add("C:/Users/Xu1fw/Desktop/qdal/end.pdf");//flist.add("d:/eotest/2014910__487506.pdf");// flist.add("d:/eotest/sect2.pdf.pdf.pdf");try {
// PDFUtilposeMTPDF(flist, "D:/ALL80/CAOM012427.pdf");PDFUtil.mergePdfFiles(flist, "C:/Users/Xu1fw/Desktop/qdal/merge2.pdf",SIX_PAGE,589);} catch (Exception e) {e.printStackTrace();}}public static void main22(String[] args) {String filePath = "C:/Users/Xu1fw/Desktop/qdal/MP07.pdf";File file = new File(filePath);PDDocument document = null;try {if(file != null && file.isFile()){System.out.println(file.getPath());document = PDDocument.load(file, MemoryUsageSetting.setupTempFileOnly());}int pages = document.getNumberOfPages();System.out.println(pages);
// String chapter = "D:/ALL80/dd.pdf";String chapter = "C:/Users/Xu1fw/Desktop/qdal/01.pdf";
// splitPdfByPages(document, 0, 2, chapter);splitPDF("C:/Users/Xu1fw/Desktop/qdal/MP07.pdf","C:/Users/Xu1fw/Desktop/qdal/start2.pdf",1,589,"14_six","start");
// splitPDF("C:/Users/Xu1fw/Desktop/qdal/MP07.pdf","C:/Users/Xu1fw/Desktop/qdal/02.pdf"
// ,3,4,"02_six","end");
// String chapter2 = "D:/ALL80/ff.pdf";
// splitPdfByPages(document, 3, pages, chapter2);
// ArrayList flist = new ArrayList();
// flist.add(chapter);
// flist.add("D:/ALL80/aa.pdf");
// flist.add(chapter2);
// PDFUtil.mergePdfFiles(flist, "D:/ALL80/bc.pdf");} catch (Exception e) {e.printStackTrace();}finally{try {if(document != null){document.close();}} catch (IOException e) {e.printStackTrace();}}}public static void splitPDF(String bytes, String newFile, int start, int end,String bookmark,String direction) {Document document = null;PdfCopy copy = null;PdfReader reader = null;try {reader = new PdfReader(bytes);List bookmarkList = SimpleBookmark.getBookmark(reader);
// System.out.println(bookmarkList.size());List resultBookmark = new ArrayList();//获取pdf页数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 = start; j <= end; j++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, j);copy.addPage(page);}//获取当前书签层级String levelStr = bookmark.substring(0,bookmark.indexOf("_"));int bookmarkLevel = 0;if(levelStr.startsWith("0")){bookmarkLevel = Integer.parseInt(levelStr.substring(1)); }else{bookmarkLevel = Integer.parseInt(levelStr);}
// int bookmarkLevel = bookmark.substring(beginIndex)//判断当前书签层级if(DIRECTION_START.equalsIgnoreCase(direction)){for(int i = 0;i < bookmarkLevel-1;i++){HashMap bk = (HashMap)bookmarkList.get(i);resultBookmark.add(bk); }}else{for(int i = bookmarkLevel;i < bookmarkList.size();i++){HashMap bk = (HashMap)bookmarkList.get(i);resultBookmark.add(bk); }}copy.setOutlines(resultBookmark);} catch (Exception e) {e.printStackTrace();System.err.println("split pdf file error:" + e.getMessage());}finally{if(document != null){document.close();}if(reader != null){reader.close();}}}public static void mergePdfFiles(ArrayList filelist, String savepath,String level,int posPageNum) {Document document = null;try {document = new Document(new PdfReader((String) filelist.get(0)).getPageSize(1));PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));document.open();List resultBookmark = new ArrayList();for (int i = 0; i < filelist.size(); i++) {PdfReader reader = new PdfReader((String) filelist.get(i));List bookmarkList = SimpleBookmark.getBookmark(reader);//如果为要插入的附件if(i == 1){ if(SIX_PAGE.equalsIgnoreCase(level)){if(bookmarkList != null && bookmarkList.size() > 0){dealHasBookmark(bookmarkList, resultBookmark, level, posPageNum);}else{dealNotBookmark(resultBookmark, level, posPageNum); }}else{if(bookmarkList != null && bookmarkList.size() > 0){dealHasBookmark(bookmarkList, resultBookmark, level, posPageNum);}else{dealNotBookmark(resultBookmark, level, posPageNum); }}}else{resultBookmark.addAll(bookmarkList); }//测试跳转页面
// if(i==1){
// if(bookmarkList != null){
// for(Object o : bookmarkList){
// HashMap bk = (HashMap)o;
// resultBookmark.add(bk);
// System.out.println((String)bk.get("Title"));
// if(((String)bk.get("Title")).contains("MP批准页")){
bk.remove("Named");
// bk.put("Action", "GoTo");
// bk.put("Page", "3 FitH 846");
// }
for(Object o1 :bk.keySet()){
System.out.println((String)o1+"-------"+bk.get((String)o1));
}
// }
// }
// }else{
// resultBookmark.addAll(bookmarkList);
// }int n = reader.getNumberOfPages();for (int j = 1; j <= n; j++) {PdfImportedPage page = copy.getImportedPage(reader, j);copy.addPage(page);}if(reader != null){reader.close();}}copy.setOutlines(resultBookmark);} catch (IOException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();}finally{document.close();}}public static void dealHasBookmark(List bookmarkList, List resultBookmark, String level,int posPageNum) {if (SIX_PAGE.equalsIgnoreCase(level)) {for (Object o : bookmarkList) {HashMap bk = (HashMap) o;
// resultBookmark.add(bk);
// if (((String) bk.get("Title")).contains("部件寿命限制清单")) {// bk.remove("Named");bk.put("Action", "GoTo");bk.put("Page", posPageNum+" FitH 600");resultBookmark.add(bk);
// }}} else {for (Object o : bookmarkList) {HashMap bk = (HashMap) o;
// System.out.println((String) bk.get("Title"));
// if (((String) bk.get("Title")).contains("MP批准页")) {// bk.remove("Named");bk.put("Action", "GoTo");bk.put("Page", posPageNum+" FitH 846");resultBookmark.add(bk);
// }}}}public static void dealNotBookmark(List resultBookmark, String level, int posPageNum) {if (SIX_PAGE.equalsIgnoreCase(level)) {HashMap bk = new HashMap();bk.put("Action", "GoTo");bk.put("Title", "第六部分 部件寿命限制清单");bk.put("Page", posPageNum + " FitH 600");resultBookmark.add(bk);} else {HashMap bk = new HashMap();bk.put("Action", "GoTo");if (APPROVE_PAGE.equals(level)) {bk.put("Title", "002 MP批准页");} else if (DECLARE_PAGE.equals(level)) {bk.put("Title", "003 维修副总裁声明");} else if (CATALOG_PAGE.equals(level)) {bk.put("Title", "004 总目录");} else if (APPROVE_VALID_PAGE.equals(level)) {bk.put("Title", "005 批准有效页清单");} else if (PLUGCHG_PAGE.equals(level)) {bk.put("Title", "008 插换页记录");}bk.put("Page", posPageNum + " FitH 846");resultBookmark.add(bk);}}
更多推荐
java带书签合并拆分pdf样例
发布评论