I am getting errors while attempting to fill out many fields in multiple PDFs. The code is actually creating and filling the PDFs as expected, and saving the PDFs, however the code is throwing errors. I am trying to understand what is causing these errors.
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
This is the code I use:
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();
}
2
Appreciate all the feedback. I was tired when I originally wrote this and was making fundamental file io no nos. This is what works:
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();
1