Chinaunix首页 | 论坛 | 博客
  • 博客访问: 546289
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-02-22 13:18:39


点击(此处)折叠或打开


  1. 指定页码插入/替换
  2. pdfbox好像没有专门提供这个方法,但是现有的方法多重组合起来也能实现这个功能,

  3. 需求:一个pdf文件A有10页,现在想在第6页插入一页新的pdf文件B,插入完成后整个pdf文件A变成11页。

  4. 思路1(插入):
  5.   先将这个10的pdf拆分成10个1页的pdf,按顺序放好,文件名分别是:1.pdf、2.pdf....10.pdf。再拆分到第6页的时候将文件B放进来,重命名问6.pdf,原本pdf文件A里面的第6页重命名为7.pdf,依次后推,最后的得到的1.pdf----->11.pdf一共11个文件

  6.   然后使合并功能将这个11个pdf按顺序合并。

  7. 思路2(替换):
  8.   在插入的基础上,拆分的时候将pdf文件A里面的第6个页丢弃,使用新的页面来代替它命名6.pdf,然后合并就完事了。

  9. 1.pom

  10. <!--pdfbox-->
  11. <dependency>
  12.     <groupId>org.apache.pdfbox</groupId>
  13.     <artifactId>pdfbox-tools</artifactId>
  14.     <version>2.0.25</version>
  15. </dependency>
  16. <dependency>
  17.     <groupId>net.sf.cssbox</groupId>
  18.     <artifactId>pdf2dom</artifactId>
  19.     <version>2.0.1</version>
  20. </dependency>

  21. <!--poi-->
  22. <dependency>
  23.     <groupId>com.itextpdf</groupId>
  24.     <artifactId>itextpdf</artifactId>
  25.     <version>5.5.10</version>
  26. </dependency>
  27. <dependency>
  28.     <groupId>com.itextpdf.tool</groupId>
  29.     <artifactId>xmlworker</artifactId>
  30.     <version>5.5.10</version>
  31. </dependency>
  32. <dependency>
  33.     <groupId>org.apache.poi</groupId>
  34.     <artifactId>poi-ooxml</artifactId>
  35.     <version>3.15</version>
  36. </dependency>
  37. <dependency>
  38.     <groupId>org.apache.poi</groupId>
  39.     <artifactId>poi-scratchpad</artifactId>
  40.     <version>3.15</version>
  41. </dependency>


  42. 2.实现方法

  43. /**from fhadmin.cn
  44.  * 指定页码插入页
  45.  * @param filename1 源pdf路径
  46.  * @param filename2 需要插入的pdf路径
  47.  * @param number 插入的页码
  48.  * @param newfilename 全新pdf的路径
  49.  * @throws Exception
  50.  */
  51. public void insertPage(String filename1,String filename2,int number,String newfilename,String tempPath) throws Exception {
  52.     PDDocument pdf1 = PDDocument.load(new File(filename1));
  53.     PDDocument pdf2 = PDDocument.load(new File(filename2));

  54.     //1、将第一个pdf按页码全部拆开
  55.     Splitter splitter = new Splitter();
  56.     List<PDDocument> Pages = splitter.split(pdf1);

  57.     Iterator<PDDocument> iterator = Pages.listIterator();

  58.     PDFMergerUtility PDFmerger = new PDFMergerUtility();

  59.     int i = 1;
  60.     while(iterator.hasNext()) {
  61.         if(i==number){
  62.             System.out.println("当前插入页码:"+number);
  63.             pdf2.save(tempPath+"/"+ i +".pdf");
  64.             i++;
  65.         }
  66.         PDDocument pd = iterator.next();
  67.         String tempFile = tempPath+"/"+ i +".pdf";
  68.         System.out.println("开始拆分:"+tempFile);
  69.         pd.save(tempFile);
  70.         i++;
  71.     }

  72.     //2、开始重组
  73.     PDFmerger.setDestinationFileName(newfilename);

  74.     //上面的i最后多加了一次,这里不取等
  75.     for(int j=1;j<i;j++){
  76.         String tempFile = tempPath+"/"+ j +".pdf";
  77.         System.out.println("开始合并:"+tempFile);
  78.         PDFmerger.addSource(tempFile);
  79.     }

  80.     //合并文档
  81.     PDFmerger.mergeDocuments();
  82.     System.out.println("文档合并完成");

  83.     pdf1.close();
  84.     pdf2.close();
  85. }


  86. 3.测试
  87. //from fhadmin.cn
  88. @Test
  89. void insertPage() throws Exception {
  90.     PdfUtils pdfUtils = new PdfUtils();
  91.     String filename1 = "F:\\Users\\admin\\Desktop\\A.pdf";
  92.     String filename2 = "F:\\Users\\admin\\Desktop\\B.pdf";
  93.     String newfilename = "F:\\Users\\admin\\Desktop\\newA.pdf";
  94.     String tempPath = "F:\\Users\\admin\\Desktop\\temp";
  95.     int insertNum = 32;

  96.     pdfUtils.insertPage(filename1,filename2,insertNum,newfilename,tempPath);
  97. }


  98.  啰嗦几句
  99. 1、我将要修改的页面先拆分出来了,比如这里的第6页,然后(我这个整页都是图片)将内容修改后,合并进来发现尺码不对,是的,你没有听错就是尺码不对,当我修改后的pdf在放进来合并的时候,这一页它变小了~,原来是我在将图片另存为pdf,或者使用打印另存为pdf的时候,纸张大小就那么几类(A4/A3等),那我就不干了啊,丑里吧唧的。

  100. 2、这个时候就用pdfbox的图片插入功能:将图片写入原来的6.pdf这一页里面来,你要问我为啥?因为原来的6.pdf尺码是对的,其中画图的时候开始位置x,y都从0开始。


阅读(698) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~