Example usage for org.apache.pdfbox.pdfwriter COSWriterXRefEntry getKey

List of usage examples for org.apache.pdfbox.pdfwriter COSWriterXRefEntry getKey

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdfwriter COSWriterXRefEntry getKey.

Prototype

public COSObjectKey getKey() 

Source Link

Document

This will get the Object key.

Usage

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

License:Apache License

/**
 * This will write the trailer to the PDF document.
 *
 * @param doc The document to create the trailer for.
 *
 * @throws IOException If there is an IOError while writing the document.
 * @throws COSVisitorException If there is an error while generating the
 * data./*from w  w  w . ja v a2  s . c o m*/
 */
protected void doWriteTrailer(COSDocument doc) throws IOException, COSVisitorException {
    getStandardOutput().write(TRAILER);
    getStandardOutput().writeEOL();

    COSDictionary trailer = doc.getTrailer();
    //sort xref, needed only if object keys not regenerated
    Collections.sort(getXRefEntries());
    COSWriterXRefEntry lastEntry = getXRefEntries().get(getXRefEntries().size() - 1);
    trailer.setInt(COSName.SIZE, (int) lastEntry.getKey().getNumber() + 1);
    // Only need to stay, if an incremental update will be performed
    if (!incrementalUpdate) {
        trailer.removeItem(COSName.PREV);
    }
    // Remove a checksum if present
    trailer.removeItem(COSName.DOC_CHECKSUM);

    trailer.accept(this);
}

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

License:Apache License

/**
 * write the x ref section for the pdf file
 *
 * currently, the pdf is reconstructed from the scratch, so we write a
 * single section/*w  ww .  jav  a2s.c om*/
 *
 * todo support for incremental writing?
 *
 * @param doc The document to write the xref from.
 *
 * @throws IOException If there is an error writing the data to the stream.
 */
protected void doWriteXRef(COSDocument doc) throws IOException {
    if (doc.isXRefStream()) {
        // sort xref, needed only if object keys not regenerated
        Collections.sort(getXRefEntries());
        COSWriterXRefEntry lastEntry = getXRefEntries().get(getXRefEntries().size() - 1);

        // remember the position where x ref is 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
        writeXrefRange(0, lastEntry.getKey().getNumber() + 1);
        // write initial start object with ref to first deleted object and magic generation number
        writeXrefEntry(COSWriterXRefEntry.getNullEntry());
        // write entry for every object
        long lastObjectNumber = 0;
        for (Iterator<COSWriterXRefEntry> i = getXRefEntries().iterator(); i.hasNext();) {
            COSWriterXRefEntry entry = i.next();
            while (lastObjectNumber < entry.getKey().getNumber() - 1) {
                writeXrefEntry(COSWriterXRefEntry.getNullEntry());
            }
            lastObjectNumber = entry.getKey().getNumber();
            writeXrefEntry(entry);
        }
    } else {

        COSDictionary trailer = doc.getTrailer();
        trailer.setLong(COSName.PREV, doc.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

private void writeXrefEntry(COSWriterXRefEntry entry) throws IOException {
    String offset = formatXrefOffset.format(entry.getOffset());
    String generation = formatXrefGeneration.format(entry.getKey().getGeneration());
    getStandardOutput().write(offset.getBytes("ISO-8859-1"));
    getStandardOutput().write(SPACE);//from   ww  w.  ja v  a  2 s  .c  o m
    getStandardOutput().write(generation.getBytes("ISO-8859-1"));
    getStandardOutput().write(SPACE);
    getStandardOutput().write(entry.isFree() ? XREF_FREE : XREF_USED);
    getStandardOutput().writeCRLF();
}