使用pdfBox合并pdf解决两种情况:
2016-12-15 10:56
399 查看
1 两个pdf合并成一个pdf
public static void main(String[] args) throws IOException,
DocumentException {
PdfReader reader1 = new PdfReader(“H:/abcd/PDF1.pdf”);
PdfReader reader2 = new PdfReader(“H:/abcd/PDF2.pdf”);
2 两个PDF各自其中的一页合并为一个PDF中的一页(合成两页PDF的内容不超过一页)
public static final String file1 = “H:/abcd/PDF1.pdf”;
public static final String file2 = “H:/abcd/PDF2.pdf”;
public static void main(String[] args) throws IOException,
DocumentException {
PdfReader reader1 = new PdfReader(“H:/abcd/PDF1.pdf”);
PdfReader reader2 = new PdfReader(“H:/abcd/PDF2.pdf”);
FileOutputStream out = new FileOutputStream("H:/abcd/PDF4.pdf"); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); PdfContentByte cb = writer.getDirectContent(); int totalPages = 0; totalPages += reader1.getNumberOfPages(); totalPages += reader2.getNumberOfPages(); List<PdfReader> readers = new ArrayList<PdfReader>(); readers.add(reader1); readers.add(reader2); int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } out.flush(); document.close(); out.close(); }
2 两个PDF各自其中的一页合并为一个PDF中的一页(合成两页PDF的内容不超过一页)
public static final String file1 = “H:/abcd/PDF1.pdf”;
public static final String file2 = “H:/abcd/PDF2.pdf”;
public static void main(String[] args) throws IOException { combine2(); } public static void combine2() { try { PDDocument _document1 = PDDocument.load(new File(file1)); PDPage _page1 = _document1.getPage(0); PDRectangle _cropBox1 = _page1.getCropBox(); float originalHeight = _cropBox1.getHeight(); _cropBox1.setLowerLeftY(-(PDRectangle.A4.getHeight() - _cropBox1 .getHeight())); _page1.setMediaBox(_cropBox1); _page1.setCropBox(_cropBox1); ByteArrayOutputStream _baos1 = new ByteArrayOutputStream(); _document1.save(_baos1); _document1.close(); PDDocument _document2 = PDDocument.load(new File(file2)); PDPage _page2 = _document2.getPage(0); PDRectangle _cropBox2 = _page2.getCropBox(); _cropBox2.setLowerLeftY(-(PDRectangle.A4.getHeight() - _cropBox2.getHeight() + originalHeight + 20)); _page2.setMediaBox(_cropBox2); _page2.setCropBox(_cropBox2); ByteArrayOutputStream _baos2 = new ByteArrayOutputStream(); _document2.save(_baos2); _document2.close(); PDFMergerUtility mergePdf = new PDFMergerUtility(); mergePdf.addSource(new ByteArrayInputStream(_baos1.toByteArray())); mergePdf.addSource(new ByteArrayInputStream(_baos2.toByteArray())); Overlay overLay = new Overlay(); overLay.setInputPDF(PDDocument.load(_baos1.toByteArray())); overLay.setDefaultOverlayPDF(PDDocument.load(_baos2.toByteArray())); Map<Integer, String> specificPageOverlayFile = new HashMap<Integer, String>(); PDDocument _doc = overLay.overlay(specificPageOverlayFile); PDPageXYZDestination dest2 = new PDPageXYZDestination(); dest2.setPage(_doc.getPage(0)); dest2.setZoom(1f); dest2.setTop(new Float(PDRectangle.A4.getHeight()).intValue()); PDActionGoTo action2 = new PDActionGoTo(); action2.setDestination(dest2); _doc.getDocumentCatalog().setOpenAction(action2); _doc.save("H:/abcd/PDF10.pdf"); } catch (Exception e) { e.printStackTrace(); } }
相关文章推荐
- 2016/3/31 ①全选时 下面选项全选中 ② 下面不选中时 全选取消 ③在“” 中 转义字符的使用\ onclick=\"Checkpa(this,'flall')\"; ④区别于分别实现 重点在于两种情况合并实现
- 在wpf中使用Image时,时常会出现图像模糊的情况,有两种方法可以解决这一问题:(转)
- 使用pdfbox实现pdf文本提取和合并功能示例
- C#调用exe解决PDF转图片问题(使用PDFBox方案,java语言编译jar包实现)
- 使用pdfbox实现pdf文本提取和合并功能示例
- 使用强大的工具来解决前后台的协调配合,做到互不干扰对方的开发工作,只需要前后台都完工的情况合并代码就好了
- 解决使用spread时快捷键无反应的情况
- 7.1 使用PDFBox处理PDF文档
- 使用C++模板和不使用C++模板两种情况中的执行代码大小近似估计
- 使用C++模板和不使用C++模板两种情况中的执行代码大小近似估计
- 使用C++模板和不使用C++模板两种情况中的执行代码大小近似估计
- 使用C++模板和不使用C++模板两种情况中的执行代码大小近似估计
- 使用C++模板和不使用C++模板两种情况中的执行代码大小近似估计
- 使用PDFBox解析PDF内容
- 使用C++模板和不使用C++模板两种情况中的执行代码大小近似估计
- 使用C++模板和不使用C++模板两种情况中的执行代码大小近似估计
- 解决dephi使用Word时出现“没有注册接口”的情况。
- Dell optiplex 740 电脑USB键盘无法正常使用的两种原因及解决
- 水晶报表使用经验谈1--建立水晶报表第一步及编译最易出现错误的解决方法及报表转换成pdf文档进行打印方法
- 使用Microsoft Lookback网卡解决了断网情况下 Virtual Server 虚机和主机的网络连接