Validate PDF document
Validate a PDF document for conformance with a PDF version, or a PDF/A standard and level. When standard violations are encountered, you get detailed information about the type and location of the violation.
Steps to validate a document:
- Reading the input document
- Creating a Conformance object
- Creating a Validator object
- Running the Validate process
- Full example
You need to initialize the library.
Reading the input document
First you need to read the PDF document you want to validate. To do this, load the input document from the file system into a read-only input Document
.
This Document
can then later be passed to the Converter
.
- .NET
- Java
// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
com.pdftools.pdf.Document inDoc = com.pdftools.pdf.Document.open(inStr);
Creating a Conformance object
The document validation process begins with the creation of a Conformance
object.
The Conformance
object defines the PDF version, or the PDF/A standard and level, against which the document is validated.
The checks performed are determined by the PDF version or PDF/A standard and conformance level.
For more information on the checks performed by the Pdftools SDK, see Validation checks.
This example uses the PDF/A-2 standard (2
), with conformance level B (Conformance.PdfALevel.B
).
- .NET
- Java
// Create a conformance object that specifies the PDF/A standard and level against which the document is validated.
var conformance = new Conformance(2, Conformance.PdfALevel.B);
// Create a conformance object that specifies the PDF/A standard and level against which the document is validated.
Conformance conformance = new Conformance(new Conformance.PdfAVersion(2, Conformance.PdfAVersion.Level.B));
Creating a Validator object
The Validator
object validates the standards conformance of the input PDF document against the PDF version, or the PDF/A standard and level, defined in the Conformance
object.
When the Validator finds standard violations, it emits Error
events that provide detailed information about the type of error and where the error occurred in the PDF document.
A handler function can listen to the Error
event and execute business logic based on the information contained in it.
- .NET
- Java
// Create a Validator object and attach an Error event handler.
// In this example we simply write the validation data to the console, but more complex business logic may be implemented based on the category, location, or other attributes of the validation error.
var validator = new Validator();
validator.Error += (s, e) => Console.WriteLine("- {0}: {1} ({2}{3})", e.Category, e.Message, e.Context, e.PageNo > 0 ? " " + e.PageNo : "");
// Create a Validator object and attach an Error event handler.
// In this example, the validation data is written to the console, but more complex business logic may be implemented based on the category, location, or other attributes of the validation error.
Validator validator = new Validator();
validator.addErrorListener(
(Validator.Error error) ->
System.out.format("- %s: %s (%s%s)%n", error.getCategory(), error.getMessage(), error.getContext(), error.getPageNo() > 0 ? String.format(" on page %d", error.getPageNo()) : "")
);
Running the Validate process
After creating the Conformance
and Validator
objects, the final step is to call the Validate
method.
The output of this method is a ValidationResult
which provides information about the conformance of the input document to the PDF version, or the PDF/A standard and level, that is defined in the Conformance
object.
If a Conformance
object is not passed in the ValidatorOptions
, the Validate
process instead validates the conformance of the input document against the document's claimed conformance.
The claimed conformance is the level of conformance that is stored internally within the input document.
The input document must be a valid PDF file format. Otherwise, the validation process fails.
- .NET
- Java
// Validate the PDF/A standard and level of the document against the defined Conformance level.
var options = new ValidationOptions() { Conformance = conformance };
var result = validator.Validate(inDoc, options);
// Write the result of the Validate method to the console.
Console.WriteLine("Document conforms to " + result.Conformance.ToString() + ": " + result.IsConforming);
// Validate the PDF/A standard and level of the document against the defined Conformance level.
ValidationOptions options = new ValidationOptions();
options.setConformance(conformance);
ValidationResult result = validator.validate(inDoc, options);
// Write the result of the Validate method to the console.
System.out.println("Document conforms to " + result.getConformance().toString() + ": " + result.getIsConforming());
Here's an example of the output that is generated by this sample code:
- Metadata: The key Metadata is required but missing. (catalog)
- Color: A device-specific color space (DeviceRGB) without an appropriate output intent is used. (content of page 1)
Document conforms to PDF/A-2b: False
Full example
- .NET
- Java
// Open the PDF document to validate
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Create a conformance object that specifies the PDF/A standard and conformance level against which the document is validated.
// Here the PDF/A-2 standard with conformance Level B is used.
var conformance = new Conformance(2, Conformance.PdfALevel.B);
// Create a Validator object and attach an Error event handler that simply writes the validation error messages to the console.
var validator = new Validator();
validator.Error += (s, e) => Console.WriteLine("- {0}: {1} ({2}{3})", e.Category, e.Message, e.Context, e.PageNo > 0 ? " " + e.PageNo : "");
// Validate the PDF/A Standard and Level of the document against the defined Conformance level.
var options = new ValidationOptions() { Conformance = conformance };
var result = validator.Validate(inDoc, options);
// Write the result of the Validate method to the console.
Console.WriteLine("Document conforms to " + result.Conformance.ToString() + ": " + result.IsConforming);
// Open the PDF document to validate
try (FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
com.pdftools.pdf.Document inDoc = com.pdftools.pdf.Document.open(inStr))
{
// Create a conformance object that specifies the PDF/A standard and level against which the document is validated.
// Here we choose the PDF/A-2 Standard, with conformance Level B.
Conformance conformance = new Conformance(new Conformance.PdfAVersion(2, Conformance.PdfAVersion.Level.B));
// Create a Validator object and attach an Error event handler that simply writes the validation error messages to the console.
Validator validator = new Validator();
validator.addErrorListener(
(Validator.Error error) ->
System.out.format("- %s: %s (%s%s)%n", error.getCategory(), error.getMessage(), error.getContext(), error.getPageNo() > 0 ? String.format(" on page %d", error.getPageNo()) : "")
);
// Validate the PDF/A standard and level of the document against the defined Conformance level.
ValidationOptions options = new ValidationOptions();
options.setConformance(conformance);
ValidationResult result = validator.validate(inDoc, options);
// Write the result of the Validate method to the console.
System.out.println("Document conforms to " + result.getConformance().toString() + ": " + result.getIsConforming());
}