Example usage for org.apache.pdfbox.preflight.exception SyntaxValidationException getResult

List of usage examples for org.apache.pdfbox.preflight.exception SyntaxValidationException getResult

Introduction

In this page you can find the example usage for org.apache.pdfbox.preflight.exception SyntaxValidationException getResult.

Prototype

public ValidationResult getResult() 

Source Link

Usage

From source file:lzawebservices.java

License:Apache License

static String validate_pdfa1b(byte[] data) throws IOException {
    ValidationResult result = null;//from   w  w w  .j  a  v a2s  .  c o  m
    try {
        ByteArrayDataSource bd = new ByteArrayDataSource(data, "pdf");

        PreflightParser parser = new PreflightParser(bd);

        /* Parse the PDF file with PreflightParser that inherits from the NonSequentialParser.
         * Some additional controls are present to check a set of PDF/A requirements.
         * (Stream length consistency, EOL after some Keyword...)
         */
        parser.parse();
        /* Once the syntax validation is done,
         * the parser can provide a PreflightDocument
         * (that inherits from PDDocument)
         * This document process the end of PDF/A validation.
         */
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        /* Get validation result */
        result = document.getResult();
        document.close();
    } catch (SyntaxValidationException e) {
        /* the parse method can throw a SyntaxValidationException
         * if the PDF file can't be parsed.
         * In this case, the exception contains an instance of ValidationResult
         */
        result = e.getResult();
    }
    /* display validation result */
    // if (result.isValid()) {
    // System.out.println("The file is a valid PDF/A-1b file");
    //} else {
    // System.out.println("The file is not a valid PDF/A-1b file");

    Gson gs = new Gson();
    String res = gs.toJson(result);
    System.out.println("Result=" + res);
    return res;
}

From source file:fi.vm.sade.eperusteet.service.dokumentti.impl.util.DokumenttiUtils.java

License:EUPL

public static ValidationResult validatePdf(byte[] pdf) throws IOException {
    ValidationResult result;/*from   w w  w  . ja va2s.  co m*/
    InputStream is = new ByteArrayInputStream(pdf);
    PreflightParser parser = new PreflightParser(new ByteArrayDataSource(is));

    try {
        parser.parse();

        PreflightDocument document = parser.getPreflightDocument();
        document.validate();

        // Get validation result
        result = document.getResult();
        document.close();

    } catch (SyntaxValidationException e) {
        result = e.getResult();
    }

    return result;
}

From source file:uk.bl.wa.parsers.ApachePreflightParser.java

License:Open Source License

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    // Attempt to reduce logging of stacktraces:
    //System.setProperty("log4j.logger.org.apache.pdfbox","");

    // Set up the validation result:
    ValidationResult result = null;/*from w w w.j a v a  2 s  . c o m*/

    InputStreamDataSource isds = new InputStreamDataSource(stream);
    PreflightParser parser = new PreflightParser(isds);
    PreflightDocument document = null;
    try {

        /* Parse the PDF file with PreflightParser that inherits from the NonSequentialParser.
         * Some additional controls are present to check a set of PDF/A requirements. 
         * (Stream length consistency, EOL after some Keyword...)
         */
        parser.parse();

        /* Once the syntax validation is done, 
         * the parser can provide a PreflightDocument 
         * (that inherits from PDDocument) 
         * This document process the end of PDF/A validation.
         */
        document = parser.getPreflightDocument();
        document.validate();

        // Get validation result
        result = document.getResult();

    } catch (SyntaxValidationException e) {
        /*
         * the parse method can throw a SyntaxValidationExceptionif the PDF
         * file can't be parsed.
         * 
         * In this case, the exception contains an instance of
         * ValidationResult
         */
        result = e.getResult();
    } catch (Exception e) {
        // Otherwise, a NULL result:
        result = null;

    } finally {
        // Ensure the document is always closed:
        if (document != null)
            document.close();
    }

    // display validation result
    Set<String> rs = new HashSet<String>();
    if (result != null && result.isValid()) {
        //System.out.println("The resource is not a valid PDF/A-1b file");
        metadata.set(PDF_PREFLIGHT_VALID, Boolean.TRUE.toString());
    } else {
        //System.out.println("The resource is not valid, error(s) :");
        metadata.set(PDF_PREFLIGHT_VALID, Boolean.FALSE.toString());
        if (result != null) {
            for (ValidationError error : result.getErrorsList()) {
                // System.out.println(error.getErrorCode() + " : " +
                // error.getDetails());
                rs.add(error.getErrorCode() + " : " + error.getDetails());
            }
        }
    }

    metadata.set(PDF_PREFLIGHT_ERRORS, rs.toArray(new String[] {}));

}