我尝试从一个文件中提取XFA,在我将PDFBox从1.8.12更新到2.0.4之前,它对我很好。
我有一个文件,可以从1.8.12中提取XFA,但不能使用2.0.4。
当我使用PDFBox使用2.0.4提取它时,我得到了XFA的结构,但是几乎所有的值都丢失了。另一方面,当我试图使用1.8.12提取相同的表单时,结果很好。
我在所以上研究了一个类似的问题。据说是在2.0.4中修正的,但我仍然面临一些问题。
有什么想法吗?
我已经把文件包括了
EDIT#1
为2.0.4
// returns PDXFA
public static byte[] getParsableXFAForm(File file) {
if (file == null)
return null;
PDDocument doc;
PDDocumentCatalog catalog;
PDAcroForm acroForm;
PDXFAResource xfa;
try {
// String pass = null;
doc = PDDocument.load(file);
if (doc == null)
return null;
// flattenPDF(doc);
doc.setAllSecurityToBeRemoved(true);
// System.out.println("Security " + doc.isAllSecurityToBeRemoved());
catalog = doc.getDocumentCatalog();
if (catalog == null) {
doc.close();
return null;
}
acroForm = catalog.getAcroForm();
if (acroForm == null) {
doc.close();
return null;
}
xfa = acroForm.getXFA();
if (xfa == null) {
doc.close();
return null;
}
// TODO return byte[]
byte[] xfaBytes = xfa.getBytes();
doc.close();
return xfaBytes;
} catch (IOException e) {
// handle IOException
// happens when the file is corrupt.
e.printStackTrace();
System.out.println("XFAUtils-getParsableXFAForm-IOException");
return null;
}
}= 1.8.12
public static byte[] getParsableXFAForm(File file) {
if (file == null)
return null;
PDDocument doc;
PDDocumentCatalog catalog;
PDAcroForm acroForm;
PDXFA xfa;
try {
doc = PDDocument.loadNonSeq(file, null);
if (doc == null)
return null;
// flattenPDF(doc);
doc.setAllSecurityToBeRemoved(true);
// System.out.println("Security " + doc.isAllSecurityToBeRemoved());
catalog = doc.getDocumentCatalog();
if (catalog == null) {
doc.close();
return null;
}
acroForm = catalog.getAcroForm();
if (acroForm == null) {
doc.close();
return null;
}
xfa = acroForm.getXFA();
if (xfa == null) {
doc.close();
return null;
}
// TODO return byte[]
byte[] xfaBytes = xfa.getBytes();
doc.close();
return xfaBytes;
} catch (IOException e) {
// handle IOException
// happens when the file is corrupt.
// e.printStackTrace();
System.out.println("XFAUtils-getParsableXFAForm-IOException");
return null;
}
}发布于 2017-03-20 22:48:47
乍一看
在您的PDF中有6次修改,在此过程中,XFA表单已被越来越多地填写。1.8.12代码提取XFA表单的最新版本,而2.0.4代码提取XFA表单的最老版本。
我使用PDFBox版本2.0.4、2.0.5和当前开发快照2.1.0-快照运行了2.0.4代码。在2.0.4版中,我确实可以再现XFA表单最古老的修订版已经加载,但是使用2.0.5或2.1.0-快照加载了当前的修订版。
这似乎是在2.0.5中修正的PDFBox 2.0.0.2.0.4中的一个缺点。
关于近距离检查
由于PDFBox 2.0.4中的一个bug从文件的错误版本中读取XFA表单似乎不太可能,我进一步研究了这一点。
特别是,我仔细查看了PDF文件本身。事实上,这个文件在实际PDF文件头之前有10个垃圾字节!
这些额外的垃圾字节使得相对于文件启动的交叉引用和偏移都是错误的。因此,PDFBox不能以常规方式解析文件,而是必须进行某种修复。
看看2.0.4和2.0.5之间的差异,尤其是代码中出现了很大的变化,以修复具有中断的交叉引用和偏移的PDF。虽然PDFBox 2.0.4只能部分修复文件(只找到初始的XFA修订版),因此,PDFBox 2.0.5成功地完成了更完整的修复,尤其是最新的XFA修订版。
修正了OP的PDF (即删除了前面的垃圾字节,cf )。XFA-File-fixed.pdf),我也可以使用PDFBox版本2.0.0.2.0.4成功地提取当前的XFA表单修订版。
因此,这不是我最初假设的PDFBox错误,而是一个PDFBox文件修复功能在PDFBox 2.0.5改进之前无法正确修复的损坏的PDFBox文件。
https://stackoverflow.com/questions/42861499
复制相似问题