Click or drag to resize
Pdf Library for .NET

Fix Pdf Documents for PDF/A, PDF/UA and PDF/X (Preflight)

SelectPdf PDF Library for .NET can fix an existing PDF document so that it conforms to a standard: PDF/A, PDF/UA or PDF/X. The document is validated first; one that conforms already is returned as it is. Otherwise the fixes for what it fails are applied, and the result is validated again, so you see what was fixed and what is left.

Fixing is done with Fix, which returns a PreflightResult: the fixed document, the fixes applied (PreflightFix: what each did and to how many objects) and the ValidationReport of the fixed document. A digitally signed document is refused: every fix rewrites the file, which the signature would no longer cover. Fix the document before it is signed.

The whole fix - the validation before, the fixes and the validation after - runs within the timeout of Validation (30 seconds by default) and under the memory limit a validation has. Past the timeout Fix throws TimeoutException; raise Timeout for very large documents.

PDF/A

The document is converted to the PDF/A level you ask for, any part from PDF/A-1 to PDF/A-4. Before the conversion, actions the level forbids (launching applications, playing sounds and movies, and from PDF/A-2 hiding fields and the like) are removed, fonts are embedded, the text of glyphs the document does not map to Unicode is recovered where the font allows it, and embedded files are given the names, type and relationship the level requires. PDF/A-3 and PDF/A-4f keep embedded files; for the other levels, embedded files the level does not admit are removed only when RemoveAttachmentsAndJavaScript is set - otherwise they are kept and reported.

PDF/UA

A document without tags is tagged (headings, paragraphs, lists, tables, figures, links and form fields are recognized on the page), and a document whose tags leave content outside them is tagged again. Then the language is set from Language when the document has none - it is never guessed - and a missing title is taken from the first heading. Annotations and form fields are tagged, links are described, heading levels that skip a level are renumbered, and fonts are embedded and mapped to Unicode. A PDF/A claim the standard contradicts (PDF/A-1 to 3 under PDF/UA-2, which is a PDF 2.0 standard, or PDF/A-4 under PDF/UA-1) is removed and reported; a PDF/A claim that can hold is kept valid.

What needs a person is still reported after the fixes: alternate text for figures, the headers of an irregular table, the label of a form field. Use the tags and alternate text APIs to supply them, then validate again.

PDF/X

Encryption is removed; the identification, dates and output intent are written; every page gets a TrimBox; colour is converted to CMYK - through the output profile you give (OutputProfile, an ICC printer profile), or with the device formula without one - or, under an RGB or Gray output intent, given the colour spaces that describe it. Transparency, hidden layers, transfer functions and compressions the level forbids are removed, and fonts are embedded from the installed fonts. Annotations in the printed area are removed only when RemoveAnnotations is set (what they print is drawn into the page first), and embedded files and JavaScript only when RemoveAttachmentsAndJavaScript is set.

Example
byte[] pdf = File.ReadAllBytes("report.pdf");

PreflightResult result = PdfPreflight.Fix(pdf, null,
    PdfConformanceStandard.PdfUA1,
    new PreflightOptions(Language: "en-US"));

foreach (PreflightFix fix in result.Applied)
    Console.WriteLine(fix.Description + " (" + fix.Count + ")");

if (result.After.IsCompliant)
    File.WriteAllBytes("report-ua1.pdf", result.Pdf);
else
    foreach (RuleFailure failure in result.After.Failures)
        Console.WriteLine("Left: " + failure.Rule + " " + failure.Message);

For print, with a printer profile:

var options = new PreflightOptions(
    OutputProfile: File.ReadAllBytes("ISOcoated_v2_eci.icc"),
    RemoveAnnotations: true);
PreflightResult result = PdfPreflight.Fix(pdf, null,
    PdfConformanceStandard.PdfX4, options);
See Also