PDF/A and PDF/UA validation
Validate PDF/A (every level from 1a to 4f) and PDF/UA with clause ids, and convert to a chosen PDF/A level or PDF/UA part, with NexPDF for iOS (Swift).
License module
Validation and conversion need the standards module (Enterprise tier).
NexPDF checks documents against the machine-checkable rules of PDF/A (ISO 19005-1 to -4) and PDF/UA (ISO 14289). Each failure names the clause of the standard that requires it, with the same numbers the veraPDF rule sets use.
Validate PDF/A
validatePdfaJson(level:) takes 1a, 1b, 2a, 2b, 2u, 3a, 3b, 3u, 4, 4e or 4f
(or PDF/A-2b). nil uses the level the document declares, or 2b.
import NexPdf
let doc = try NexPdfDocument.open(url: archiveURL)
let report = NexPdf.json(try doc.validatePdfaJson(level: "2b")) as? [String: Any] ?? [:]
print(report["level"] ?? "", report["compliant"] as? Bool == true ? "compliant" : "not compliant")
for f in report["failures"] as? [[String: Any]] ?? [] {
print(f["clause"] ?? "", f["message"] ?? "", f["page"] ?? "", f["object"] ?? "")
}Each failure has clause (such as 6.2.11.4.1), message, page (1-based, or NSNull) and
object (such as 12 0 R, or NSNull).
The rules cover file structure, metadata (XMP, pdfaid, extension schemas), fonts (embedding,
glyphs, widths, encodings), colour (output intents, ICC profiles), graphics and transparency,
interactive features (annotations, actions, forms, embedded files) and, for level A, the tagged
structure.
Validate PDF/UA
validatePdfuaJson() validates against the PDF/UA part the document declares, PDF/UA-1 by
default. The report has the same shape, with level PDF/UA-1 or PDF/UA-2.
let ua = NexPdf.json(try doc.validatePdfuaJson()) as? [String: Any] ?? [:]
print(ua["level"] ?? "", ua["compliant"] ?? false, (ua["failures"] as? [Any])?.count ?? 0)PDF/UA-1 follows the Matterhorn Protocol. PDF/UA-2 validation is complete: PDF 2.0 namespaces
(/NS, /RoleMapNS, the root /RoleMap, MathML), the ISO 14289-2 structure, annotation, form,
destination and text-string rules, and the ISO/TS 32005 parent and child rules.
To validate against one part whatever the document declares, use validatePdfuaPartJson(part:)
with 1 or 2. It returns the same report.
let ua2 = NexPdf.json(try doc.validatePdfuaPartJson(part: 2)) as? [String: Any] ?? [:]
print(ua2["level"] ?? "", ua2["compliant"] ?? false)Convert to PDF/UA
convertPdfuaJson(part:lang:) converts to PDF/UA-1 (1) or PDF/UA-2 (2): it tags an untagged
document, embeds fonts and, for part 2, uses the PDF 2.0 namespace, structure destinations and the
PDF/UA-2 annotation rules. lang (nil is en-US) applies when the document has no language.
The report has profile, compliant, fixed, warnings, substitutions and remaining.
let doc = try NexPdfDocument.open(url: reportURL)
let r = NexPdf.json(try doc.convertPdfuaJson(part: 2, lang: "en-GB")) as? [String: Any] ?? [:]
print(r["profile"] ?? "", r["compliant"] ?? false, r["fixed"] ?? [], r["warnings"] ?? [])
for f in r["remaining"] as? [Any] ?? [] { print("left:", f) }
try doc.write(to: pdfua2URL)Convert to a level
convertPdfaLevel(level:) converts to the level you choose (nil is 2b). Level A tags the
document first. When the result validates against the level it returns 1. When some clauses
still fail, the fixes that could be made are kept and the engine reports the clauses that are left
as the error, so the call throws NexPdfError with that list:
let doc = try NexPdfDocument.open(url: reportURL)
do {
try doc.convertPdfaLevel(level: "3a")
} catch let error as NexPdfError {
print("still failing:", error.message) // the document keeps the fixes that worked
}
try doc.write(to: pdfa3aURL)Conversion with a report
convertPdfaJson(level:flattenDpi:substituteFonts:) converts like convertPdfaLevel(level:) but
does not throw when clauses remain: it reports them.
| Argument | Meaning |
|---|---|
level | 1a to 4f; nil is 2b |
flattenDpi | Resolution at which PDF/A-1 transparency is flattened; 0 or less is 300 |
substituteFonts | 1 replaces non-embedded fonts with metric-compatible ones; 0 leaves non-embedded non-standard fonts alone |
let doc = try NexPdfDocument.open(url: reportURL)
let r = NexPdf.json(try doc.convertPdfaJson(level: "1b", flattenDpi: 300, substituteFonts: 1)) as? [String: Any] ?? [:]
print(r["level"] ?? "", r["compliant"] ?? false, r["fixed"] ?? [])
for s in r["substitutions"] as? [[String: Any]] ?? [] {
print(s["original"] ?? "", "->", s["substitute"] ?? "", s["exact"] ?? false, s["widths_adjusted"] ?? false)
}
for f in r["remaining"] as? [[String: Any]] ?? [] {
print(f["clause"] ?? "", f["message"] ?? "", f["page"] ?? "", f["object"] ?? "")
}
try doc.write(to: pdfa1bURL)The conversion flattens PDF/A-1 transparency into opaque image patches (text stays text), fixes device colour (a CMYK output intent or Default colour spaces), draws missing appearance streams for widgets and markup annotations, substitutes metric-compatible fonts for non-embedded fonts and removes unreachable objects. For PDF/A-4e it handles U3D and PRC 3D streams and 3D and RichMedia annotations.
To use your own font for substitution, register its TrueType data (with glyf outlines) first.
Non-embedded fonts with the same PostScript or family name then use it:
let name = try NexPdf.registerSubstituteFont(data: try Data(contentsOf: frutigerURL))
print("registered", name)OCR text layers use glyph-less fonts; convert to PDF/A after running OCR.
See PDF/A, PDF/X and PDF/UA for PDF/X and accessibility tagging.