Example usage for org.apache.pdfbox.cos COSName XREF

List of usage examples for org.apache.pdfbox.cos COSName XREF

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName XREF.

Prototype

COSName XREF

To view the source code for org.apache.pdfbox.cos COSName XREF.

Click Source Link

Usage

From source file:org.apache.padaf.preflight.helpers.TrailerValidationHelper.java

License:Apache License

/**
 * Accesses and compares First and Last trailers for a PDF version higher than 1.4.
 * /*from  w ww  .  j  a v  a  2s.c  om*/
 * @param handler
 * @param result
 */
protected void checkTrailersForLinearizedPDF15(DocumentHandler handler, List<ValidationError> result) {
    PDDocument pdfDoc = handler.getDocument();
    try {
        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(handler, result);

        } 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(pdfDoc.getDocument(), firstTrailer, result);
            if (!compareIds(firstTrailer, lastTrailer, pdfDoc.getDocument())) {
                result.add(new ValidationResult.ValidationError(
                        ValidationConstants.ERROR_SYNTAX_TRAILER_ID_CONSISTENCY,
                        "ID is different in the first and the last trailer"));
            }
        }
    } catch (IOException e) {
        result.add(new ValidationResult.ValidationError(ValidationConstants.ERROR_SYNTAX_TRAILER,
                "Unable to check PDF Trailers due to : " + e.getMessage()));
    }
}