Example usage for org.apache.pdfbox.preflight.parser PreflightParser parse

List of usage examples for org.apache.pdfbox.preflight.parser PreflightParser parse

Introduction

In this page you can find the example usage for org.apache.pdfbox.preflight.parser PreflightParser parse.

Prototype

@Override
    public void parse() throws IOException 

Source Link

Usage

From source file:lzawebservices.java

License:Apache License

static String validate_pdfa1b(byte[] data) throws IOException {
    ValidationResult result = null;/*from   ww w .j  a  v a2  s .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:de.uzk.hki.da.convert.PdfService.java

License:Open Source License

/**
 * The Apache Preflight library is a Java tool that implements a parser
 * compliant with the ISO-19005 specification (aka PDF/A-1). Check
 * Compliance with PDF/A-1b/*from ww  w.ja  v a 2  s  . c  om*/
 *
 * @param file the file
 * @return true, if successful
 * @Author: Jens Peters
 */

public static boolean validatePdfA(File file) {

    ValidationResult result = null;
    try {

        FileDataSource fd = new FileDataSource(file);
        PreflightParser parser;
        parser = new PreflightParser(fd);
        parser.parse();
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        result = document.getResult();
        document.close();
    } catch (Exception e) {
        logger.error("Exception validating PDF/A compliance for " + file + " " + e.getCause());
        return false;
    }
    if (result.isValid()) {
        logger.info("The file " + file + " is a valid PDF/A-1b file");
        return true;
    } else {
        logger.info("The file" + file + " is not a valid PDF/A-1b, error(s) :");
        for (ValidationError error : result.getErrorsList()) {
            logger.info(error.getErrorCode() + " : " + error.getDetails());
        }
    }
    return false;
}

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.j a v a  2s .  com
    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:org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA3Factory.java

License:Open Source License

private static boolean getPDFAParserValidationResult(PreflightParser parser) throws IOException {
    /*//from   w ww.  j a v  a2 s. c o m
     * 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();// might add a Format.PDF_A1A as parameter and iterate through A1 and A3

    try (PreflightDocument document = parser.getPreflightDocument()) {
        /*
         * 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.validate();

        // Get validation result
        return document.getResult().isValid();
    } catch (ValidationException 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
         */
        return false;
    }
}

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  ww  .java  2s .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[] {}));

}