Home > Mobile >  Getting specification "Unknown" version "0.0" Exception for a simple pdf file
Getting specification "Unknown" version "0.0" Exception for a simple pdf file

Time:07-07

I'm learning to work with itext7 library in android but I'm facing an issue with it when using some specific pdf files.

I'm running this code:-

val pdfReader = PdfReader(readerFile)
val pdfWriter = PdfWriter(writerFile)

val pdfDoc = PdfDocument(pdfReader, pdfWriter)

On the following files:-

  1. https://drive.google.com/file/d/19t3XlxRCU_h0_eLtkx74V5BQpaAxXvUh/view?usp=sharing
  2. https://drive.google.com/file/d/1kHfU9TwShVWEzUiBOwN8QELDXMWEmLEa/view?usp=sharing
  3. https://drive.google.com/file/d/1KLRD0kLOxk0xc6HMNphmbZ1exEGUdVTk/view?usp=sharing

This gives me the following exception:-

java.lang.UnsupportedOperationException: This parser does not support specification "Unknown" version "0.0"

Stacktrace:-

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.compresspdf, PID: 9272
    java.lang.UnsupportedOperationException: This parser does not support specification "Unknown" version "0.0"
        at javax.xml.parsers.DocumentBuilderFactory.setXIncludeAware(DocumentBuilderFactory.java:475)
        at com.itextpdf.kernel.utils.DefaultSafeXmlParserFactory.configureSafeDocumentBuilderFactory(DefaultSafeXmlParserFactory.java:177)
        at com.itextpdf.kernel.utils.DefaultSafeXmlParserFactory.createDocumentBuilderInstance(DefaultSafeXmlParserFactory.java:118)
        at com.itextpdf.kernel.utils.XmlProcessorCreator.createSafeDocumentBuilder(XmlProcessorCreator.java:96)
        at com.itextpdf.kernel.xmp.impl.XMPMetaParser.parseInputSource(XMPMetaParser.java:284)
        at com.itextpdf.kernel.xmp.impl.XMPMetaParser.parseXmlFromBytebuffer(XMPMetaParser.java:204)
        at com.itextpdf.kernel.xmp.impl.XMPMetaParser.parseXml(XMPMetaParser.java:147)
        at com.itextpdf.kernel.xmp.impl.XMPMetaParser.parse(XMPMetaParser.java:92)
        at com.itextpdf.kernel.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:169)
        at com.itextpdf.kernel.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:155)
        at com.itextpdf.kernel.pdf.XmpMetaInfoConverter.appendMetadataToInfo(XmpMetaInfoConverter.java:62)
        at com.itextpdf.kernel.pdf.PdfDocument.getDocumentInfo(PdfDocument.java:677)
        at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:2033)
        at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:318)
        at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:286)
        at com.example.compresspdf.PdfKt.editDocument(pdf.kt:46)

This stacktrace does indicate that the error is thrown from the "javax.xml.parsers.DocumentBuilderFactory" used inside "kernel-7.2.3.jar\com\itextpdf\kernel\utils\DefaultSafeXmlParserFactory.class" but I don't know how to fix this.

Example android project for the above case:- https://github.com/chaudharydeepanshu/Jetpack_Compose_Pick_Edit_Save_PDF_itext7_Example.git

Note - The above project uses Jetpack Compose

So, is there any way I can fix this exception for these files?

Thank you.

CodePudding user response:

The error you are getting while the XMP bits of the PDF file is getting parsed:

    java.lang.UnsupportedOperationException: This parser does not support specification "Unknown" version "0.0"

is due to the fact that Android uses a different XML validation mechanism (mostly stuff tied with Security) than what iText 7 Core typically expects/uses.

Long story short, and I'll update this answer with some more details, iText ended up creating a separate build that tackles this, and other differences between your typical JDK and what Android uses.

We are looking into incorporating these changes into the main modules, but for now, we've created a separate branch/builds with these changes.

Feel free to use this Android specific repo (gradle instructions):

allprojects {
    repositories {
        maven {
            url "https://repo.itextsupport.com/android"
        }
    }
}

and then append -android to any module you would like to use. For instance, if you'd like to use the kernel module with version 7.2.2:

dependencies {
    implementation 'com.itextpdf.android:kernel-android:7.2.2'
}

if for some reason you cannot use this version, you can always implement your own XML Parser Factory, by setting it with (API docs):

com.itextpdf.kernel.utils.XmlProcessorCreator.setXmlParserFactory(new AwsomeParserFactory()); 

and then implement com.itextpdf.kernel.utils.IXmlParserFactory.

  • Related