好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现PDF文件的分割与加密功能

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

加密文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/**

      * 加密文件测试

      * @from fhadmin.cn

      */

     @Test

     public void encryptTest(){

         try {

             String filePath = "D:\\test\\像李开复一样思考人生.pdf" ;

             String password = "1234" ;

             PDDocument document = PDDocument.load( new File(filePath));

             StandardProtectionPolicy spp = new StandardProtectionPolicy(password, password, new AccessPermission());

             document.protect(spp);

             String newFilePath = "D:\\test\\像李开复一样思考人生2.pdf" ;

             document.save(newFilePath);

             document.close();

         } catch (IOException e) {

             e.printStackTrace();

         }

     }

切割文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

      * 切割文件测试

      * @from fhadmin.cn

      */

     @Test

     public void extractTest(){

         try {

             String newFilePath = "D:\\test\\像李开复一样思考人生2.pdf" ;

             String password = "1234" ;

             PDDocument document = PDDocument.load( new File(newFilePath), password); //带密码读取

             //从第一页截取到第二页

             PageExtractor pageExtractor = new PageExtractor(document, 1 , 2 );

             PDDocument extract = pageExtractor.extract();

             extract.save( "D:\\test\\像李开复一样思考人生free.pdf" );

             extract.close();

             document.close();

             } catch (IOException e) {

                 e.printStackTrace();

             }

     }

生成封面图

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

/**

      * 切割文件测试

      * @from fhadmin.cn

      */

     @Test

     public void createCoverPicTest(){

         try {

             String pdfPath = "D:\\test\\像李开复一样思考人生.pdf" ;

             File file = new File(pdfPath);

             //order目录

             String orderPath = file.getParent();

             //转换后的img目录

             String bookName = file.getName().substring( 0 ,file.getName().lastIndexOf( "." ));

             String imgPath = orderPath + File.separator +bookName+ ".png" ;

             log.debug( "pdf封面图生成成功:{}" , imgPath);

             PDDocument pdDocument = PDDocument.load( new File(pdfPath));

             PDFRenderer renderer = new PDFRenderer(pdDocument);

             /* 第二位参数越大转换后越清晰,相对转换速度越慢 */

             BufferedImage image = renderer.renderImageWithDPI( 0 , 150 );

             ImageIO.write(image, "png" , new File(imgPath));

 

         } catch (IOException e) {

             e.printStackTrace();

         }

     }

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

step-1 去maven仓库检索同类型的包,比较一下热度和使用人数

step-2 下载对应包的source源代码,看一下框架整体结构,里面都有哪些package和类,不知道类是干什么的,可以看一下类上面的注释,一般都是比较简单的英文

step-3 动手写单元测试进行验证。

到此这篇关于Java实现PDF文件的分割与加密功能的文章就介绍到这了,更多相关Java 文件分割 加密内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/7088180757239169037

查看更多关于Java实现PDF文件的分割与加密功能的详细内容...

  阅读:23次