I am getting the following errors while trying to convert PDF(XFA) to string.
These errors started coming when I switched from PDFBox 1.8.12
to PDFBox 2.0.4
Here is the log
Mar 09, 2017 7:16:07 AM org.apache.pdfbox.pdfparser.BaseParser parseCOSArray
WARNING: Corrupt object reference at offset 779916
Mar 09, 2017 7:16:07 AM org.apache.pdfbox.pdfparser.BaseParser parseCOSArray
WARNING: Corrupt object reference at offset 780049
Mar 09, 2017 7:16:07 AM org.apache.pdfbox.pdfparser.BaseParser parseCOSArray
WARNING: Corrupt object reference at offset 780074
java.io.IOException: Unknown dir object c='>' cInt=62 peek='>' peekInt=62 at offset 780074
at org.apache.pdfbox.pdfparser.BaseParser.parseDirObject(BaseParser.java:951)
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSArray(BaseParser.java:651)
at org.apache.pdfbox.pdfparser.BaseParser.parseDirObject(BaseParser.java:866)
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSDictionaryValue(BaseParser.java:150)
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSDictionaryNameValuePair(BaseParser.java:274)
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSDictionary(BaseParser.java:207)
at org.apache.pdfbox.pdfparser.BaseParser.parseDirObject(BaseParser.java:854)
at org.apache.pdfbox.pdfparser.COSParser.parseFileObject(COSParser.java:772)
at org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(COSParser.java:741)
at org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(COSParser.java:672)
at org.apache.pdfbox.pdfparser.COSParser.parseDictObjects(COSParser.java:632)
at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:217)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:252)
and
java.io.IOException: Wrong type of referenced length object COSObject{7, 0}: COSDictionary
at org.apache.pdfbox.pdfparser.COSParser.getLength(COSParser.java:907)
at org.apache.pdfbox.pdfparser.COSParser.parseCOSStream(COSParser.java:949)
at org.apache.pdfbox.pdfparser.COSParser.parseFileObject(COSParser.java:780)
at org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(COSParser.java:741)
at org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(COSParser.java:672)
at org.apache.pdfbox.pdfparser.COSParser.parseDictObjects(COSParser.java:632)
at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:217)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:252)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:966)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:922)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:870)
I read the migration and used load instead of loadNonSeq, because now PDFBox handles that internally.
Any suggestions on how to fix these errors.
EDIT#2 @TilmanHausherr I checked your theory. I opened the file in Sublime, removed the extra spaces in the starting and saved it. I got the following error
org.apache.pdfbox.filter.FlateFilter decode
SEVERE: FlateFilter: stop reading corrupt stream due to a DataFormatException
java.io.IOException: java.util.zip.DataFormatException: invalid distance too far back
at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:82)
at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:69)
at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:162)
at org.apache.pdfbox.pdfparser.PDFXrefStreamParser.<init>(PDFXrefStreamParser.java:56)
at org.apache.pdfbox.pdfparser.COSParser.parseXrefStream(COSParser.java:2075)
at org.apache.pdfbox.pdfparser.COSParser.parseXrefObjStream(COSParser.java:348)
at org.apache.pdfbox.pdfparser.COSParser.parseXref(COSParser.java:303)
at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:194)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:252)
at utils.PDFManager.PDFToText(PDFManager.java:280)
at processing.charge.CertificateUtils.getCertificateTypeFromFile(CertificateUtils.java:56)
at processing.charge.CertificateUtils.getCertificateType(CertificateUtils.java:48)
at processing.Controller.getDocumentType(Controller.java:110)
at processing.Controller.insertIntoDb(Controller.java:43)
at Test.main(Test.java:203)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.util.zip.DataFormatException: invalid distance too far back
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Inflater.java:259)
at java.util.zip.Inflater.inflate(Inflater.java:280)
at org.apache.pdfbox.filter.FlateFilter.decompress(FlateFilter.java:107)
at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:64)
... 19 more
Mar 09, 2017 11:07:22 PM org.apache.pdfbox.filter.FlateFilter decode
SEVERE: FlateFilter: stop reading corrupt stream due to a DataFormatException
java.io.IOException: java.util.zip.DataFormatException: invalid distance too far back
at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:82)
at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:69)
at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:162)
at org.apache.pdfbox.pdfparser.PDFXrefStreamParser.<init>(PDFXrefStreamParser.java:56)
at org.apache.pdfbox.pdfparser.COSParser.parseXrefStream(COSParser.java:2075)
at org.apache.pdfbox.pdfparser.COSParser.parseXrefObjStream(COSParser.java:348)
at org.apache.pdfbox.pdfparser.COSParser.parseXref(COSParser.java:303)
at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:194)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:252)
at utils.PDFManager.PDFToText(PDFManager.java:280)
at processing.charge.CertificateUtils.getCertificateTypeFromFile(CertificateUtils.java:56)
at processing.charge.CertificateUtils.getCertificateType(CertificateUtils.java:49)
at processing.Controller.getDocumentType(Controller.java:110)
at processing.Controller.insertIntoDb(Controller.java:43)
at Test.main(Test.java:203)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.util.zip.DataFormatException: invalid distance too far back
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Inflater.java:259)
at java.util.zip.Inflater.inflate(Inflater.java:280)
at org.apache.pdfbox.filter.FlateFilter.decompress(FlateFilter.java:107)
at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:64)
Also to verify your theory, I opened another file(that was working correctly) in Sublime, and it had the same spaces, tabs and CRs.
As discussed in the comments, the files have blanks (CRs and TABs) before the PDF header starts. You can remove them with NOTEPAD++ (or with any editor that can edit binary files), or (if all your files have that flaw) by writing a short code that opens an input stream, swallow bytes until you hit "%" and then copy all the rest from there to an output stream.
I've also opened issue PDFBOX-3714.
Update: This has been fixed in 2.0.5, now available.