我在尝试填写多个 PDF 中的许多字段时遇到错误。该代码实际上是按预期创建和填充 PDF,并保存 PDF,但代码抛出错误。我正在尝试了解导致这些错误的原因。
Caused by: java.util.zip.DataFormatException: invalid stored block lengths
ERROR FlateFilter FlateFilter: stop reading corrupt stream due to a DataFormatException
ERROR COSParser object stream 51 could not be parsed due to an exception
这是我使用的代码:
for (int dCounter = 4; dCounter < nColCount; dCounter++) {
File adobeFile = new File(strDestFileName);
PDDocument adobeDocument = Loader.loadPDF(adobeFile);
PDDocumentCatalog adobeCatalog = adobeDocument.getDocumentCatalog();
PDAcroForm adobeAcroForm = adobeCatalog.getAcroForm();
PDField adobeField = adobeAcroForm.getField(strArrayHeaders[dCounter]);
if(!Objects.equals(strArrayCellValues[dCounter], "")) {
adobeField.setValue(strArrayCellValues[dCounter]);
}
FileOutputStream adobeOut = new FileOutputStream(adobeFile);
adobeDocument.saveIncremental(adobeOut);
adobeDocument.close();
}
感谢所有反馈。当我最初写这篇文章并制作基本文件 io no nos 时,我很累。 这就是有效的方法:
File adobeFile = new File(strSourceFileName);
PDDocument adobeDocument = Loader.loadPDF(adobeFile);
PDDocumentCatalog adobeCatalog = adobeDocument.getDocumentCatalog();
PDAcroForm adobeAcroForm = adobeCatalog.getAcroForm();
for (int dCounter = 4; dCounter < nColCount; dCounter++) {
PDField adobeField = adobeAcroForm.getField(strArrayHeaders[dCounter]);
if(!Objects.equals(strArrayCellValues[dCounter], "")) {
adobeField.setValue(strArrayCellValues[dCounter]);
}
}
File adobeDestFile = new File(strDestFileName);
FileOutputStream adobeOut = new FileOutputStream(adobeDestFile);
adobeDocument.saveIncremental(adobeOut);
adobeDocument.close();
adobeOut.close();