PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Use validateStandardPDFWithType:basePath: of CPDFDocument to run a basic rule check of an opened document against a specified standard. The method returns a CPDFStandardValidationResult. Validation works on the document that is opened in memory, so to validate a conversion result, open the converted output file first and then validate it.
Supported Validation Targets
| Standard | format | typenum | partType | Status |
|---|---|---|---|---|
| PDF/A-1a, 1b | CPDFStandardFormatPDFA | 1 | CPDFPartTypePDF_a, CPDFPartTypePDF_b | Stable |
| PDF/A-2a, 2u, 2b, PDF/A-3a, 3u, 3b | CPDFStandardFormatPDFA | 2, 3 | CPDFPartTypePDF_a, CPDFPartTypePDF_u, CPDFPartTypePDF_b | Stable |
| PDF/E-1, E-2 | CPDFStandardFormatPDFE | 1, 2 | CPDFPartTypeNone | Stable |
| PDF/X-1a, X-3, X-4 | CPDFStandardFormatPDFX | 1, 3, 4 | CPDFPartTypeNone | Experimental |
| PDF/UA | CPDFStandardFormatPDFUA | Ignored | CPDFPartTypeNone | Experimental |
| PDF/VT | CPDFStandardFormatPDFVT | Ignored (checks the VT level the document declares) | CPDFPartTypeNone | Experimental |
basePath is the directory used to resolve externally referenced files. It is only used for PDF/X validation and can be nil.
The method returns nil in the following cases:
Validation Results
Properties of CPDFStandardValidationResult:
| Property | Type | Description |
|---|---|---|
passed | BOOL | Whether the document conforms to the validated standard. |
detectedType | CPDFStandardMode | The standard level the document declares; nil when validating PDF/E, PDF/UA, or PDF/VT. |
issues | NSArray<CPDFStandardValidationIssue *> | The list of issues found. |
Properties of CPDFStandardValidationIssue:
| Property | Type | Description |
|---|---|---|
code | NSInteger | The issue code reported by the core library. |
ruleId | NSString | The identifier of the violated rule, such as a clause of the standard. May be empty. |
message | NSString | The description of the issue. |
pageIndex | NSInteger | The index of the page the issue belongs to; -1 means the issue applies to the whole document. |
standardName | NSString | The name of the standard the rule belongs to. May be empty. |
severity | CPDFStandardValidationSeverity | The severity of the issue. |
Values of CPDFStandardValidationSeverity:
| Value | Description |
|---|---|
CPDFStandardValidationSeverityError | Error. The document does not conform to the standard. |
CPDFStandardValidationSeverityWarning | Warning. |
CPDFStandardValidationSeverityInfo | Informational note. |
"Converted" vs. "Passed Validation"
A successful conversion only means that a document of the target standard has been generated (converted). Whether the document conforms to the rules of the standard must be confirmed by calling the validation method (passed is YES when the document passes the basic validation). These are two different states, and we recommend showing them separately in your UI.
This example shows how to convert a document to PDF/A-2b, then validate the converted document and print the issues:
let document = CPDFDocument(url: URL(fileURLWithPath: pdfPath))
let pdfA2b = CPDFStandardMode()
pdfA2b.format = .PDFA
pdfA2b.typenum = 2
pdfA2b.partType = .pdf_b
do {
// 1. Convert to PDF/A-2b
try document?.writeStandardPDF(toFilePath: savePath, withType: pdfA2b, iccFilePath: nil, with: nil, isSaveFontSubset: false, error: ())
} catch {
print("Conversion failed: \((error as NSError).localizedFailureReason ?? "")")
return
}
// 2. Open the converted document and validate it against PDF/A-2b
let convertedDocument = CPDFDocument(url: URL(fileURLWithPath: savePath))
guard let result = convertedDocument?.validateStandardPDF(withType: pdfA2b, basePath: nil) else {
// The document is not loaded, the target is not supported, or the license does not include PDF/A conversion
return
}
print("Passed: \(result.passed)")
for issue in result.issues {
let level: String
switch issue.severity {
case .error:
level = "Error"
case .warning:
level = "Warning"
default:
level = "Info"
}
// pageIndex -1 means a document-level issue
let location = issue.pageIndex == -1 ? "Document" : "Page \(issue.pageIndex + 1)"
print("[\(level)] \(location) \(issue.ruleId): \(issue.message)")
}CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:pdfPath]];
CPDFStandardMode *pdfA2b = [[CPDFStandardMode alloc] init];
pdfA2b.format = CPDFStandardFormatPDFA;
pdfA2b.typenum = 2;
pdfA2b.partType = CPDFPartTypePDF_b;
// 1. Convert to PDF/A-2b
NSError *error = nil;
if (![document writeStandardPDFToFilePath:savePath
withType:pdfA2b
iccFilePath:nil
withUAConfig:nil
isSaveFontSubset:NO
error:&error]) {
NSLog(@"Conversion failed: %@", error.localizedFailureReason);
return;
}
// 2. Open the converted document and validate it against PDF/A-2b
CPDFDocument *convertedDocument = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:savePath]];
CPDFStandardValidationResult *result = [convertedDocument validateStandardPDFWithType:pdfA2b basePath:nil];
if (!result) {
// The document is not loaded, the target is not supported, or the license does not include PDF/A conversion
return;
}
NSLog(@"Passed: %d", result.passed);
for (CPDFStandardValidationIssue *issue in result.issues) {
NSString *level = @"Info";
if (issue.severity == CPDFStandardValidationSeverityError) {
level = @"Error";
} else if (issue.severity == CPDFStandardValidationSeverityWarning) {
level = @"Warning";
}
// pageIndex -1 means a document-level issue
NSString *location = issue.pageIndex == -1 ? @"Document" : [NSString stringWithFormat:@"Page %ld", (long)issue.pageIndex + 1];
NSLog(@"[%@] %@ %@: %@", level, location, issue.ruleId, issue.message);
}Note: Standard validation is a basic rule check. It is not a full compliance certification equivalent to a third-party professional validator. PDF/X, PDF/UA, and PDF/VT validation are experimental, and the results may be incomplete.