Class COSDocumentWriter


  • public final class COSDocumentWriter
    extends java.lang.Object
    Serializes a whole COSDocument built up in memory (e.g. by com.pd4ml.pdf.merge) into a complete, self-contained PDF file: header, every registered indirect object, cross-reference data and a trailer -- as opposed to COSWriter (one object's syntax only) or IncrementalUpdateWriter (an update appended to one specific original document's original bytes).

    Two layouts. A document of PDF 1.5 or later that is not encrypted is written compactly, the way PD4ML's own PDF output is: its objects other than streams packed into compressed object streams, its cross-reference data a compressed cross-reference stream (ISO 32000-1 7.5.7, 7.5.8). Written one by one as text with a classic cross-reference table, the dictionaries and arrays of a vector-heavy PDF took several times the space of the original that packed them (an optimized PD4ML output came out three times as large). A PDF 1.4 document (PDF/A-1 among them, which allows neither) and an encrypted one keep the classic layout: a string in an object stream is not encrypted on its own, and PdfEncryptor has encrypted every string already.

    The document's objects are expected to have been allocated with contiguous object numbers starting at 1 (which is what every allocator in COSObjectImporter guarantees); any gap is still handled (written as a free xref entry) but is not something this writer's own callers are expected to produce.

    Since:
    v4.1.1
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isPdfA1​(COSDocument document)
      Whether a document declares PDF/A-1 conformance: pdfaid:part 1 in its catalog's XMP metadata (as an attribute or an element).
      static byte[] write​(COSDocument document)
      Writes document as a complete PDF file, in the compact layout (object streams, a cross-reference stream) when its version is 1.5 or later and it is not encrypted, in the classic layout otherwise.
      static byte[] write​(COSDocument document, boolean objectStreams)
      Writes document as a complete PDF file, in the compact layout (object streams and a cross-reference stream; PDF 1.5) or the classic one (every object on its own, a cross-reference table).
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • write

        public static byte[] write​(COSDocument document)
                            throws COSException
        Writes document as a complete PDF file, in the compact layout (object streams, a cross-reference stream) when its version is 1.5 or later and it is not encrypted, in the classic layout otherwise. document's trailer must already have a /Root entry pointing at an indirect (keyed) Catalog object.
        Throws:
        COSException
      • write

        public static byte[] write​(COSDocument document,
                                   boolean objectStreams)
                            throws COSException
        Writes document as a complete PDF file, in the compact layout (object streams and a cross-reference stream; PDF 1.5) or the classic one (every object on its own, a cross-reference table). The compact layout is not for an encrypted document: it falls back to the classic one.
        Throws:
        COSException
        Since:
        v4.2.0
      • isPdfA1

        public static boolean isPdfA1​(COSDocument document)
        Whether a document declares PDF/A-1 conformance: pdfaid:part 1 in its catalog's XMP metadata (as an attribute or an element).
        Since:
        v4.2.0