Package org.apache.pdfbox.pdfwriter
Class COSWriter
- java.lang.Object
-
- org.apache.pdfbox.pdfwriter.COSWriter
-
- All Implemented Interfaces:
ICOSVisitor
public class COSWriter extends java.lang.Object implements ICOSVisitor
This class acts on a in-memory representation of a PDF document.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Set<COSBase>actualsAddedstatic byte[]ARRAY_CLOSEThe array close token.static byte[]ARRAY_OPENThe array open token.private booleanblockAddingObjectprivate COSArraybyteRangeArrayprivate longbyteRangeLengthprivate longbyteRangeOffsetstatic byte[]COMMENTThe start to a PDF comment.private CompressParameterscompressParametersprivate COSObjectKeycurrentObjectKeystatic byte[]DICT_CLOSEThe dictionary close token.static byte[]DICT_OPENThe dictionary open token.static byte[]ENDOBJThe end object token.static byte[]ENDSTREAMThe close stream token.static byte[]EOFThe EOF constant.private FDFDocumentfdfDocumentprivate static java.text.NumberFormatformatXrefGenerationprivate static java.text.NumberFormatformatXrefOffsetstatic byte[]GARBAGEGarbage bytes used to create the PDF header.private RandomAccessReadincrementalInputprivate java.io.OutputStreamincrementalOutputprivate booleanincrementalUpdateprivate byte[]incrementPartprivate java.util.Map<COSObjectKey,COSBase>keyObjectprivate static org.apache.commons.logging.LogLOGprivate longnumberstatic byte[]OBJThe starting object token.private java.util.Map<COSBase,COSObjectKey>objectKeysprivate java.util.Deque<COSBase>objectsToWriteprivate java.io.OutputStreamoutputprivate PDDocumentpdDocumentprivate booleanreachedSignaturestatic byte[]REFERENCEThe reference token.private SignatureInterfacesignatureInterfaceprivate longsignatureLengthprivate longsignatureOffsetstatic byte[]SPACEspace character.private COSStandardOutputStreamstandardOutputprivate longstartxrefstatic byte[]STARTXREFThe start xref token.static byte[]STREAMThe open stream token.static byte[]TRAILERThe trailer token.static byte[]VERSIONThe output version of the PDF.private booleanwillEncryptprivate java.util.Set<COSBase>writtenObjectsstatic byte[]XREFThe XREF token.static byte[]XREF_FREEThe xref free token.static byte[]XREF_USEDThe xref used token.private java.util.List<XReferenceEntry>xRefEntries
-
Constructor Summary
Constructors Constructor Description COSWriter(java.io.OutputStream outputStream)COSWriter constructor.COSWriter(java.io.OutputStream outputStream, RandomAccessRead inputData)COSWriter constructor for incremental updates.COSWriter(java.io.OutputStream outputStream, RandomAccessRead inputData, java.util.Set<COSDictionary> objectsToWrite)Constructor for incremental updates with a list of objects to write.COSWriter(java.io.OutputStream outputStream, CompressParameters compressParameters)COSWriter constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddObjectToWrite(COSBase object)protected voidaddXRefEntry(XReferenceEntry entry)add an entry in the x ref table for later dump.private voiddetectPossibleSignature(COSDictionary obj)protected voiddoWriteBody(COSDocument doc)This will write the body of the document.private voiddoWriteBodyCompressed(COSDocument document)This will write the compressed body of the document.protected voiddoWriteHeader(COSDocument doc)This will write the header to the PDF document.private voiddoWriteIncrement()Write an incremental update for a non signature case.voiddoWriteObject(COSBase obj)This will write a COS object.voiddoWriteObject(COSObjectKey key, COSBase obj)This will write a COS object for a predefined key.private voiddoWriteObjects()private voiddoWriteSignature()protected voiddoWriteTrailer(COSDocument doc)This will write the trailer to the PDF document.private voiddoWriteXRefInc(COSDocument doc)private voiddoWriteXRefTable()private voidfillGapsWithFreeEntries()java.io.InputStreamgetDataToSign()Return the stream of PDF data to be signed.private COSObjectKeygetObjectKey(COSBase obj)This will get the object key for the object.protected java.io.OutputStreamgetOutput()This will get the output stream.protected COSStandardOutputStreamgetStandardOutput()This will get the standard output stream.protected longgetStartxref()This will get the current start xref.protected java.util.List<XReferenceEntry>getXRefEntries()This will get the xref entries.protected java.lang.Long[]getXRefRanges(java.util.List<XReferenceEntry> xRefEntriesList)check the xref entries and write out the ranges.booleanisCompress()Returns true, if the resulting document shall be compressed.private booleanisNeedToBeUpdated(COSBase base)Convenience method, so that we get false for types that can't be updated.private voidprepareIncrement()private voidsetOutput(java.io.OutputStream newOutput)This will set the output stream.private voidsetStandardOutput(COSStandardOutputStream newStandardOutput)This will set the standard output stream.protected voidsetStartxref(long newStartxref)This will set the start xref.voidvisitFromArray(COSArray array)Notification of visit to Array object.voidvisitFromBoolean(COSBoolean obj)Notification of visit to boolean object.voidvisitFromDictionary(COSDictionary obj)Notification of visit to dictionary object.voidvisitFromDocument(COSDocument doc)Notification of visit to document object.voidvisitFromFloat(COSFloat obj)Notification of visit to float object.voidvisitFromInt(COSInteger obj)Notification of visit to integer object.voidvisitFromName(COSName obj)Notification of visit to name object.voidvisitFromNull(COSNull obj)Notification of visit to null object.voidvisitFromStream(COSStream obj)Notification of visit to stream object.voidvisitFromString(COSString obj)Notification of visit to string object.voidwrite(COSDocument doc)This will write the pdf document.voidwrite(FDFDocument doc)This will write the fdf document.voidwrite(PDDocument doc)This will write the pdf document.voidwrite(PDDocument doc, SignatureInterface signInterface)This will write the pdf document.private voidwriteArray(COSArray array)private voidwriteDictionary(COSDictionary dictionary)voidwriteExternalSignature(byte[] cmsSignature)Write externally created signature of PDF data obtained viagetDataToSign()method.voidwriteReference(COSBase obj)visitFromObjRef method comment.private static voidwriteString(byte[] bytes, boolean forceHex, java.io.OutputStream output)This will output the given text/byte string as a PDF object.static voidwriteString(byte[] bytes, java.io.OutputStream output)This will output the given text/byte getString as a PDF object.static voidwriteString(COSString string, java.io.OutputStream output)This will output the given byte getString as a PDF object.private voidwriteXrefEntry(XReferenceEntry entry)private voidwriteXrefRange(long x, long y)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.pdfbox.cos.ICOSVisitor
visitFromObject
-
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
DICT_OPEN
public static final byte[] DICT_OPEN
The dictionary open token.
-
DICT_CLOSE
public static final byte[] DICT_CLOSE
The dictionary close token.
-
SPACE
public static final byte[] SPACE
space character.
-
COMMENT
public static final byte[] COMMENT
The start to a PDF comment.
-
VERSION
public static final byte[] VERSION
The output version of the PDF.
-
GARBAGE
public static final byte[] GARBAGE
Garbage bytes used to create the PDF header.
-
EOF
public static final byte[] EOF
The EOF constant.
-
REFERENCE
public static final byte[] REFERENCE
The reference token.
-
XREF
public static final byte[] XREF
The XREF token.
-
XREF_FREE
public static final byte[] XREF_FREE
The xref free token.
-
XREF_USED
public static final byte[] XREF_USED
The xref used token.
-
TRAILER
public static final byte[] TRAILER
The trailer token.
-
STARTXREF
public static final byte[] STARTXREF
The start xref token.
-
OBJ
public static final byte[] OBJ
The starting object token.
-
ENDOBJ
public static final byte[] ENDOBJ
The end object token.
-
ARRAY_OPEN
public static final byte[] ARRAY_OPEN
The array open token.
-
ARRAY_CLOSE
public static final byte[] ARRAY_CLOSE
The array close token.
-
STREAM
public static final byte[] STREAM
The open stream token.
-
ENDSTREAM
public static final byte[] ENDSTREAM
The close stream token.
-
formatXrefOffset
private static final java.text.NumberFormat formatXrefOffset
-
formatXrefGeneration
private static final java.text.NumberFormat formatXrefGeneration
-
output
private java.io.OutputStream output
-
standardOutput
private COSStandardOutputStream standardOutput
-
startxref
private long startxref
-
number
private long number
-
objectKeys
private final java.util.Map<COSBase,COSObjectKey> objectKeys
-
keyObject
private final java.util.Map<COSObjectKey,COSBase> keyObject
-
xRefEntries
private final java.util.List<XReferenceEntry> xRefEntries
-
objectsToWrite
private final java.util.Deque<COSBase> objectsToWrite
-
writtenObjects
private final java.util.Set<COSBase> writtenObjects
-
actualsAdded
private final java.util.Set<COSBase> actualsAdded
-
currentObjectKey
private COSObjectKey currentObjectKey
-
pdDocument
private PDDocument pdDocument
-
fdfDocument
private FDFDocument fdfDocument
-
willEncrypt
private boolean willEncrypt
-
incrementalUpdate
private boolean incrementalUpdate
-
reachedSignature
private boolean reachedSignature
-
signatureOffset
private long signatureOffset
-
signatureLength
private long signatureLength
-
byteRangeOffset
private long byteRangeOffset
-
byteRangeLength
private long byteRangeLength
-
incrementalInput
private RandomAccessRead incrementalInput
-
incrementalOutput
private java.io.OutputStream incrementalOutput
-
signatureInterface
private SignatureInterface signatureInterface
-
incrementPart
private byte[] incrementPart
-
byteRangeArray
private COSArray byteRangeArray
-
compressParameters
private final CompressParameters compressParameters
-
blockAddingObject
private boolean blockAddingObject
-
-
Constructor Detail
-
COSWriter
public COSWriter(java.io.OutputStream outputStream)
COSWriter constructor.- Parameters:
outputStream- The output stream to write the PDF.
-
COSWriter
public COSWriter(java.io.OutputStream outputStream, CompressParameters compressParameters)COSWriter constructor.- Parameters:
outputStream- The output stream to write the PDF.compressParameters- The configuration for the document's compression.
-
COSWriter
public COSWriter(java.io.OutputStream outputStream, RandomAccessRead inputData) throws java.io.IOExceptionCOSWriter constructor for incremental updates. There must be a path of objects that haveCOSUpdateInfo.isNeedToBeUpdated()set, starting from the document catalog. For signatures this is taken care by PDFBox itself.- Parameters:
outputStream- output stream where the new PDF data will be written.inputData- random access read containing source PDF data- Throws:
java.io.IOException- if something went wrong
-
COSWriter
public COSWriter(java.io.OutputStream outputStream, RandomAccessRead inputData, java.util.Set<COSDictionary> objectsToWrite) throws java.io.IOExceptionConstructor for incremental updates with a list of objects to write. This allows to include objects even if there is no path of objects that haveCOSUpdateInfo.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.- Parameters:
outputStream- output stream where the new PDF data will be written.inputData- random access read containing source PDF data.objectsToWrite- objects that must be part of the incremental saving.- Throws:
java.io.IOException- if something went wrong
-
-
Method Detail
-
isCompress
public boolean isCompress()
Returns true, if the resulting document shall be compressed.- Returns:
- True, if the resulting document shall be compressed.
-
prepareIncrement
private void prepareIncrement()
-
addXRefEntry
protected void addXRefEntry(XReferenceEntry entry)
add an entry in the x ref table for later dump.- Parameters:
entry- The new entry to add.
-
getOutput
protected java.io.OutputStream getOutput()
This will get the output stream.- Returns:
- The output stream.
-
getStandardOutput
protected COSStandardOutputStream getStandardOutput()
This will get the standard output stream.- Returns:
- The standard output stream.
-
getStartxref
protected long getStartxref()
This will get the current start xref.- Returns:
- The current start xref.
-
getXRefEntries
protected java.util.List<XReferenceEntry> getXRefEntries()
This will get the xref entries.- Returns:
- All available xref entries.
-
setOutput
private void setOutput(java.io.OutputStream newOutput)
This will set the output stream.- Parameters:
newOutput- The new output stream.
-
setStandardOutput
private void setStandardOutput(COSStandardOutputStream newStandardOutput)
This will set the standard output stream.- Parameters:
newStandardOutput- The new standard output stream.
-
setStartxref
protected void setStartxref(long newStartxref)
This will set the start xref.- Parameters:
newStartxref- The new start xref attribute.
-
doWriteBody
protected void doWriteBody(COSDocument doc) throws java.io.IOException
This will write the body of the document.- Parameters:
doc- The document to write the body for.- Throws:
java.io.IOException- If there is an error writing the data.
-
doWriteBodyCompressed
private void doWriteBodyCompressed(COSDocument document) throws java.io.IOException
This will write the compressed body of the document.- Parameters:
document- The document to write the body for.- Throws:
java.io.IOException- If there is an error writing the data.
-
doWriteObjects
private void doWriteObjects() throws java.io.IOException- Throws:
java.io.IOException
-
addObjectToWrite
private void addObjectToWrite(COSBase object)
-
doWriteObject
public void doWriteObject(COSObjectKey key, COSBase obj) throws java.io.IOException
This will write a COS object for a predefined key.- Parameters:
key- The key of the object to write.obj- The object to write.- Throws:
java.io.IOException- if the output cannot be written
-
isNeedToBeUpdated
private boolean isNeedToBeUpdated(COSBase base)
Convenience method, so that we get false for types that can't be updated.- Parameters:
base-- Returns:
-
doWriteObject
public void doWriteObject(COSBase obj) throws java.io.IOException
This will write a COS object.- Parameters:
obj- The object to write.- Throws:
java.io.IOException- if the output cannot be written
-
doWriteHeader
protected void doWriteHeader(COSDocument doc) throws java.io.IOException
This will write the header to the PDF document.- Parameters:
doc- The document to get the data from.- Throws:
java.io.IOException- If there is an error writing to the stream.
-
doWriteTrailer
protected void doWriteTrailer(COSDocument doc) throws java.io.IOException
This will write the trailer to the PDF document.- Parameters:
doc- The document to create the trailer for.- Throws:
java.io.IOException- If there is an IOError while writing the document.
-
doWriteXRefInc
private void doWriteXRefInc(COSDocument doc) throws java.io.IOException
- Throws:
java.io.IOException
-
doWriteXRefTable
private void doWriteXRefTable() throws java.io.IOException- Throws:
java.io.IOException
-
fillGapsWithFreeEntries
private void fillGapsWithFreeEntries()
-
doWriteIncrement
private void doWriteIncrement() throws java.io.IOExceptionWrite an incremental update for a non signature case. This can be used for e.g. augmenting signatures.- Throws:
java.io.IOException
-
doWriteSignature
private void doWriteSignature() throws java.io.IOException- Throws:
java.io.IOException
-
getDataToSign
public java.io.InputStream getDataToSign() throws java.io.IOExceptionReturn the stream of PDF data to be signed. Clients should use this method only to create signatures externally.write(PDDocument)method should have been called prior. The created signature should be set usingwriteExternalSignature(byte[]).When
SignatureInterfaceinstance is used, COSWriter obtains and writes the signature itself.- Returns:
- data stream to be signed
- Throws:
java.lang.IllegalStateException- if PDF is not prepared for external signingjava.io.IOException- if input data is closed
-
writeExternalSignature
public void writeExternalSignature(byte[] cmsSignature) throws java.io.IOExceptionWrite externally created signature of PDF data obtained viagetDataToSign()method.- Parameters:
cmsSignature- CMS signature byte array- Throws:
java.lang.IllegalStateException- if PDF is not prepared for external signingjava.io.IOException- if source data stream is closed
-
writeXrefRange
private void writeXrefRange(long x, long y) throws java.io.IOException- Throws:
java.io.IOException
-
writeXrefEntry
private void writeXrefEntry(XReferenceEntry entry) throws java.io.IOException
- Throws:
java.io.IOException
-
getXRefRanges
protected java.lang.Long[] getXRefRanges(java.util.List<XReferenceEntry> xRefEntriesList)
check the xref entries and write out the ranges. The format of the returned array is exactly the same as the pdf specification. See section 7.5.4 of ISO32000-1:2008, example 1 (page 40) for reference.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.
- Parameters:
xRefEntriesList- list with the xRef entries that was written- Returns:
- a integer array with the ranges
-
getObjectKey
private COSObjectKey getObjectKey(COSBase obj)
This will get the object key for the object.- Parameters:
obj- The object to get the key for.- Returns:
- The object key for the object.
-
visitFromArray
public void visitFromArray(COSArray array) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to Array object.- Specified by:
visitFromArrayin interfaceICOSVisitor- Parameters:
array- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
writeArray
private void writeArray(COSArray array) throws java.io.IOException
- Throws:
java.io.IOException
-
writeDictionary
private void writeDictionary(COSDictionary dictionary) throws java.io.IOException
- Throws:
java.io.IOException
-
visitFromBoolean
public void visitFromBoolean(COSBoolean obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to boolean object.- Specified by:
visitFromBooleanin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
visitFromDictionary
public void visitFromDictionary(COSDictionary obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to dictionary object.- Specified by:
visitFromDictionaryin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
detectPossibleSignature
private void detectPossibleSignature(COSDictionary obj) throws java.io.IOException
- Throws:
java.io.IOException
-
visitFromDocument
public void visitFromDocument(COSDocument doc) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to document object.- Specified by:
visitFromDocumentin interfaceICOSVisitor- Parameters:
doc- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
visitFromFloat
public void visitFromFloat(COSFloat obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to float object.- Specified by:
visitFromFloatin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
visitFromInt
public void visitFromInt(COSInteger obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to integer object.- Specified by:
visitFromIntin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
visitFromName
public void visitFromName(COSName obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to name object.- Specified by:
visitFromNamein interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
visitFromNull
public void visitFromNull(COSNull obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to null object.- Specified by:
visitFromNullin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
writeReference
public void writeReference(COSBase obj) throws java.io.IOException
visitFromObjRef method comment.- Parameters:
obj- The object that is being visited.- Throws:
java.io.IOException- If there is an exception while visiting this object.
-
visitFromStream
public void visitFromStream(COSStream obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to stream object.- Specified by:
visitFromStreamin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
visitFromString
public void visitFromString(COSString obj) throws java.io.IOException
Description copied from interface:ICOSVisitorNotification of visit to string object.- Specified by:
visitFromStringin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Throws:
java.io.IOException- If there is an error while visiting this object.
-
write
public void write(COSDocument doc) throws java.io.IOException
This will write the pdf document.- Parameters:
doc- The document to write.- Throws:
java.io.IOException- If an error occurs while generating the data.
-
write
public void write(PDDocument doc) throws java.io.IOException
This will write the pdf document. If signature should be created externally,writeExternalSignature(byte[])should be invoked to set signature after calling this method.- Parameters:
doc- The document to write.- Throws:
java.io.IOException- If an error occurs while generating the data.
-
write
public void write(PDDocument doc, SignatureInterface signInterface) throws java.io.IOException
This will write the pdf document. If signature should be created externally,writeExternalSignature(byte[])should be invoked to set signature after calling this method.- Parameters:
doc- The document to write.signInterface- class to be used for signing;nullif external signing would be performed or there will be no signing at all- Throws:
java.io.IOException- If an error occurs while generating the data.java.lang.IllegalStateException- If the document has an encryption dictionary but no protection policy.
-
write
public void write(FDFDocument doc) throws java.io.IOException
This will write the fdf document.- Parameters:
doc- The document to write.- Throws:
java.io.IOException- If an error occurs while generating the data.
-
writeString
public static void writeString(COSString string, java.io.OutputStream output) throws java.io.IOException
This will output the given byte getString as a PDF object.- Parameters:
string- COSString to be writtenoutput- The stream to write to.- Throws:
java.io.IOException- If there is an error writing to the stream.
-
writeString
public static void writeString(byte[] bytes, java.io.OutputStream output) throws java.io.IOExceptionThis will output the given text/byte getString as a PDF object.- Parameters:
bytes- byte array representation of a string to be writtenoutput- The stream to write to.- Throws:
java.io.IOException- If there is an error writing to the stream.
-
writeString
private static void writeString(byte[] bytes, boolean forceHex, java.io.OutputStream output) throws java.io.IOExceptionThis will output the given text/byte string as a PDF object.- Parameters:
output- The stream to write to.- Throws:
java.io.IOException- If there is an error writing to the stream.
-
-