public static long appendIncrementalUpdate(
        Document document, OutputStream out, long documentLength)
            throws IOException {
        // Iterate over StateManager entries, writing changed objects
        // (like Annotation, Page, Annots array) to output stream via
        // IncrementalUpdater
        
        if (logger.isLoggable(Level.FINE)) {
            if (document.getStateManager().isChanged())
                logger.fine("Have changes, will append incremental update");
            else
                logger.fine("No changes, will not append incremental update");
        }
        
        // If no changes, don't append an incremental update
        if (!document.getStateManager().isChanged())
            return 0L;

        IncrementalUpdater updater = new IncrementalUpdater(
            out, documentLength);
        updater.begin();
        
        Iterator<PObject> changes =
            document.getStateManager().iteratorSortedByObjectNumber();
        while (changes.hasNext()) {
            PObject pobject = changes.next();
            updater.writeObject(pobject.getReference(), pobject.getObject());
        }

        // Write out xref table, based on IncrementalUpdater entries
        updater.writeXRefTable();

        // Write trailer
        updater.writeTrailer(document.getStateManager().getTrailer());

        return updater.getIncrementalUpdateLength();
    }