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

public void parse(Format format) throws IOException 

Source Link

Document

Parse the given file and check if it is a confirming file according to the given format.

Usage

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;//  ww  w  . j av  a 2s .c om
    ValidationResult result = null;
    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:io.inkstand.scribble.pdf.PDFAConformanceMatcher.java

License:Apache License

@Override
protected boolean matches(PDF pdf) {

    try {/*from ww  w.ja  va  2 s . c om*/
        final PreflightParser parser = new PreflightParser(pdf.toDataSource());
        parser.parse(conformanceLevel.getFormat());
        final PreflightDocument doc = parser.getPreflightDocument();
        doc.validate();
        final ValidationResult result = doc.getResult();
        this.validationResult = result;
        return result.isValid();
    } catch (IOException e) {
        LOG.debug("Could not read PDF", e);
        return false;
    }
}