public class COSWriter extends Object implements org.apache.pdfbox.cos.ICOSVisitor
Modifier and Type | Field and Description |
---|---|
static byte[] |
ARRAY_CLOSE
The array close token.
|
static byte[] |
ARRAY_OPEN
The array open token.
|
static byte[] |
COMMENT
The start to a PDF comment.
|
protected org.apache.pdfbox.cos.COSObjectKey |
currentObjectKey |
static byte[] |
DICT_CLOSE
The dictionary close token.
|
static byte[] |
DICT_OPEN
The dictionary open token.
|
static byte[] |
ENDOBJ
The end object token.
|
static byte[] |
ENDSTREAM
The close stream token.
|
static byte[] |
EOF
The EOF constant.
|
protected org.apache.pdfbox.pdmodel.fdf.FDFDocument |
fdfDocument |
static byte[] |
GARBAGE
Garbage bytes used to create the PDF header.
|
protected Map<org.apache.pdfbox.cos.COSObjectKey,org.apache.pdfbox.cos.COSBase> |
keyObject |
static byte[] |
OBJ
The starting object token.
|
protected Map<org.apache.pdfbox.cos.COSBase,org.apache.pdfbox.cos.COSObjectKey> |
objectKeys |
protected Deque<org.apache.pdfbox.cos.COSBase> |
objectsToWrite |
protected PDDocument |
pdDocument |
static byte[] |
REFERENCE
The reference token.
|
static byte[] |
SPACE
space character.
|
static byte[] |
STARTXREF
The start xref token.
|
static byte[] |
STREAM
The open stream token.
|
static byte[] |
TRAILER
The trailer token.
|
static byte[] |
VERSION
The output version of the PDF.
|
protected boolean |
willEncrypt |
protected Set<org.apache.pdfbox.cos.COSBase> |
writtenObjects |
static byte[] |
XREF
The XREF token.
|
static byte[] |
XREF_FREE
The xref free token.
|
static byte[] |
XREF_USED
The xref used token.
|
protected List<org.apache.pdfbox.pdfparser.xref.XReferenceEntry> |
xRefEntries |
Constructor and Description |
---|
COSWriter(OutputStream outputStream)
COSWriter constructor.
|
COSWriter(OutputStream outputStream,
org.apache.pdfbox.pdfwriter.compress.CompressParameters compressParameters)
COSWriter constructor.
|
COSWriter(OutputStream outputStream,
org.apache.pdfbox.io.RandomAccessRead inputData)
COSWriter constructor for incremental updates.
|
COSWriter(OutputStream outputStream,
org.apache.pdfbox.io.RandomAccessRead inputData,
Set<org.apache.pdfbox.cos.COSDictionary> objectsToWrite)
Constructor for incremental updates with a list of objects to write.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addXRefEntry(org.apache.pdfbox.pdfparser.xref.XReferenceEntry entry)
add an entry in the x ref table for later dump.
|
protected void |
doWriteBody(org.apache.pdfbox.cos.COSDocument doc)
This will write the body of the document.
|
protected void |
doWriteHeader(org.apache.pdfbox.cos.COSDocument doc)
This will write the header to the PDF document.
|
void |
doWriteObject(org.apache.pdfbox.cos.COSBase obj)
This will write a COS object.
|
void |
doWriteObject(org.apache.pdfbox.cos.COSObjectKey key,
org.apache.pdfbox.cos.COSBase obj)
This will write a COS object for a predefined key.
|
protected void |
doWriteTrailer(org.apache.pdfbox.cos.COSDocument doc)
This will write the trailer to the PDF document.
|
InputStream |
getDataToSign()
Return the stream of PDF data to be signed.
|
Map<org.apache.pdfbox.cos.COSBase,org.apache.pdfbox.cos.COSObjectKey> |
getObjectKeys()
This will get all available object keys.
|
protected OutputStream |
getOutput()
This will get the output stream.
|
protected org.apache.pdfbox.pdfwriter.COSStandardOutputStream |
getStandardOutput()
This will get the standard output stream.
|
protected long |
getStartxref()
This will get the current start xref.
|
protected List<org.apache.pdfbox.pdfparser.xref.XReferenceEntry> |
getXRefEntries()
This will get the xref entries.
|
protected Long[] |
getXRefRanges(List<org.apache.pdfbox.pdfparser.xref.XReferenceEntry> xRefEntriesList)
check the xref entries and write out the ranges.
|
boolean |
isCompress()
Returns true, if the resulting document shall be compressed.
|
protected void |
setOutput(OutputStream newOutput)
This will set the output stream.
|
protected void |
setStandardOutput(org.apache.pdfbox.pdfwriter.COSStandardOutputStream newStandardOutput)
This will set the standard output stream.
|
protected void |
setStartxref(long newStartxref)
This will set the start xref.
|
void |
visitFromArray(org.apache.pdfbox.cos.COSArray array) |
void |
visitFromBoolean(org.apache.pdfbox.cos.COSBoolean obj) |
void |
visitFromDictionary(org.apache.pdfbox.cos.COSDictionary obj) |
void |
visitFromDocument(org.apache.pdfbox.cos.COSDocument doc) |
void |
visitFromFloat(org.apache.pdfbox.cos.COSFloat obj) |
void |
visitFromInt(org.apache.pdfbox.cos.COSInteger obj) |
void |
visitFromName(org.apache.pdfbox.cos.COSName obj) |
void |
visitFromNull(org.apache.pdfbox.cos.COSNull obj) |
void |
visitFromStream(org.apache.pdfbox.cos.COSStream obj) |
void |
visitFromString(org.apache.pdfbox.cos.COSString obj) |
void |
write(org.apache.pdfbox.cos.COSDocument doc)
This will write the pdf document.
|
void |
write(org.apache.pdfbox.pdmodel.fdf.FDFDocument doc)
This will write the fdf document.
|
void |
write(PDDocument doc)
This will write the pdf document.
|
void |
write(PDDocument doc,
org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface signInterface)
This will write the pdf document.
|
void |
writeExternalSignature(byte[] cmsSignature)
Write externally created signature of PDF data obtained via
getDataToSign() method. |
void |
writeReference(org.apache.pdfbox.cos.COSBase obj)
visitFromObjRef method comment.
|
static void |
writeString(byte[] bytes,
OutputStream output)
This will output the given text/byte getString as a PDF object.
|
static void |
writeString(org.apache.pdfbox.cos.COSString string,
OutputStream output)
This will output the given byte getString as a PDF object.
|
public static final byte[] DICT_OPEN
public static final byte[] DICT_CLOSE
public static final byte[] SPACE
public static final byte[] COMMENT
public static final byte[] VERSION
public static final byte[] GARBAGE
public static final byte[] EOF
public static final byte[] REFERENCE
public static final byte[] XREF
public static final byte[] XREF_FREE
public static final byte[] XREF_USED
public static final byte[] TRAILER
public static final byte[] STARTXREF
public static final byte[] OBJ
public static final byte[] ENDOBJ
public static final byte[] ARRAY_OPEN
public static final byte[] ARRAY_CLOSE
public static final byte[] STREAM
public static final byte[] ENDSTREAM
protected final Map<org.apache.pdfbox.cos.COSBase,org.apache.pdfbox.cos.COSObjectKey> objectKeys
protected final Map<org.apache.pdfbox.cos.COSObjectKey,org.apache.pdfbox.cos.COSBase> keyObject
protected final List<org.apache.pdfbox.pdfparser.xref.XReferenceEntry> xRefEntries
protected final Deque<org.apache.pdfbox.cos.COSBase> objectsToWrite
protected final Set<org.apache.pdfbox.cos.COSBase> writtenObjects
protected org.apache.pdfbox.cos.COSObjectKey currentObjectKey
protected PDDocument pdDocument
protected org.apache.pdfbox.pdmodel.fdf.FDFDocument fdfDocument
protected boolean willEncrypt
public COSWriter(OutputStream outputStream)
outputStream
- The output stream to write the PDF. It will be closed when this object is
closed.public COSWriter(OutputStream outputStream, org.apache.pdfbox.pdfwriter.compress.CompressParameters compressParameters)
outputStream
- The output stream to write the PDF. It will be closed when this object is closed.compressParameters
- The configuration for the document's compression.public COSWriter(OutputStream outputStream, org.apache.pdfbox.io.RandomAccessRead inputData) throws IOException
COSUpdateInfo.isNeedToBeUpdated()
set, starting from the document catalog. For signatures this is taken
care by PDFBox itself.outputStream
- output stream where the new PDF data will be written. It will be closed when this object is
closed.inputData
- random access read containing source PDF dataIOException
- if something went wrongpublic COSWriter(OutputStream outputStream, org.apache.pdfbox.io.RandomAccessRead inputData, Set<org.apache.pdfbox.cos.COSDictionary> objectsToWrite) throws IOException
COSUpdateInfo.isNeedToBeUpdated()
set so the incremental update gets smaller. Only
dictionaries are supported; if you need to update other objects classes, then add their
parent dictionary.outputStream
- output stream where the new PDF data will be written. It will be closed
when this object is closed.inputData
- random access read containing source PDF data.objectsToWrite
- objects that must be part of the incremental saving.IOException
- if something went wrongpublic static void writeString(org.apache.pdfbox.cos.COSString string, OutputStream output) throws IOException
string
- COSString to be writtenoutput
- The stream to write to.IOException
- If there is an error writing to the stream.public static void writeString(byte[] bytes, OutputStream output) throws IOException
bytes
- byte array representation of a string to be writtenoutput
- The stream to write to.IOException
- If there is an error writing to the stream.public boolean isCompress()
protected void addXRefEntry(org.apache.pdfbox.pdfparser.xref.XReferenceEntry entry)
entry
- The new entry to add.protected OutputStream getOutput()
protected void setOutput(OutputStream newOutput)
newOutput
- The new output stream.protected org.apache.pdfbox.pdfwriter.COSStandardOutputStream getStandardOutput()
protected void setStandardOutput(org.apache.pdfbox.pdfwriter.COSStandardOutputStream newStandardOutput)
newStandardOutput
- The new standard output stream.protected long getStartxref()
protected void setStartxref(long newStartxref)
newStartxref
- The new start xref attribute.protected List<org.apache.pdfbox.pdfparser.xref.XReferenceEntry> getXRefEntries()
protected void doWriteBody(org.apache.pdfbox.cos.COSDocument doc) throws IOException
doc
- The document to write the body for.IOException
- If there is an error writing the data.public void doWriteObject(org.apache.pdfbox.cos.COSObjectKey key, org.apache.pdfbox.cos.COSBase obj) throws IOException
key
- The key of the object to write.obj
- The object to write.IOException
- if the output cannot be writtenpublic void doWriteObject(org.apache.pdfbox.cos.COSBase obj) throws IOException
obj
- The object to write.IOException
- if the output cannot be writtenprotected void doWriteHeader(org.apache.pdfbox.cos.COSDocument doc) throws IOException
doc
- The document to get the data from.IOException
- If there is an error writing to the stream.protected void doWriteTrailer(org.apache.pdfbox.cos.COSDocument doc) throws IOException
doc
- The document to create the trailer for.IOException
- If there is an IOError while writing the document.public InputStream getDataToSign() throws IOException
write(PDDocument)
method should have been called prior. The
created signature should be set using writeExternalSignature(byte[])
.
When SignatureInterface
instance is used, COSWriter obtains and writes the signature
itself.
IllegalStateException
- if PDF is not prepared for external signingIOException
- if input data is closedpublic void writeExternalSignature(byte[] cmsSignature) throws IOException
getDataToSign()
method.cmsSignature
- CMS signature byte arrayIllegalStateException
- if PDF is not prepared for external signingIOException
- if source data stream is closedprotected Long[] getXRefRanges(List<org.apache.pdfbox.pdfparser.xref.XReferenceEntry> xRefEntriesList)
example: 0 1 2 5 6 7 8 10
will create a array with follow ranges
0 3 5 4 10 1
this mean that the element 0 is followed by two other related numbers that represent a cluster of the size 3. 5 is follow by three other related numbers and create a cluster of size 4. etc.
xRefEntriesList
- list with the xRef entries that was writtenpublic void visitFromArray(org.apache.pdfbox.cos.COSArray array) throws IOException
visitFromArray
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromBoolean(org.apache.pdfbox.cos.COSBoolean obj) throws IOException
visitFromBoolean
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromDictionary(org.apache.pdfbox.cos.COSDictionary obj) throws IOException
visitFromDictionary
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromDocument(org.apache.pdfbox.cos.COSDocument doc) throws IOException
visitFromDocument
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromFloat(org.apache.pdfbox.cos.COSFloat obj) throws IOException
visitFromFloat
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromInt(org.apache.pdfbox.cos.COSInteger obj) throws IOException
visitFromInt
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromName(org.apache.pdfbox.cos.COSName obj) throws IOException
visitFromName
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromNull(org.apache.pdfbox.cos.COSNull obj) throws IOException
visitFromNull
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void writeReference(org.apache.pdfbox.cos.COSBase obj) throws IOException
obj
- The object that is being visited.IOException
- If there is an exception while visiting this object.public void visitFromStream(org.apache.pdfbox.cos.COSStream obj) throws IOException
visitFromStream
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void visitFromString(org.apache.pdfbox.cos.COSString obj) throws IOException
visitFromString
in interface org.apache.pdfbox.cos.ICOSVisitor
IOException
public void write(org.apache.pdfbox.cos.COSDocument doc) throws IOException
doc
- The document to write.IOException
- If an error occurs while generating the data.public void write(PDDocument doc) throws IOException
writeExternalSignature(byte[])
should be invoked to set signature after calling this method.doc
- The document to write.IOException
- If an error occurs while generating the data.public void write(PDDocument doc, org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface signInterface) throws IOException
writeExternalSignature(byte[])
should be invoked to set signature after calling this method.doc
- The document to write.signInterface
- class to be used for signing; null
if external signing would be performed
or there will be no signing at allIOException
- If an error occurs while generating the data.IllegalStateException
- If the document has an encryption dictionary but no protection
policy.public void write(org.apache.pdfbox.pdmodel.fdf.FDFDocument doc) throws IOException
doc
- The document to write.IOException
- If an error occurs while generating the data.public Map<org.apache.pdfbox.cos.COSBase,org.apache.pdfbox.cos.COSObjectKey> getObjectKeys()
Copyright © 2025. All rights reserved.