This sample Java code using jPDFPreflight shows how to cancel the verification process once a failure of verification is found.

The com.qoppa.pdfPreflight.PDFPreflight interface gives access to a class called ResultRecord that contains a lot of information about the issue found. So it is possible to adapt this sample to cancel the verification or conversion process based on:

  • the type of result / issue found
  • the number of issues found
  • whether or not the issue can be fixed.
package jPDFPreflightSamples;
import java.io.IOException;
import com.qoppa.pdf.PDFException;
import com.qoppa.pdfPreflight.PDFPreflight;
import com.qoppa.pdfPreflight.ProgressListener;
import com.qoppa.pdfPreflight.profiles.PDFA_1_B_Verification;
import com.qoppa.pdfPreflight.results.PreflightResults;
import com.qoppa.pdfPreflight.results.ResultRecord;
 
public class ValidatePDFA_FailFast
{
/**
* @param args
*/
public static void main(String[] args)
{
try
{
System.out.println("Starting");
PDFPreflight pdfPreflight = new PDFPreflight("C:\\input.pdf", null);
 
/*
* create a new ProgressListener instance that will tell the preflight operation
* to cancel running once the first ResultRecord is generated
* In general a ResultRecord indicates failure of verification. A user
* may or may not be interested in generating the full list of Result Records.
* This mechanism allows for quick termination of the verification process based
* on the accumulated issues found. 
*/
ProgressListener listener = new ProgressListener(){
 
private boolean shouldCancel = false;
 
@Override
public void progress(int pageNumber, int percent, String message)
{
//ignore
}
 
@Override
public boolean shouldCancel()
{
return shouldCancel;
}
 
@Override
public void resultAdded(ResultRecord result)
{
shouldCancel = true;
}
 
};
 
// Validate the document
PreflightResults results = pdfPreflight.verifyDocument(new PDFA_1_B_Verification(), listener);
if (results.isSuccessful())
{
System.out.println("PDF is compliant");
}
else
{
System.out.println("PDF is not compliant");
// Add annotations to the document
results.addResultAnnotations();
// Append report to the document
results.appendPreflightReport(612, 792);
// Save the PDF document with annotations and report
pdfPreflight.saveDocument("C:\\validationreport.pdf");
}
}
catch(Throwable t)
{
System.out.println(t);
}
}
}