Example usage for org.apache.pdfbox.preflight PreflightDocument close

List of usage examples for org.apache.pdfbox.preflight PreflightDocument close

Introduction

In this page you can find the example usage for org.apache.pdfbox.preflight PreflightDocument close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

This will close the underlying COSDocument object.

Usage

From source file:lzawebservices.java

License:Apache License

static String validate_pdfa1b(byte[] data) throws IOException {
    ValidationResult result = null;// w ww.j  a  v a 2 s  .com
    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:at.gv.egiz.pdfas.lib.impl.signing.pdfbox2.PADESPDFBOXSigner.java

License:EUPL

private void runPDFAPreflight(final DataSource signedDocument) throws PdfAsException {
    PreflightDocument document = null;
    ValidationResult result = null;// w w w.  j a v  a2 s  .c  o  m
    try {
        PreflightParser parser = new PreflightParser(signedDocument);
        parser.parse(Format.PDF_A1B);
        parser.parse();
        document = parser.getPreflightDocument();
        document.validate();

        document.close();
        result = document.getResult();

        if (result.getErrorsList().size() > 0) {
            logger.error("The following validation errors occured for PDF-A validation");
        }

        for (ValidationResult.ValidationError ve : result.getErrorsList()) {
            logger.error("\t" + ve.getErrorCode() + ": " + ve.getDetails());
        }

        if (!result.isValid()) {
            throw new PdfAsException("The file is not a valid PDF-A document");
        }

    } catch (SyntaxValidationException e) {
        logger.error("The file is syntactically invalid.", e);
        throw new PdfAsException("Resulting PDF Document is syntactically invalid.");
    } catch (ValidationException e) {
        logger.error("The file is not a valid PDF-A document.", e);
        throw new PdfAsException("The file is not a valid PDF-A document");
    } catch (IOException e) {
        logger.error("An IOException (" + e.getMessage() + ") occurred, while validating the PDF-A conformance",
                e);
        throw new PdfAsException("Failed validating PDF Document IOException.");
    } catch (RuntimeException e) {
        logger.debug(
                "An RuntimeException (" + e.getMessage() + ") occurred, while validating the PDF-A conformance",
                e);
        throw new PdfAsException("Failed validating PDF Document RuntimeException.");
    } finally {
        if (document != null) {
            IOUtils.closeQuietly((Closeable) document);
        }
    }
}

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 w  ww  .  j  a v  a2 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;//  w  ww  .  j  a v  a  2  s  .  c o 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. ja va 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[] {}));

}