Package com.pd4ml.pdf.cos.writer
Class COSWriter
- java.lang.Object
-
- com.pd4ml.pdf.cos.writer.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, shipsICOSVisitoras 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 throwsIOExceptiononwrite, 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) aCOSObject(an indirect reference is what that wrapper always means), or (b) any otherCOSBasethat has already been assigned an object identity viaCOSBase.setKey(com.pd4ml.pdf.cos.COSObjectKey), which is exactly the bookkeepingsetKey/getKeyexist for per their javadoc. This mirrors how every real COS document writer (PDFBox included) decides the same question.
-
-
Constructor Summary
Constructors Constructor Description COSWriter(java.io.ByteArrayOutputStream out)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.StringformatFloat(float value)Formats a PDF real number without scientific notation, keeping at least one fractional digit.intposition()java.lang.VoidvisitFromArray(COSArray array)java.lang.VoidvisitFromBoolean(COSBoolean bool)java.lang.VoidvisitFromDictionary(COSDictionary dict)java.lang.VoidvisitFromFloat(COSFloat number)java.lang.VoidvisitFromInteger(COSInteger number)java.lang.VoidvisitFromName(COSName name)java.lang.VoidvisitFromNull(COSNull nul)java.lang.VoidvisitFromObject(COSObject object)java.lang.VoidvisitFromStream(COSStream stream)java.lang.VoidvisitFromString(COSString string)voidwriteAscii(java.lang.String s)voidwriteBytes(byte[] bytes)voidwriteHex(byte[] bytes)voidwriteIndirectObject(COSObjectKey key, COSBase value)Writes a complete indirect object:"num gen obj\n<body>\nendobj\n".voidwriteName(java.lang.String name)Writes/Namewith the minimal escaping PDF name syntax requires.voidwriteString(COSString string)Writes a literal(...)string, escaping only what PDF syntax requires.voidwriteTrailerDictionary(COSDictionary trailer)Writes a bare dictionary body (no wrapping object header) -- used for the file trailer.voidwriteValue(COSBase value)Writes one nested value using the reference-vs-inline rule described in the class javadoc.
-
-
-
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 ofvalue's own key -- that key is whatvalue'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
-
visitFromDictionary
public java.lang.Void visitFromDictionary(COSDictionary dict) throws COSException
- Specified by:
visitFromDictionaryin interfaceICOSVisitor<java.lang.Void>- Throws:
COSException
-
visitFromArray
public java.lang.Void visitFromArray(COSArray array) throws COSException
- Specified by:
visitFromArrayin interfaceICOSVisitor<java.lang.Void>- Throws:
COSException
-
visitFromStream
public java.lang.Void visitFromStream(COSStream stream) throws COSException
- Specified by:
visitFromStreamin interfaceICOSVisitor<java.lang.Void>- Throws:
COSException
-
visitFromName
public java.lang.Void visitFromName(COSName name)
- Specified by:
visitFromNamein interfaceICOSVisitor<java.lang.Void>
-
visitFromInteger
public java.lang.Void visitFromInteger(COSInteger number)
- Specified by:
visitFromIntegerin interfaceICOSVisitor<java.lang.Void>
-
visitFromFloat
public java.lang.Void visitFromFloat(COSFloat number)
- Specified by:
visitFromFloatin interfaceICOSVisitor<java.lang.Void>
-
visitFromBoolean
public java.lang.Void visitFromBoolean(COSBoolean bool)
- Specified by:
visitFromBooleanin interfaceICOSVisitor<java.lang.Void>
-
visitFromNull
public java.lang.Void visitFromNull(COSNull nul)
- Specified by:
visitFromNullin interfaceICOSVisitor<java.lang.Void>
-
visitFromString
public java.lang.Void visitFromString(COSString string)
- Specified by:
visitFromStringin interfaceICOSVisitor<java.lang.Void>
-
visitFromObject
public java.lang.Void visitFromObject(COSObject object)
- Specified by:
visitFromObjectin interfaceICOSVisitor<java.lang.Void>
-
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/Contentsand/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/Namewith 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()
-
-