Search in sources :

Example 46 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class TrailerValidationProcess method checkTrailersForLinearizedPDF15.

/**
 * Accesses and compares First and Last trailers for a PDF version higher than 1.4.
 *
 * @param ctx the preflight context.
 */
protected void checkTrailersForLinearizedPDF15(PreflightContext ctx) {
    PDDocument pdfDoc = ctx.getDocument();
    COSDocument cosDocument = pdfDoc.getDocument();
    List<COSObject> xrefs = cosDocument.getObjectsByType(COSName.XREF);
    if (xrefs.isEmpty()) {
        // no XRef CosObject, may by this pdf file used the PDF 1.4 syntaxe
        checkTrailersForLinearizedPDF14(ctx);
    } else {
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        COSDictionary firstTrailer = null;
        COSDictionary lastTrailer = null;
        // Search First and Last trailers according to offset position.
        for (COSObject co : xrefs) {
            long offset = cosDocument.getXrefTable().get(new COSObjectKey(co));
            if (offset < min) {
                min = offset;
                firstTrailer = (COSDictionary) co.getObject();
            }
            if (offset > max) {
                max = offset;
                lastTrailer = (COSDictionary) co.getObject();
            }
        }
        checkMainTrailer(ctx, firstTrailer);
        if (!compareIds(firstTrailer, lastTrailer, cosDocument)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_ID_CONSISTENCY, "ID is different in the first and the last trailer"));
        }
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSObject(org.apache.pdfbox.cos.COSObject) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 47 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class XObjFormValidator method checkGroup.

/**
 * A Form XObject may contain a Group object (Key =" Group"). If a Group object is present, this method checks if
 * the S entry is present and if its value is different from "Transparency".
 */
protected void checkGroup() {
    COSBase baseGroup = this.xobject.getItem(XOBJECT_DICTIONARY_KEY_GROUP);
    COSDictionary groupDictionary = COSUtils.getAsDictionary(baseGroup, cosDocument);
    if (groupDictionary != null) {
        if (!XOBJECT_DICTIONARY_KEY_GROUP.equals(groupDictionary.getNameAsString(COSName.TYPE))) {
            context.addValidationError(new ValidationError(PreflightConstants.ERROR_GRAPHIC_MISSING_FIELD, "The Group dictionary hasn't Group as Type value"));
        } else {
            String sVal = groupDictionary.getNameAsString(COSName.S);
            if (sVal == null || XOBJECT_DICTIONARY_VALUE_S_TRANSPARENCY.equals(sVal)) {
                context.addValidationError(new ValidationError(ERROR_GRAPHIC_TRANSPARENCY_GROUP, "Group has a transparency S entry or the S entry is null [" + xobject.toString() + "]"));
                return;
            }
        }
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 48 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class Benchmark method main.

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("Usage : Benchmark loop resultFile <file1 ... filen|dir>");
        System.exit(255);
    }
    Integer loop = Integer.parseInt(args[0]);
    FileWriter resFile = new FileWriter(new File(args[1]));
    List<File> lfd = new ArrayList<>();
    for (int i = 2; i < args.length; ++i) {
        File fi = new File(args[i]);
        if (fi.isDirectory()) {
            // Get All files contained by the dir
            Collection<File> cf = FileUtils.listFiles(fi, null, true);
            lfd.addAll(cf);
        } else {
            lfd.add(fi);
        }
    }
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.Z");
    long startGTime = System.currentTimeMillis();
    int size = lfd.size();
    for (int i = 0; i < loop; i++) {
        File file = lfd.get(i % size);
        long startLTime = System.currentTimeMillis();
        PreflightParser parser = new PreflightParser(file);
        parser.parse();
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        ValidationResult result = document.getResult();
        if (!result.isValid()) {
            resFile.write(file.getAbsolutePath() + " isn't PDF/A\n");
            for (ValidationError error : result.getErrorsList()) {
                resFile.write(error.getErrorCode() + " : " + error.getDetails() + "\n");
            }
        }
        document.close();
        long endLTime = System.currentTimeMillis();
        resFile.write(file.getName() + " (ms) : " + (endLTime - startLTime) + "\n");
        resFile.flush();
    }
    long endGTime = System.currentTimeMillis();
    resFile.write("Start : " + sdf.format(new Date(startGTime)) + "\n");
    resFile.write("End : " + sdf.format(new Date(endGTime)) + "\n");
    resFile.write("Duration (ms) : " + (endGTime - startGTime) + "\n");
    resFile.write("Average (ms) : " + (int) ((endGTime - startGTime) / loop) + "\n");
    System.out.println("Start : " + sdf.format(new Date(startGTime)));
    System.out.println("End : " + sdf.format(new Date(endGTime)));
    System.out.println("Duration (ms) : " + (endGTime - startGTime));
    System.out.println("Average (ms) : " + (int) ((endGTime - startGTime) / loop));
    resFile.flush();
    IOUtils.closeQuietly(resFile);
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Date(java.util.Date) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser)

Example 49 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class TextAnnotationValidator method checkFlags.

/*
     * (non-Javadoc)
     * 
     * @see net.awl.edoc.pdfa.validation.annotation.AnnotationValidator#checkFlags( java.util.List)
     */
protected boolean checkFlags() {
    // call common flags settings
    boolean result = super.checkFlags();
    /*
         * For Text Annotation, this two flags should be set to avoid potential ambiguity between the annotation
         * dictionary and the reader behavior.
         */
    result = result && this.pdAnnot.isNoRotate();
    result = result && this.pdAnnot.isNoZoom();
    if (!result) {
        ctx.addValidationError(new ValidationError(ERROR_ANNOT_NOT_RECOMMENDED_FLAG));
    }
    return result;
}
Also used : ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 50 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class AbstractInvalidFileTester method validate.

@Test()
public final 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
        boolean found = false;
        if (this.expectedError != null) {
            for (ValidationError error : result.getErrorsList()) {
                if (error.getErrorCode().equals(this.expectedError)) {
                    found = true;
                    if (outputResult == null) {
                        break;
                    }
                }
                if (outputResult != null) {
                    String log = path.getName().replace(".pdf", "") + "#" + error.getErrorCode() + "#" + error.getDetails() + "\n";
                    outputResult.write(log.getBytes());
                }
            }
        }
        if (result.getErrorsList().size() > 0) {
            if (this.expectedError == null) {
                logger.info("File invalid as expected (no expected code) :" + this.path.getAbsolutePath());
            } else if (!found) {
                StringBuilder message = new StringBuilder(100);
                message.append(path).append(" : Invalid error code returned. Expected ");
                message.append(this.expectedError).append(", found ");
                for (ValidationError error : result.getErrorsList()) {
                    message.append(error.getErrorCode()).append(" ");
                }
                Assert.fail(message.toString());
            }
        } else {
            Assert.assertEquals(path + " : Invalid error code returned.", this.expectedError, result.getErrorsList().get(0).getErrorCode());
        }
    } 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) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) 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)

Aggregations

ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)108 COSBase (org.apache.pdfbox.cos.COSBase)36 COSDictionary (org.apache.pdfbox.cos.COSDictionary)28 IOException (java.io.IOException)27 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSObject (org.apache.pdfbox.cos.COSObject)13 COSArray (org.apache.pdfbox.cos.COSArray)10 COSStream (org.apache.pdfbox.cos.COSStream)10 COSString (org.apache.pdfbox.cos.COSString)8 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)8 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)8 COSName (org.apache.pdfbox.cos.COSName)7 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)7 PreflightPath (org.apache.pdfbox.preflight.PreflightPath)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)5 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)5 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)5 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)4