使用pdfBox禁用pdf文本搜索

时间:2018-03-27 07:49:30

标签: java pdfbox

我有一个pdf文档(没有表单),我想使用pdfBox(java)禁用文本搜索。 我可以想象以下可能性:

  • 拼合文字
  • 删除文字信息(不删除文字本身)
  • 向文档添加叠加层。

目前我不知道如何实现这一点。有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

这取决于你的目标:

  • 避免某些文字上的所有内容:打印,用黑色墨水标记,然后重新扫描;

  • 删除敏感文本:您必须扫描文本内部,并删除/替换它(使用pdfbox),但它有风险(某些文本被拆分);

  • 为查看器屏蔽一些文本:查找文本并添加黑色矩形(使用pdfbox),但它不是很安全。您可以删除矩形,或使用其他工具来阅读文本。通常,如果文本在里面,某些工具可以找到它;

  • 避免复制/粘贴文本(但不能搜索/查看):使用安全选项,密码:

请参阅:https://pdfbox.apache.org/2.0/cookbook/encryption.html

PDDocument doc = PDDocument.load(new File("filename.pdf"));

// Define the length of the encryption key.
// Possible values are 40, 128 or 256.
int keyLength = 128;
// 256 => plante

AccessPermission ap = new AccessPermission();

// disable printing, everything else is allowed
ap.setCanPrint(false);

ap.setCanExtractContent(false);
ap.setCanExtractForAccessibility(false);

// Owner password (to open the file with all permissions) is "12345"
// User password (to open the file but with restricted permissions, is empty here)
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", ap);
spp.setEncryptionKeyLength(keyLength);
spp.setPermissions(ap);
doc.protect(spp);

doc.save("filename-encrypted2.pdf");
doc.close();

答案 1 :(得分:0)

非常感谢你的帮助。我想我找到了一种符合要求的方法。 (老实说,不是很干净):

  1. 将矩形添加到地址部分
  2. 将PDF转换为图片
  3. 将图片转换回pdf。
  4. 在丢失所有文本信息的同时,用户无法再查看关键信息。由于这个原因,这只是为了显示(初始PDF文档没有改变),现在可以了。