最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java实现PDF文件的分割与加密功能代码示例
时间:2022-06-29 01:49:18 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Java实现PDF文件的分割与加密功能代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
加密文件
/**
* 加密文件测试
* @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();
}
}
切割文件
/**
* 切割文件测试
* @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();
}
}
生成封面图
/**
* 切割文件测试
* @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();
}
}
相关文章
- 豚豚剧App官方入口下载:海量影视资源一键解锁 02-04
- 漫蛙漫画网APP最新版本下载-漫蛙正版漫画资源免费安装入口 02-04
- picacg2.1.2.3.4最新版-picacg安装包高速下载 02-04
- 百度网盘网页版入口-百度网盘网页端登录平台 02-04
- 漫蛙漫画网页最新下载入口-漫蛙漫画APP安卓苹果安装包下载 02-04
- 腾讯视频24小时人工客服热线详情-腾讯视频官方人工客服地址 02-04