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());
        XReferenceEntry lastEntry = getXRefEntries().get(getXRefEntries().size() - 1);

        trailer.setLong(COSName.SIZE, lastEntry.getReferencedKey().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);
    }
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.
tika-fuzzing/src/main/java/org/apache/tika/fuzzing/pdf/EvilCOSWriter.java