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

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

Introduction

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

Prototype

COSName XREF_STM

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

Click Source Link

Usage

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

License:Apache License

private void doWriteXRefInc(COSDocument doc, long hybridPrev) throws IOException, COSVisitorException {
    if (doc.isXRefStream() || hybridPrev != -1) {
        // the file uses XrefStreams, so we need to update
        // it with an xref stream. We create a new one and fill it
        // with data available here
        // first set an entry for the null entry in the xref table
        // this is probably not necessary
        // addXRefEntry(COSWriterXRefEntry.getNullEntry());

        // create a new XRefStrema object
        PDFXRefStream pdfxRefStream = new PDFXRefStream();

        // add all entries from the incremental update.
        List<COSWriterXRefEntry> xRefEntries2 = getXRefEntries();
        for (COSWriterXRefEntry cosWriterXRefEntry : xRefEntries2) {
            pdfxRefStream.addEntry(cosWriterXRefEntry);
        }/*from  w  w  w .j a  v a2 s .c om*/

        COSDictionary trailer = doc.getTrailer();
        //            trailer.setLong(COSName.PREV, hybridPrev == -1 ? prev : hybridPrev);
        trailer.setLong(COSName.PREV, doc.getStartXref());

        pdfxRefStream.addTrailerInfo(trailer);
        // the size is the highest object number+1. we add one more
        // for the xref stream object we are going to write
        pdfxRefStream.setSize(getNumber() + 2);

        setStartxref(getStandardOutput().getPos());
        COSStream stream2 = pdfxRefStream.getStream();
        doWriteObject(stream2);
    }

    if (!doc.isXRefStream() || hybridPrev != -1) {
        COSDictionary trailer = doc.getTrailer();
        trailer.setLong(COSName.PREV, doc.getStartXref());
        if (hybridPrev != -1) {
            COSName xrefStm = COSName.XREF_STM;
            trailer.removeItem(xrefStm);
            trailer.setLong(xrefStm, getStartxref());
        }
        addXRefEntry(COSWriterXRefEntry.getNullEntry());

        // sort xref, needed only if object keys not regenerated
        Collections.sort(getXRefEntries());

        // remember the position where x ref was written
        setStartxref(getStandardOutput().getPos());

        getStandardOutput().write(XREF);
        getStandardOutput().writeEOL();
        // write start object number and object count for this x ref section
        // we assume starting from scratch

        Integer[] xRefRanges = getXRefRanges(getXRefEntries());
        int xRefLength = xRefRanges.length;
        int x = 0;
        int j = 0;
        while (x < xRefLength && (xRefLength % 2) == 0) {
            writeXrefRange(xRefRanges[x], xRefRanges[x + 1]);

            for (int i = 0; i < xRefRanges[x + 1]; ++i) {
                writeXrefEntry(xRefEntries.get(j++));
            }
            x += 2;
        }
    }
}

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

License:Apache License

/**
 * The visit from document method.//from   ww  w. j a v a 2  s .c  o m
 *
 * @param doc The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromDocument(COSDocument doc) throws COSVisitorException {
    try {
        if (!incrementalUpdate) {
            doWriteHeader(doc);
        }
        doWriteBody(doc);

        // get the previous trailer
        COSDictionary trailer = doc.getTrailer();
        long hybridPrev = -1;

        if (trailer != null) {
            hybridPrev = trailer.getLong(COSName.XREF_STM);
        }

        if (incrementalUpdate) {
            doWriteXRefInc(doc, hybridPrev);
        } else {
            doWriteXRef(doc);
        }

        // the trailer section should only be used for xref tables not for xref streams
        if (!incrementalUpdate || !doc.isXRefStream() || hybridPrev != -1) {
            doWriteTrailer(doc);
        }

        // write endof
        getStandardOutput().write(STARTXREF);
        getStandardOutput().writeEOL();
        getStandardOutput().write(String.valueOf(getStartxref()).getBytes("ISO-8859-1"));
        getStandardOutput().writeEOL();
        getStandardOutput().write(EOF);
        getStandardOutput().writeEOL();

        if (incrementalUpdate) {
            doWriteSignature(doc);
        }

        return null;
    } catch (IOException e) {
        throw new COSVisitorException(e);
    } catch (SignatureException e) {
        throw new COSVisitorException(e);
    }
}