This small sample shows how to check the compliance of a file with the PDF/A-1b specification.
ValidationResult result = null;
FileDataSource fd = new FileDataSource(args[0]);
PreflightParser parser = new PreflightParser(fd);
try {
/* Parse the PDF file with PreflightParser that inherits from the NonSequentialParser.
* Some additional controls are present to check a set of PDF/A requirements.
* (Stream length consistency, EOL after some Keyword...)
*/
parser.parse();
/* Once the syntax validation is done,
* the parser can provide a PreflightDocument
* (that inherits from PDDocument)
* This document process the end of PDF/A validation.
*/
PreflightDocument document = parser.getPreflightDocument();
document.validate();
// Get validation result
result = document.getResult();
document.close();
} catch (SyntaxValidationException e) {
/* the parse method can throw a SyntaxValidationException
*if the PDF file can't be parsed.
*/ In this case, the exception contains an instance of ValidationResult
result = e.getResult();
}
// display validation result
if (result.isValid()) {
System.out.println("The file " + args[0] + " is a valid PDF/A-1b file");
} else {
System.out.println("The file" + args[0] + " is not valid, error(s) :");
for (ValidationError error : result.getErrorsList()) {
System.out.println(error.getErrorCode() + " : " + error.getDetails());
}
}
If a validation fails, the ValidationResult object contains all causes of the failure.
In order to help in the failure understanding, all error codes have the following form X[.Y[.Z]] where :
Here after, you can find all Categories (for detailed cause, see constants in the PreglihtConstant interface) :
| Category | Description |
|---|---|
| 1[.y[.z]] | Syntax Error |
| 2[.y[.z]] | Graphic Error |
| 3[.y[.z]] | Font Error |
| 4[.y[.z]] | Transparency Error |
| 5[.y[.z]] | Annotation Error |
| 6[.y[.z]] | Action Error |
| 7[.y[.z]] | Metadata Error |