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
// Create the validator
pValidator = PdfValidatorCreateObject();
// Open input file
if (!PdfValidatorOpen(pValidator, szInputPath, _T(""), ePDFA2b))
{
_tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
iReturnValue = 1;
goto cleanup;
}
// Stop validation when a conformance violation found
PdfValidatorSetStopOnError(pValidator, 1);
// Validate document
bool isCompliant = PdfValidatorValidate(pValidator);
// Close input file
PdfValidatorClose(pValidator);
// Print result
_tprintf(_T("Document is %scompliant with the PDF/A-2b standard.\n"), isCompliant ? "" : "not ");
// Create the validator
using (PdfValidator validator = new PdfValidator())
{
// Open input file
if (!validator.Open(inputPath, "", PDFCompliance.ePDFA2b))
throw new Exception(String.Format("Input file {0} cannot be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
// Stop validation when a conformance violation found
validator.StopOnError = true;
// Validate document
bool isCompliant = validator.Validate();
// Close input file
validator.Close();
// Print result
Console.WriteLine("Document is {0}compliant with the PDF/A-2b standard.",
isCompliant ? "" : "not ");
}
// Create the validator
validator = new PdfValidatorAPI();
// Open input file
if (!validator.open(inputPath, "", NativeLibrary.COMPLIANCE.ePDFA2b))
throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
inputPath, validator.getErrorMessage(), validator.getErrorCode()));
// Stop validation when a conformance violation found
validator.setStopOnError(true);
// Validate document
boolean isCompliant = validator.validate();
// Close input file
validator.close();
// Print result
System.out.printf("Document is%s compliant with the PDF/A-2b standard.\n",
isCompliant ? "" : " not");
Quality Assurance
Check if document meets a specific compliance
// Create the validator
pValidator = PdfValidatorCreateObject();
// Open input file
if (!PdfValidatorOpen(pValidator, szInputPath, _T(""), iCompliance))
{
_tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
iReturnValue = 1;
goto cleanup;
}
// Validate document
// If true, the PDF is compliant to the specified compliance level.
// If false, the validation either aborted or the PDF is not compliant to
// the specified compliance level.
if (!PdfValidatorValidate(pValidator))
{
if (PdfValidatorGetErrorCode(pValidator) == PDF_E_CONFORMANCE)
{
_tprintf(_T("Document %s is not %s compliant.\n"), szInputPath, szCompliance);
// Print compliance violations
pCurrError = PdfValidatorGetFirstError(pValidator);
while (pCurrError != NULL)
{
int nBufSize = PdfValidatorErrorGetMessage(pCurrError, NULL, 0);
TCHAR* szErrorBuff = malloc(nBufSize*sizeof(TCHAR));
PdfValidatorErrorGetMessage(pCurrError, szErrorBuff, nBufSize);
_tprintf(_T("Page: %d, Object: %d, %s\n"), PdfValidatorErrorGetPageNo(pCurrError), PdfValidatorErrorGetObjectNo(pCurrError), szErrorBuff);
pCurrError = PdfValidatorGetNextError(pValidator);
free(szErrorBuff);
}
}
else
{
_tprintf(_T("Validation of %s was aborted. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
}
}
else
{
_tprintf(_T("Document %s is %s compliant.\n"), szInputPath, szCompliance);
}
// Close input file
PdfValidatorClose(pValidator);
// Create the validator
using (PdfValidator validator = new PdfValidator())
{
// Open input file
if (!validator.Open(inputPath, "", complianceLevel))
throw new Exception(String.Format("Input file {0} cannot be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
// Validate document
// If true, the PDF is compliant to the specified compliance level.
// If false, the validation either aborted or the PDF is not compliant to
// the specified compliance level.
if (!validator.Validate())
{
if (validator.ErrorCode == PDFErrorCode.PDF_E_CONFORMANCE)
{
Console.WriteLine("Document {0} is not {1} compliant.", inputPath,
compliance);
// Print compliance violations
PdfError currError = validator.GetFirstError();
while (currError != null)
{
Console.WriteLine("Page: {0}, Object: {1}, {2}", currError.PageNo,
currError.ObjectNo, currError.Message);
currError = validator.GetNextError();
}
}
else
throw new Exception(String.Format("Validation of {0} was aborted. {1} " +
"(ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
}
else
Console.WriteLine("Document {0} is {1} compliant.", inputPath, compliance);
// Close input file
validator.Close();
}
// Create the validator
validator = new PdfValidatorAPI();
// Open input file
if (!validator.open(inputPath, "", complianceLevel))
throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
inputPath, validator.getErrorMessage(), validator.getErrorCode()));
// Validate document
// If true, the PDF is compliant to the specified compliance level.
// If false, the validation either aborted or the PDF is not compliant to
// the specified compliance level.
if (!validator.validate())
{
if (validator.getErrorCode() == NativeLibrary.ERRORCODE.PDF_E_CONFORMANCE)
{
System.out.printf("Document %s is not %s compliant.\n", inputPath, compliance);
// Print compliance violations
PdfError currError = validator.getFirstError();
while (currError != null)
{
System.out.printf("Page: %d, Object: %s, %s\n", currError.getPageNo(),
currError.getObjectNo(), currError.getMessage());
currError = validator.getNextError();
}
}
else
throw new IOException(String.format("Validation of %s was aborted. %s (ErrorCode: 0x%08x).",
inputPath, validator.getErrorMessage(), validator.getErrorCode()));
}
else
System.out.printf("Document %s is %s compliant.\n", inputPath, compliance);
// Close input file
validator.close();
Check if document satisfies custom profile
// Create the validator
pValidator = PdfValidatorCreateObject();
// Open input file
if (!PdfValidatorOpen(pValidator, szInputPath, _T(""), ePDFA2b))
{
_tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
iReturnValue = 1;
goto cleanup;
}
// Set custom profile
if (!PdfValidatorSetProfile(pValidator, szProfilePath))
{
_tprintf(_T("Setting custom validation profile %s failed. %s (ErrorCode: 0x%08x).\n"), szProfilePath, PdfValidatorGetErrorMessage(pValidator), PdfValidatorGetErrorCode(pValidator));
iReturnValue = 1;
goto cleanup;
}
// Validate document, i.e. check if document satisfies requirements specified in custom profile
bool isCompliant = PdfValidatorValidate(pValidator);
// Close input file
PdfValidatorClose(pValidator);
// Print result
_tprintf(_T("Document is %scompliant to custom profile.\n"), isCompliant ? "" : "not ");
// Create the validator
using (PdfValidator validator = new PdfValidator())
{
// Open input file
if (!validator.Open(inputPath, "", PDFCompliance.ePDFA2b))
throw new Exception(String.Format("Input file {0} cannot be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, validator.ErrorMessage, validator.ErrorCode));
// Set custom profile
if (!validator.SetProfile(profilePath))
throw new Exception(String.Format("Setting custom validation profile {0} failed. " +
"{1} (ErrorCode: 0x{2:x}).", profilePath, validator.ErrorMessage, validator.ErrorCode));
// Validate document, i.e. check if document satisfies requirements specified in custom profile
bool isCompliant = validator.Validate();
// Close input file
validator.Close();
// Print result
Console.WriteLine("Document is {0}compliant to custom profile.", isCompliant ? "" : "not ");
}
// Create the validator
validator = new PdfValidatorAPI();
// Open input file
if (!validator.open(inputPath, "", NativeLibrary.COMPLIANCE.ePDFA2b))
throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
inputPath, validator.getErrorMessage(), validator.getErrorCode()));
// Set custom profile
if (!validator.setProfile(profilePath))
throw new Exception(String.format("Setting custom validation profile %s failed. " +
"%s (ErrorCode: 0x%08x).", profilePath, validator.getErrorMessage(),
validator.getErrorCode()));
// Validate document, i.e. check if document satisfies requirements specified in custom profile
boolean isCompliant = validator.validate();
// Close input file
validator.close();
// Print result
System.out.printf("Document is%s compliant to custom profile.\n", isCompliant ? "" : " not");