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();
}
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);
}
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();
}
}
}
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;
}
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;
}
Aggregations