Skip to main content
Version: 1.2

Validate a 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:

  1. Read the input document
  2. Create a Conformance object.
  3. Create a Validator object.
  4. Run the Validate process.

Before you begin

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.

// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = 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.

info

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).

// 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);

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.

// 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 : "");

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.

info

The input document must be a valid PDF file format. Otherwise, the validation process fails.

// 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);

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

// 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);