Java Code Examples for org.apache.pdfbox.cos.COSDocument#isXRefStream()

The following examples show how to use org.apache.pdfbox.cos.COSDocument#isXRefStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: COSWriter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 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.
 */
protected void doWriteTrailer(COSDocument doc) throws IOException
{
    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.setLong(COSName.SIZE, lastEntry.getKey().getNumber()+1);
    // Only need to stay, if an incremental update will be performed
    if (!incrementalUpdate) 
    {
      trailer.removeItem( COSName.PREV );
    }
    if (!doc.isXRefStream())
    {
        trailer.removeItem( COSName.XREF_STM );
    }
    // Remove a checksum if present
    trailer.removeItem( COSName.DOC_CHECKSUM );

    COSArray idArray = trailer.getCOSArray(COSName.ID);
    if (idArray != null)
    {
        idArray.setDirect(true);
    }

    trailer.accept(this);
}
 
Example 2
Source File: COSWriter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Object visitFromDocument(COSDocument doc) throws IOException
{
    if(!incrementalUpdate)
    {
        doWriteHeader(doc);
    }
    else
    {
        // Sometimes the original file will be missing a newline at the end
        // In order to avoid having %%EOF the first object on the same line
        // as the %%EOF, we put a newline here. If there's already one at
        // the end of the file, an extra one won't hurt. PDFBOX-1051
        getStandardOutput().writeCRLF();
    }

    doWriteBody(doc);

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

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

    if(incrementalUpdate || doc.isXRefStream())
    {
        doWriteXRefInc(doc, hybridPrev);
    }
    else
    {
        doWriteXRefTable();
        doWriteTrailer(doc);
    }

    // write endof
    getStandardOutput().write(STARTXREF);
    getStandardOutput().writeEOL();
    getStandardOutput().write(String.valueOf(getStartxref()).getBytes(Charsets.ISO_8859_1));
    getStandardOutput().writeEOL();
    getStandardOutput().write(EOF);
    getStandardOutput().writeEOL();

    if (incrementalUpdate)
    {
        if (signatureOffset == 0 || byteRangeOffset == 0)
        {
            doWriteIncrement();
        }
        else
        {
            doWriteSignature();
        }
    }

    return null;
}