Search in sources :

Example 6 with PreflightDocument

use of org.apache.pdfbox.preflight.PreflightDocument in project mustangproject by ZUGFeRD.

the class ZUGFeRDExporter method getA1ParserValidationResult.

private boolean getA1ParserValidationResult(PreflightParser parser) throws IOException {
    ValidationResult result = 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.
			 */
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        // Get validation result
        result = document.getResult();
        document.close();
    } 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;
    }
    // display validation result
    return result.isValid();
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument)

Example 7 with PreflightDocument

use of org.apache.pdfbox.preflight.PreflightDocument in project pdfbox by apache.

the class PreflightParser method createPdfADocument.

protected void createPdfADocument(Format format, PreflightConfiguration config) throws IOException {
    COSDocument cosDocument = getDocument();
    this.preflightDocument = new PreflightDocument(cosDocument, format, config);
}
Also used : COSDocument(org.apache.pdfbox.cos.COSDocument) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument)

Example 8 with PreflightDocument

use of org.apache.pdfbox.preflight.PreflightDocument in project pdfbox by apache.

the class TestValidFiles method validate.

@Test()
public void validate() throws Exception {
    if (path == null) {
        logger.warn("This is an empty test");
        return;
    }
    PreflightDocument document = null;
    try {
        PreflightParser parser = new PreflightParser(path);
        parser.parse();
        document = parser.getPreflightDocument();
        document.validate();
        ValidationResult result = document.getResult();
        Assert.assertFalse(path + " : Isartor file should be invalid (" + path + ")", result.isValid());
        Assert.assertTrue(path + " : Should find at least one error", result.getErrorsList().size() > 0);
        // could contain more than one error
        if (result.getErrorsList().size() > 0) {
            Assert.fail("File expected valid : " + path.getAbsolutePath());
        }
    } catch (ValidationException e) {
        throw new Exception(path + " :" + e.getMessage(), e);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) Test(org.junit.Test)

Example 9 with PreflightDocument

use of org.apache.pdfbox.preflight.PreflightDocument in project pdfbox by apache.

the class TestMetadataFiles method checkPDF.

private boolean checkPDF(File pdf) {
    PreflightDocument document = null;
    boolean testResult = false;
    if (pdf.exists()) {
        ValidationResult result = null;
        try {
            PreflightParser parser = new PreflightParser(pdf);
            parser.parse();
            document = (PreflightDocument) parser.getPDDocument();
            document.validate();
            result = document.getResult();
        } catch (SyntaxValidationException e) {
            result = e.getResult();
        } catch (IOException e) {
            fail("An exception occured while parsing the PDF " + pdf + ": " + e);
        }
        if (result != null) {
            testResult = result.isValid();
        }
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
            // shouldn't happen;
            }
        }
    } else {
        fail("Can't find the given file " + pdf);
    }
    return testResult;
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) IOException(java.io.IOException) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument)

Example 10 with PreflightDocument

use of org.apache.pdfbox.preflight.PreflightDocument in project pdfbox by apache.

the class AbstractTestAction method createContext.

/**
 * Read a simple PDF/A to create a valid Context
 *
 * @return
 * @throws Exception
 */
protected PreflightContext createContext() throws Exception {
    DataSource ds = new FileDataSource("src/test/resources/pdfa-with-annotations-square.pdf");
    PDDocument doc = PDDocument.load(ds.getInputStream());
    PreflightDocument preflightDocument = new PreflightDocument(doc.getDocument(), Format.PDF_A1B);
    PreflightContext ctx = new PreflightContext(ds);
    ctx.setDocument(preflightDocument);
    preflightDocument.setContext(ctx);
    return ctx;
}
Also used : PDDocument(org.apache.pdfbox.pdmodel.PDDocument) FileDataSource(javax.activation.FileDataSource) PreflightContext(org.apache.pdfbox.preflight.PreflightContext) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Aggregations

PreflightDocument (org.apache.pdfbox.preflight.PreflightDocument)10 ValidationResult (org.apache.pdfbox.preflight.ValidationResult)6 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)4 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)4 IOException (java.io.IOException)3 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)3 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)2 SyntaxValidationException (org.apache.pdfbox.preflight.exception.SyntaxValidationException)2 Test (org.junit.Test)2 File (java.io.File)1 InputStream (java.io.InputStream)1 DataSource (javax.activation.DataSource)1 FileDataSource (javax.activation.FileDataSource)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 COSArray (org.apache.pdfbox.cos.COSArray)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSDocument (org.apache.pdfbox.cos.COSDocument)1 COSStream (org.apache.pdfbox.cos.COSStream)1