Skip to main content

Code samples for PDF Validator

Product update

This product has been replaced by the Pdftools SDK.

PDF Validator lets you check single documents or an entire batch for conformance with the ISO PDF and PDF/A standards. Here you'll find some examples of how to integrate the code in your development.

Archive

Check if document meets PDF/A-2b compliance

1// Create the validator
2pValidator = PdfValidatorCreateObject();
3
4// Open input file
5if (!PdfValidatorOpen(pValidator, szInputPath, _T(""), ePDFA2b))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Stop validation when a conformance violation found
13PdfValidatorSetStopOnError(pValidator, 1);
14
15// Validate document
16bool isCompliant = PdfValidatorValidate(pValidator);
17
18// Close input file 
19PdfValidatorClose(pValidator);
20
21// Print result
22_tprintf(_T("Document is %scompliant with the PDF/A-2b standard.\n"), isCompliant ? "" : "not ");
23
Download code sample
1// Create the validator
2using (PdfValidator validator = new PdfValidator())
3{
4    // Open input file
5    if (!validator.Open(inputPath, "", PDFCompliance.ePDFA2b))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
8
9    // Stop validation when a conformance violation found
10    validator.StopOnError = true;
11
12    // Validate document
13    bool isCompliant = validator.Validate();
14
15    // Close input file
16    validator.Close();
17
18    // Print result
19    Console.WriteLine("Document is {0}compliant with the PDF/A-2b standard.",
20        isCompliant ? "" : "not ");
21}
22
Download code sample
1// Create the validator
2validator = new PdfValidatorAPI();
3
4// Open input file
5if (!validator.open(inputPath, "", NativeLibrary.COMPLIANCE.ePDFA2b))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, validator.getErrorMessage(), validator.getErrorCode()));
8
9// Stop validation when a conformance violation found
10validator.setStopOnError(true);
11
12// Validate document
13boolean isCompliant = validator.validate();
14
15// Close input file
16validator.close();
17
18// Print result
19System.out.printf("Document is%s compliant with the PDF/A-2b standard.\n", 
20    isCompliant ? "" : " not");
21
Download code sample

Quality Assurance

Check if document meets a specific compliance

1// Create the validator
2pValidator = PdfValidatorCreateObject();
3
4// Open input file
5if (!PdfValidatorOpen(pValidator, szInputPath, _T(""), iCompliance))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Validate document
13// If true, the PDF is compliant to the specified compliance level.
14// If false, the validation either aborted or the PDF is not compliant to 
15// the specified compliance level.
16if (!PdfValidatorValidate(pValidator))
17{
18    if (PdfValidatorGetErrorCode(pValidator) == PDF_E_CONFORMANCE)
19    {
20        _tprintf(_T("Document %s is not %s compliant.\n"), szInputPath, szCompliance);
21
22        // Print compliance violations
23        pCurrError = PdfValidatorGetFirstError(pValidator);
24        while (pCurrError != NULL)
25        {
26            int nBufSize = PdfValidatorErrorGetMessage(pCurrError, NULL, 0);
27            TCHAR* szErrorBuff = malloc(nBufSize*sizeof(TCHAR));
28            PdfValidatorErrorGetMessage(pCurrError, szErrorBuff, nBufSize);
29            _tprintf(_T("Page: %d, Object: %d, %s\n"), PdfValidatorErrorGetPageNo(pCurrError), PdfValidatorErrorGetObjectNo(pCurrError), szErrorBuff);
30            pCurrError = PdfValidatorGetNextError(pValidator);
31            free(szErrorBuff);
32        }
33    }
34    else
35    {
36        _tprintf(_T("Validation of %s was aborted. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
37    }
38}
39else
40{
41    _tprintf(_T("Document %s is %s compliant.\n"), szInputPath, szCompliance);
42}
43
44// Close input file 
45PdfValidatorClose(pValidator);
46
Download code sample
1// Create the validator
2using (PdfValidator validator = new PdfValidator())
3{
4    // Open input file
5    if (!validator.Open(inputPath, "", complianceLevel))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
8
9    // Validate document
10    // If true, the PDF is compliant to the specified compliance level.
11    // If false, the validation either aborted or the PDF is not compliant to 
12    // the specified compliance level.
13    if (!validator.Validate())
14    {
15        if (validator.ErrorCode == PDFErrorCode.PDF_E_CONFORMANCE)
16        {
17            Console.WriteLine("Document {0} is not {1} compliant.", inputPath, 
18                compliance);
19
20            // Print compliance violations
21            PdfError currError = validator.GetFirstError();
22            while (currError != null)
23            {
24                Console.WriteLine("Page: {0}, Object: {1}, {2}", currError.PageNo, 
25                    currError.ObjectNo, currError.Message);
26                currError = validator.GetNextError();
27            }
28        }
29        else
30            throw new Exception(String.Format("Validation of {0} was aborted. {1} " + 
31                "(ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
32    }
33    else
34        Console.WriteLine("Document {0} is {1} compliant.", inputPath, compliance);
35
36    // Close input file
37    validator.Close();
38}
39
Download code sample
1// Create the validator
2validator = new PdfValidatorAPI();
3
4// Open input file
5if (!validator.open(inputPath, "", complianceLevel))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7        inputPath, validator.getErrorMessage(), validator.getErrorCode()));
8
9// Validate document
10// If true, the PDF is compliant to the specified compliance level.
11// If false, the validation either aborted or the PDF is not compliant to 
12// the specified compliance level.
13if (!validator.validate())       
14{
15    if (validator.getErrorCode() == NativeLibrary.ERRORCODE.PDF_E_CONFORMANCE)
16    {
17        System.out.printf("Document %s is not %s compliant.\n", inputPath, compliance);
18
19        // Print compliance violations
20        PdfError currError = validator.getFirstError();
21        while (currError != null)
22        {
23            System.out.printf("Page: %d, Object: %s, %s\n", currError.getPageNo(), 
24                currError.getObjectNo(), currError.getMessage());
25            currError = validator.getNextError();
26        }
27    }
28    else
29        throw new IOException(String.format("Validation of %s was aborted. %s (ErrorCode: 0x%08x).",
30            inputPath, validator.getErrorMessage(), validator.getErrorCode()));
31}
32else
33    System.out.printf("Document %s is %s compliant.\n", inputPath, compliance);
34
35// Close input file
36validator.close();
37
Download code sample

Check if document satisfies custom profile

1// Create the validator
2pValidator = PdfValidatorCreateObject();
3
4// Open input file
5if (!PdfValidatorOpen(pValidator, szInputPath, _T(""), ePDFA2b))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Set custom profile
13if (!PdfValidatorSetProfile(pValidator, szProfilePath))
14{
15    _tprintf(_T("Setting custom validation profile %s failed. %s (ErrorCode: 0x%08x).\n"), szProfilePath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
16    iReturnValue = 1;
17    goto cleanup;
18}
19
20// Validate document, i.e. check if document satisfies requirements specified in custom profile
21bool isCompliant = PdfValidatorValidate(pValidator);
22
23// Close input file 
24PdfValidatorClose(pValidator);
25
26// Print result
27_tprintf(_T("Document is %scompliant to custom profile.\n"), isCompliant ? "" : "not ");
28
Download code sample
1// Create the validator
2using (PdfValidator validator = new PdfValidator())
3{
4    // Open input file
5    if (!validator.Open(inputPath, "", PDFCompliance.ePDFA2b))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
8
9    // Set custom profile
10    if (!validator.SetProfile(profilePath))
11        throw new Exception(String.Format("Setting custom validation profile {0} failed. " + 
12            "{1} (ErrorCode: 0x{2:x}).", profilePath, validator.ErrorMessage, validator.ErrorCode));
13
14    // Validate document, i.e. check if document satisfies requirements specified in custom profile
15    bool isCompliant = validator.Validate();
16
17    // Close input file
18    validator.Close();
19
20    // Print result
21    Console.WriteLine("Document is {0}compliant to custom profile.", isCompliant ? "" : "not ");
22}
23
Download code sample
1// Create the validator
2validator = new PdfValidatorAPI();
3
4// Open input file
5if (!validator.open(inputPath, "", NativeLibrary.COMPLIANCE.ePDFA2b))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, validator.getErrorMessage(), validator.getErrorCode()));
8
9// Set custom profile
10if (!validator.setProfile(profilePath))
11    throw new Exception(String.format("Setting custom validation profile %s failed. " + 
12        "%s (ErrorCode: 0x%08x).", profilePath, validator.getErrorMessage(), 
13        validator.getErrorCode()));
14
15// Validate document, i.e. check if document satisfies requirements specified in custom profile
16boolean isCompliant = validator.validate();
17
18// Close input file
19validator.close();   
20
21// Print result
22System.out.printf("Document is%s compliant to custom profile.\n", isCompliant ? "" : " not");
23
Download code sample