当前位置: 首页 > news >正文

完整教程:PDFBox - PDDocument 与 byte 数组、PDF 加密

一、PDDocument 与 byte 数组

1、由 byte 数组创建 PDDocument
(1)基本介绍
  • 调用 PDDocument 的 load 静态方法,由 byte 数组创建 PDDocument
public static PDDocument load(byte[] input) throws IOException {
return load(input, "");
}
(2)演示
File pdfFile = new File("pdf/image_example.pdf");
InputStream pdfInputStream;
try {
pdfInputStream = new FileInputStream(pdfFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("PDF 文件不存在");
return;
}
ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
try {
while ((nRead = pdfInputStream.read(data, 0, data.length)) != -1) {
pdfBuffer.write(data, 0, nRead);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("PDF 读取文件失败");
return;
}
byte[] pdfBytes = pdfBuffer.toByteArray();
try (PDDocument document = PDDocument.load(pdfBytes)) {
PDPage page = new PDPage();
document.addPage(page);
File imgFile = new File("pdf/dzs.jpeg");
InputStream imgInputStream;
try {
imgInputStream = new FileInputStream(imgFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("图片不存在");
return;
}
ByteArrayOutputStream imgBuffer = new ByteArrayOutputStream();
int nReadImg;
byte[] dataImg = new byte[1024];
try {
while ((nReadImg = imgInputStream.read(dataImg, 0, dataImg.length)) != -1) {
imgBuffer.write(dataImg, 0, nReadImg);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("图片读取失败");
return;
}
byte[] imgBytes = imgBuffer.toByteArray();
PDImageXObject pdImage = PDImageXObject.createFromByteArray(document, imgBytes, "dzs.jpeg");
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.drawImage(pdImage, 100, 600, pdImage.getWidth() * 0.25f, pdImage.getHeight() * 0.25f);
} catch (IOException e) {
e.printStackTrace();
}
document.save("pdf/image_example.pdf");
} catch (IOException e) {
e.printStackTrace();
}
2、由 PDDocument 得到 byte 数组
(1)基本介绍
public void save(OutputStream output) throws IOException {
if (this.document.isClosed()) {
throw new IOException("Cannot save a document which has been closed");
} else {
Iterator var2 = this.fontsToSubset.iterator();
while(var2.hasNext()) {
PDFont font = (PDFont)var2.next();
font.subset();
}
this.fontsToSubset.clear();
COSWriter writer = new COSWriter(output);
try {
writer.write(this);
} finally {
writer.close();
}
}
}
(2)演示
File pdfFile = new File("pdf/image_example.pdf");
InputStream pdfInputStream;
try {
pdfInputStream = new FileInputStream(pdfFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("PDF 文件不存在");
return;
}
ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
try {
while ((nRead = pdfInputStream.read(data, 0, data.length)) != -1) {
pdfBuffer.write(data, 0, nRead);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("PDF 读取文件失败");
return;
}
byte[] pdfBytes = pdfBuffer.toByteArray();
try (PDDocument document = PDDocument.load(pdfBytes)) {
PDPage page = new PDPage();
document.addPage(page);
File imgFile = new File("pdf/dzs.jpeg");
InputStream imgInputStream;
try {
imgInputStream = new FileInputStream(imgFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("图片不存在");
return;
}
ByteArrayOutputStream imgBuffer = new ByteArrayOutputStream();
int nReadImg;
byte[] dataImg = new byte[1024];
try {
while ((nReadImg = imgInputStream.read(dataImg, 0, dataImg.length)) != -1) {
imgBuffer.write(dataImg, 0, nReadImg);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("图片读取失败");
return;
}
byte[] imgBytes = imgBuffer.toByteArray();
PDImageXObject pdImage = PDImageXObject.createFromByteArray(document, imgBytes, "dzs.jpeg");
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.drawImage(pdImage, 100, 600, pdImage.getWidth() * 0.25f, pdImage.getHeight() * 0.25f);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
document.save(output);
byte[] bytes = output.toByteArray();
System.out.println("PDF 字节数:" + bytes.length);
} catch (IOException e) {
e.printStackTrace();
}
# 输出结果
PDF 字节数:86736

二、PDF 加密

1、基本介绍
(1)用户密码
  1. 用户密码(User Password / Open Password)用于打开 PDF 文件,也叫打开密码或文档密码

  2. 如果设置了用户密码,任何人在打开该 PDF 时都必须输入这个密码,否则无法查看内容

(2)所有者密码
  1. 所有者密码(Owner Password / Permissions Password)用于解除权限限制

  2. 所有者密码用于禁止某些操作,例如,打印、复制、编辑

  3. 只有拥有所有者密码的用户,才能移除这些限制,普通用户即使能打开 PDF,也无法绕过这些限制

2、创建加密的 PDF
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello PDFBox");
contentStream.endText();
} catch (IOException e) {
e.printStackTrace();
}
// 设置权限
AccessPermission accessPermission = new AccessPermission();
accessPermission.setReadOnly();
// 设置保护策略
StandardProtectionPolicy policy = new StandardProtectionPolicy("12345", "12345", accessPermission);
document.protect(policy);
document.save("pdf/test.pdf");
} catch (IOException e) {
e.printStackTrace();
}
3、读取加密的 PDF
  1. 读取时未设置密码或设置了错误的密码
try (PDDocument document = PDDocument.load(new File("pdf/test.pdf"), "abc")) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
}
# 输出结果
org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException: Cannot decrypt PDF, the password is incorrect
  1. 读取时设置了正确的密码
try (PDDocument document = PDDocument.load(new File("pdf/test.pdf"), "12345")) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
}
# 输出结果
Hello PDFBox
http://www.jsqmd.com/news/40537/

相关文章:

  • Dark Side of the Moon
  • flask:自定义异常
  • 图片合集
  • OpenWrt路由的端口映射问题
  • 算法沉淀第七天(AtCoder Beginner Contest 428 和 小训练赛) - 详解
  • How-to-extract-text-from-PDF-Image-files-OCR-CarlZeng
  • Web应用模糊测试完全指南
  • 升鲜宝供应链管理系统、各端的访问地址及nginx 真实的配置方法
  • uiautomator2元素查看器WEditor的安装和启动
  • WEditor的使用方法
  • 【题解】LOJ6300. 「CodePlus 2018 3 月赛」博弈论与概率统计
  • 感情粉末沿着试管边缘 在祝福中逐渐分解 加热认知离子重新排列 于底部悲伤沉淀
  • C#循序渐进 - 详解
  • 2025.11.14 - A
  • 从RvmTranslator到PlantAssistant
  • MI50 在ubuntu 下 风扇控制实现
  • PortSwigger靶场之 CSRF where token is not tied to user session通关秘籍 - 实践
  • nvm不能下载安装低版本node解决办法
  • flask: 抛出异常
  • 20251114——读后感5
  • 雪地奔驰全等级提升所需经验一览
  • 2025皮肤亚健康管理品牌最新专业推荐:科技赋能健康美新生态
  • 【HT-086-Div.2】嗡嗡蜜蜂
  • 第四十一篇
  • 深入解析:Vue3 路由配置和使用与讲解(超级详细)
  • 好题集 (0) - 目录
  • 251114
  • HubSpot如何规模化推进AI编码助手应用
  • 好题集 (4) - CF487E Tourists
  • 完整教程:OpenHarmony内核基础:LiteOS-M内核与POSIX/CMSIS接口