2

I had a function written using iText v5 and I'm trying to update it to use iText v7. The function checks if a document claims to be PDF/A (I know iText is not a PDF/A Validatior, I just need to know if it declares to be).

The implementation in v5 is the one from this other question. However, reader.getMetadata() method is no longer available in iText v7.

I've found that the reader in v7 provides a new method that looks perfect for that situation getPdfAConformanceLevel, but it is returning always null. After checking the source code I don't see where pdfAConformanceLevel is initialized, so I wonder, how does this work?

I've tried also reading the DocumentInformation, but without sucess.

My code is:

PdfReader reader = new PdfReader(file);
PdfAConformanceLevel level = reader.getPdfAConformanceLevel();

if (level != null) {
    String conformance = level.getConformance();
    return "A".equalsIgnoreCase(conformance) || "B".equalsIgnoreCase(conformance);
}

return false
Daniel Rodríguez
  • 548
  • 1
  • 10
  • 30

1 Answers1

2

There are at least two ways to get the conformance level.

First way is explicit - you do all the work manually and responsible for exception handling.

// Open the document
PdfDocument pdfDocument = new PdfDocument(new PdfReader(filePath));

// Parse conformance level from metadata explicitly
byte[] existingXmpMetadata = pdfDocument.getXmpMetadata();
XMPMeta meta = XMPMetaFactory.parseFromBuffer(existingXmpMetadata);
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.getConformanceLevel(meta);

Second way is indeed via PdfReader. You have identified this way correctly. The conformanceLevel field is set when the document is opened, so creating PdfReader only is not enough, you need to open the document:

PdfDocument pdfDocument = new PdfDocument(new PdfReader(filePath));
PdfAConformanceLevel conformanceLevel = pdfDocument.getReader().getPdfAConformanceLevel();
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60