gpt4 book ai didi

java - apache pdfbox - 如何测试文档是否被展平?

转载 作者:行者123 更新时间:2023-12-02 09:05:11 24 4
gpt4 key购买 nike

我写了下面的小Java main方法。它接受一个(出于测试目的而硬编码!)PDF 文档,我知道该文档包含表单中的 Activity 元素,需要将其展平。

public static void main(String [] args) {

try {
// for testing
Tika tika = new Tika();
String filePath = "<path-to>/<pdf-document-with-active-elements>.pdf";
String fileName = filePath.substring(0, filePath.length() -4);
File file = new File(filePath);
if (tika.detect(file).equalsIgnoreCase("application/pdf")) {
PDDocument pdDocument = PDDocument.load(file);
PDAcroForm pdAcroForm = pdDocument.getDocumentCatalog().getAcroForm();
if (pdAcroForm != null) {
pdAcroForm.flatten();
pdAcroForm.refreshAppearances();

pdDocument.save(fileName + "-flattened.pdf");
}
pdDocument.close();
}
}
catch (Exception e) {
System.err.println("Exception: " + e.getLocalizedMessage());
}
}

什么样的测试会断言 File(<path-to>/<pdf-document-with-active-elements>-flattened.pdf)事实上,由这段代码生成的代码会是平坦的吗?

最佳答案

What kind of test would assert that the file generated by this code would, in fact, be flat?

重新加载该文档并检查其 PDAcroForm 中是否有任何表单字段(如果有 PDAcroForm)。

如果您想彻底了解,还可以遍历页面并确保不再有与它们关联的 Widget 注释。

为了真正彻底,在拼合之前另外确定字段位置和内容,并将这些位置的文本提取应用到拼合的 pdf 中。这证实了表单不仅被丢弃而且确实被压平了。

关于java - apache pdfbox - 如何测试文档是否被展平?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59875133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com