Class COSWriter

  • All Implemented Interfaces:
    ICOSVisitor<java.lang.Void>

    public final class COSWriter
    extends java.lang.Object
    implements ICOSVisitor<java.lang.Void>
    A from-scratch PDF-syntax serializer for the pd4ml COS object model (which, being a reader, ships ICOSVisitor as an extension point for exactly this purpose but no implementation of its own -- see the project's README).

    Writes into a ByteArrayOutputStream, which conveniently never throws IOException on write, so the whole visitor can stay checked-exception-free.

    Reference vs. inline: a nested value is written as an indirect reference ("12 0 R") rather than inlined whenever it is either (a) a COSObject (an indirect reference is what that wrapper always means), or (b) any other COSBase that has already been assigned an object identity via COSBase.setKey(com.pd4ml.pdf.cos.COSObjectKey), which is exactly the bookkeeping setKey/getKey exist for per their javadoc. This mirrors how every real COS document writer (PDFBox included) decides the same question.

    • Constructor Detail

      • COSWriter

        public COSWriter​(java.io.ByteArrayOutputStream out)
    • Method Detail

      • writeIndirectObject

        public void writeIndirectObject​(COSObjectKey key,
                                        COSBase value)
                                 throws COSException
        Writes a complete indirect object: "num gen obj\n<body>\nendobj\n". The body is always written inline regardless of value's own key -- that key is what value's number/generation in the header comes from, and the header is exactly the declaration that "this identity now refers to this inline content".
        Throws:
        COSException
      • writeTrailerDictionary

        public void writeTrailerDictionary​(COSDictionary trailer)
                                    throws COSException
        Writes a bare dictionary body (no wrapping object header) -- used for the file trailer.
        Throws:
        COSException
      • writeValue

        public void writeValue​(COSBase value)
                        throws COSException
        Writes one nested value using the reference-vs-inline rule described in the class javadoc. Public so callers that hand-write a handful of entries in an otherwise-generic object (e.g. a PDF signature dictionary's /Contents and /ByteRange, to track their exact byte offsets) can still reuse correct reference handling for everything else in that same object.
        Throws:
        COSException
      • writeName

        public void writeName​(java.lang.String name)
        Writes /Name with the minimal escaping PDF name syntax requires.
      • writeString

        public void writeString​(COSString string)
        Writes a literal (...) string, escaping only what PDF syntax requires.
      • writeHex

        public void writeHex​(byte[] bytes)
      • formatFloat

        public static java.lang.String formatFloat​(float value)
        Formats a PDF real number without scientific notation, keeping at least one fractional digit.
      • writeAscii

        public void writeAscii​(java.lang.String s)
      • writeBytes

        public void writeBytes​(byte[] bytes)
      • position

        public int position()