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

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

Introduction

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

Prototype

COSName BYTERANGE

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

Click Source Link

Usage

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

License:Apache License

/**
 * visitFromDictionary method comment./*from   w w w.  j  av a2 s  . c o m*/
 *
 * @param obj The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromDictionary(COSDictionary obj) throws COSVisitorException {
    try {
        getStandardOutput().write(DICT_OPEN);
        getStandardOutput().writeEOL();
        for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
            COSBase value = entry.getValue();
            if (value != null) {
                entry.getKey().accept(this);
                getStandardOutput().write(SPACE);
                if (value instanceof COSDictionary) {
                    COSDictionary dict = (COSDictionary) value;

                    // write all XObjects as direct objects, this will save some size
                    COSBase item = dict.getItem(COSName.XOBJECT);
                    if (item != null) {
                        item.setDirect(true);
                    }
                    item = dict.getItem(COSName.RESOURCES);
                    if (item != null) {
                        item.setDirect(true);
                    }

                    if (dict.isDirect()) {
                        // If the object should be written direct, we need
                        // to pass the dictionary to the visitor again.
                        visitFromDictionary(dict);
                    } else {
                        addObjectToWrite(dict);
                        writeReference(dict);
                    }
                } else if (value instanceof COSObject) {
                    COSBase subValue = ((COSObject) value).getObject();
                    if (subValue instanceof COSDictionary || subValue == null) {
                        addObjectToWrite(value);
                        writeReference(value);
                    } else {
                        subValue.accept(this);
                    }
                } else {
                    // If we reach the pdf signature, we need to determinate the position of the
                    // content and byterange
                    if (reachedSignature && COSName.CONTENTS.equals(entry.getKey())) {
                        signaturePosition = new int[2];
                        signaturePosition[0] = (int) getStandardOutput().getPos();
                        value.accept(this);
                        signaturePosition[1] = (int) getStandardOutput().getPos();
                    } else if (reachedSignature && COSName.BYTERANGE.equals(entry.getKey())) {
                        byterangePosition = new int[2];
                        byterangePosition[0] = (int) getStandardOutput().getPos() + 1;
                        value.accept(this);
                        byterangePosition[1] = (int) getStandardOutput().getPos() - 1;
                        reachedSignature = false;
                    } else {
                        value.accept(this);
                    }
                }
                getStandardOutput().writeEOL();

            } else {
                //then we won't write anything, there are a couple cases
                //were the value of an entry in the COSDictionary will
                //be a dangling reference that points to nothing
                //so we will just not write out the entry if that is the case
            }
        }
        getStandardOutput().write(DICT_CLOSE);
        getStandardOutput().writeEOL();
        return null;
    } catch (IOException e) {
        throw new COSVisitorException(e);
    }
}