Example usage for org.apache.pdfbox.cos COSDocument getXrefTable

List of usage examples for org.apache.pdfbox.cos COSDocument getXrefTable

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSDocument getXrefTable.

Prototype

public Map<COSObjectKey, Long> getXrefTable() 

Source Link

Document

Returns the xrefTable which is a mapping of ObjectKeys to byte offsets in the file.

Usage

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

private void prepareIncrement(PDDocument doc) {
    try {//from  ww  w  . j a  v  a 2  s .  c om
        if (doc != null) {
            COSDocument cosDoc = doc.getDocument();

            Map<COSObjectKey, Long> xrefTable = cosDoc.getXrefTable();
            Set<COSObjectKey> keySet = xrefTable.keySet();
            long highestNumber = 0;
            for (COSObjectKey cosObjectKey : keySet) {
                COSBase object = cosDoc.getObjectFromPool(cosObjectKey).getObject();
                if (object != null && cosObjectKey != null && !(object instanceof COSNumber)) {
                    objectKeys.put(object, cosObjectKey);
                    keyObject.put(cosObjectKey, object);
                }

                long num = cosObjectKey.getNumber();
                if (num > highestNumber) {
                    highestNumber = num;
                }
            }
            setNumber(highestNumber);
            // xrefTable.clear();

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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 w  w .ja v  a 2  s.c o m*/
 * @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()));
    }
}