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

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-04-19 13:23:34


点击(此处)折叠或打开


  1. 由于``某些不可抗力原因,公司不允许使用itext系列的jar包,因此系统中使用的相关jar得替换成开源的。经比较和尝试考虑使用org.apache.pdfbox来替换,同时修改系统中原有的方法,发现比itext系列稍显简洁一点,记录如下:

  2. 加密文件

  3. /**
  4.      * 加密文件测试
  5.      * @from fhadmin.cn
  6.      */
  7.     @Test
  8.     public void encryptTest(){
  9.         try {
  10.             String filePath = "D:\\test\\像李开复一样思考人生.pdf";
  11.             String password = "1234";
  12.             PDDocument document = PDDocument.load(new File(filePath));
  13.             StandardProtectionPolicy spp = new StandardProtectionPolicy(password, password,new AccessPermission());
  14.             document.protect(spp);
  15.             String newFilePath = "D:\\test\\像李开复一样思考人生2.pdf";
  16.             document.save(newFilePath);
  17.             document.close();
  18.         } catch (IOException e) {
  19.             e.printStackTrace();
  20.         }
  21.     }

  22. 切割文件

  23. /**
  24.      * 切割文件测试
  25.      * @from fhadmin.cn
  26.      */
  27.     @Test
  28.     public void extractTest(){
  29.         try {
  30.             String newFilePath = "D:\\test\\像李开复一样思考人生2.pdf";
  31.             String password = "1234";
  32.             PDDocument document = PDDocument.load(new File(newFilePath), password);//带密码读取
  33.             //从第一页截取到第二页
  34.             PageExtractor pageExtractor = new PageExtractor(document, 1, 2);
  35.             PDDocument extract = pageExtractor.extract();
  36.             extract.save("D:\\test\\像李开复一样思考人生free.pdf");
  37.             extract.close();
  38.             document.close();
  39.             } catch (IOException e) {
  40.                 e.printStackTrace();
  41.             }
  42.     }

  43. 生成封面图

  44. /**
  45.      * 切割文件测试
  46.      * @from fhadmin.cn
  47.      */
  48.     @Test
  49.     public void createCoverPicTest(){
  50.         try {
  51.             String pdfPath = "D:\\test\\像李开复一样思考人生.pdf";
  52.             File file = new File(pdfPath);
  53.             //order目录
  54.             String orderPath = file.getParent();
  55.             //转换后的img目录
  56.             String bookName = file.getName().substring(0,file.getName().lastIndexOf("."));
  57.             String imgPath = orderPath + File.separator +bookName+".png";
  58.             log.debug("pdf封面图生成成功:{}", imgPath);
  59.             PDDocument pdDocument = PDDocument.load(new File(pdfPath));
  60.             PDFRenderer renderer = new PDFRenderer(pdDocument);
  61.             /* 第二位参数越大转换后越清晰,相对转换速度越慢 */
  62.             BufferedImage image = renderer.renderImageWithDPI(0, 150);
  63.             ImageIO.write(image, "png", new File(imgPath));

  64.         } catch (IOException e) {
  65.             e.printStackTrace();
  66.         }
  67.     }

  68. 总结一下,现在的工具都比较丰富了,不需要自己去造轮子,

  69. step-1 去maven仓库检索同类型的包,比较一下热度和使用人数
  70. step-2 下载对应包的source源代码,看一下框架整体结构,里面都有哪些package和类,不知道类是干什么的,可以看一下类上面的注释,一般都是比较简单的英文
  71. step-3 动手写单元测试进行验证。


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