Package org.apache.pdfbox.pdfwriter
Class COSWriter
java.lang.Object
org.apache.pdfbox.pdfwriter.COSWriter
- All Implemented Interfaces:
Closeable,AutoCloseable,ICOSVisitor
This class acts on a in-memory representation of a PDF document.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byte[]The array close token.static final byte[]The array open token.private COSArrayprivate longprivate longstatic final byte[]The start to a PDF comment.private COSObjectKeystatic final byte[]The dictionary close token.static final byte[]The dictionary open token.static final byte[]The end object token.static final byte[]The close stream token.static final byte[]The EOF constant.private FDFDocumentprivate final NumberFormatprivate final NumberFormatstatic final byte[]Garbage bytes used to create the PDF header.private RandomAccessReadprivate OutputStreamprivate booleanprivate byte[]private final Map<COSObjectKey,COSBase> private static final org.apache.commons.logging.Logprivate longstatic final byte[]The starting object token.private final Map<COSBase,COSObjectKey> private OutputStreamprivate PDDocumentprivate booleanstatic final byte[]The reference token.private SignatureInterfaceprivate longprivate longstatic final byte[]space character.private COSStandardOutputStreamprivate longstatic final byte[]The start xref token.static final byte[]The open stream token.static final byte[]The trailer token.static final byte[]The output version of the PDF.private booleanstatic final byte[]The XREF token.static final byte[]The xref free token.static final byte[]The xref used token.private final List<COSWriterXRefEntry> -
Constructor Summary
ConstructorsConstructorDescriptionCOSWriter(OutputStream outputStream) COSWriter constructor.COSWriter(OutputStream outputStream, RandomAccessRead inputData) COSWriter constructor for incremental updates.COSWriter(OutputStream outputStream, RandomAccessRead inputData, Set<COSDictionary> objectsToWrite) Constructor for incremental updates with a list of objects to write. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddObjectToWrite(COSBase object) protected voidaddXRefEntry(COSWriterXRefEntry entry) add an entry in the x ref table for later dump.voidclose()This will close the stream.private voidprotected voiddoWriteBody(COSDocument doc) This will write the body of the document.protected voiddoWriteHeader(COSDocument doc) This will write the header to the PDF document.private voidWrite an incremental update for a non signature case.voiddoWriteObject(COSBase obj) This will write a COS object.private voidprivate voidprotected voidThis will write the trailer to the PDF document.private voiddoWriteXRefInc(COSDocument doc, long hybridPrev) private voidReturn the stream of PDF data to be signed.protected longThis will get the current object number.private COSObjectKeygetObjectKey(COSBase obj) This will get the object key for the object.This will get all available object keys.protected OutputStreamThis will get the output stream.protected COSStandardOutputStreamThis will get the standard output stream.protected longThis will get the current start xref.protected List<COSWriterXRefEntry>This will get the xref entries.protected Long[]getXRefRanges(List<COSWriterXRefEntry> xRefEntriesList) check the xref entries and write out the ranges.private booleanisNeedToBeUpdated(COSBase base) Convenience method, so that we get false for types that can't be updated.private voidprotected voidsetNumber(long newNumber) This will set the current object number.private voidsetOutput(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.visitFromArray(COSArray obj) Notification of visit to Array object.Notification of visit to boolean object.Notification of visit to dictionary object.Notification of visit to document object.visitFromFloat(COSFloat obj) Notification of visit to float object.visitFromInt(COSInteger obj) Notification of visit to integer object.visitFromName(COSName obj) Notification of visit to name object.visitFromNull(COSNull obj) Notification of visit to null object.visitFromStream(COSStream obj) Notification of visit to stream object.visitFromString(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.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, OutputStream output) This will output the given text/byte string as a PDF object.static voidwriteString(byte[] bytes, OutputStream output) This will output the given text/byte getString as a PDF object.static voidwriteString(COSString string, OutputStream output) This will output the given byte getString as a PDF object.private voidwriteXrefEntry(COSWriterXRefEntry entry) private voidwriteXrefRange(long x, long y)
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
DICT_OPEN
public static final byte[] DICT_OPENThe dictionary open token. -
DICT_CLOSE
public static final byte[] DICT_CLOSEThe dictionary close token. -
SPACE
public static final byte[] SPACEspace character. -
COMMENT
public static final byte[] COMMENTThe start to a PDF comment. -
VERSION
public static final byte[] VERSIONThe output version of the PDF. -
GARBAGE
public static final byte[] GARBAGEGarbage bytes used to create the PDF header. -
EOF
public static final byte[] EOFThe EOF constant. -
REFERENCE
public static final byte[] REFERENCEThe reference token. -
XREF
public static final byte[] XREFThe XREF token. -
XREF_FREE
public static final byte[] XREF_FREEThe xref free token. -
XREF_USED
public static final byte[] XREF_USEDThe xref used token. -
TRAILER
public static final byte[] TRAILERThe trailer token. -
STARTXREF
public static final byte[] STARTXREFThe start xref token. -
OBJ
public static final byte[] OBJThe starting object token. -
ENDOBJ
public static final byte[] ENDOBJThe end object token. -
ARRAY_OPEN
public static final byte[] ARRAY_OPENThe array open token. -
ARRAY_CLOSE
public static final byte[] ARRAY_CLOSEThe array close token. -
STREAM
public static final byte[] STREAMThe open stream token. -
ENDSTREAM
public static final byte[] ENDSTREAMThe close stream token. -
formatXrefOffset
-
formatXrefGeneration
-
output
-
standardOutput
-
startxref
private long startxref -
number
private long number -
objectKeys
-
keyObject
-
xRefEntries
-
objectsToWriteSet
-
objectsToWrite
-
writtenObjects
-
actualsAdded
-
currentObjectKey
-
pdDocument
-
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
-
incrementalOutput
-
signatureInterface
-
incrementPart
private byte[] incrementPart -
byteRangeArray
-
-
Constructor Details
-
COSWriter
COSWriter constructor.- Parameters:
outputStream- The output stream to write the PDF. It will be closed when this object is closed.
-
COSWriter
COSWriter 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. It will be closed when this object is closed.inputData- random access read containing source PDF data- Throws:
IOException- if something went wrong
-
COSWriter
public COSWriter(OutputStream outputStream, RandomAccessRead inputData, Set<COSDictionary> objectsToWrite) throws IOException Constructor 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. 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.- Throws:
IOException- if something went wrong
-
-
Method Details
-
prepareIncrement
-
addXRefEntry
add an entry in the x ref table for later dump.- Parameters:
entry- The new entry to add.
-
close
This will close the stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- If the underlying stream throws an exception.
-
getNumber
protected long getNumber()This will get the current object number.- Returns:
- The current object number.
-
getObjectKeys
This will get all available object keys.- Returns:
- A map of all object keys.
-
getOutput
This will get the output stream.- Returns:
- The output stream.
-
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
This will get the xref entries.- Returns:
- All available xref entries.
-
setNumber
protected void setNumber(long newNumber) This will set the current object number.- Parameters:
newNumber- The new object number.
-
setOutput
This will set the output stream.- Parameters:
newOutput- The new output stream.
-
setStandardOutput
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
This will write the body of the document.- Parameters:
doc- The document to write the body for.- Throws:
IOException- If there is an error writing the data.
-
doWriteObjects
- Throws:
IOException
-
addObjectToWrite
-
isNeedToBeUpdated
Convenience method, so that we get false for types that can't be updated.- Parameters:
base-- Returns:
-
doWriteObject
This will write a COS object.- Parameters:
obj- The object to write.- Throws:
IOException- if the output cannot be written
-
doWriteHeader
This will write the header to the PDF document.- Parameters:
doc- The document to get the data from.- Throws:
IOException- If there is an error writing to the stream.
-
doWriteTrailer
This will write the trailer to the PDF document.- Parameters:
doc- The document to create the trailer for.- Throws:
IOException- If there is an IOError while writing the document.
-
doWriteXRefInc
- Throws:
IOException
-
doWriteXRefTable
- Throws:
IOException
-
doWriteIncrement
Write an incremental update for a non signature case. This can be used for e.g. augmenting signatures.- Throws:
IOException
-
doWriteSignature
- Throws:
IOException
-
getDataToSign
Return 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:
IllegalStateException- if PDF is not prepared for external signingIOException- if input data is closed
-
writeExternalSignature
Write externally created signature of PDF data obtained viagetDataToSign()method.- Parameters:
cmsSignature- CMS signature byte array- Throws:
IllegalStateException- if PDF is not prepared for external signingIOException- if source data stream is closed
-
writeXrefRange
- Throws:
IOException
-
writeXrefEntry
- Throws:
IOException
-
getXRefRanges
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
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
Description copied from interface:ICOSVisitorNotification of visit to Array object.- Specified by:
visitFromArrayin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
visitFromBoolean
Description copied from interface:ICOSVisitorNotification of visit to boolean object.- Specified by:
visitFromBooleanin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
visitFromDictionary
Description copied from interface:ICOSVisitorNotification of visit to dictionary object.- Specified by:
visitFromDictionaryin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
detectPossibleSignature
- Throws:
IOException
-
visitFromDocument
Description copied from interface:ICOSVisitorNotification of visit to document object.- Specified by:
visitFromDocumentin interfaceICOSVisitor- Parameters:
doc- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
visitFromFloat
Description copied from interface:ICOSVisitorNotification of visit to float object.- Specified by:
visitFromFloatin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
visitFromInt
Description copied from interface:ICOSVisitorNotification of visit to integer object.- Specified by:
visitFromIntin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
visitFromName
Description copied from interface:ICOSVisitorNotification of visit to name object.- Specified by:
visitFromNamein interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
visitFromNull
Description copied from interface:ICOSVisitorNotification of visit to null object.- Specified by:
visitFromNullin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
writeReference
visitFromObjRef method comment.- Parameters:
obj- The object that is being visited.- Throws:
IOException- If there is an exception while visiting this object.
-
visitFromStream
Description copied from interface:ICOSVisitorNotification of visit to stream object.- Specified by:
visitFromStreamin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
visitFromString
Description copied from interface:ICOSVisitorNotification of visit to string object.- Specified by:
visitFromStringin interfaceICOSVisitor- Parameters:
obj- The Object that is being visited.- Returns:
- any Object depending on the visitor implementation, or null
- Throws:
IOException- If there is an error while visiting this object.
-
write
This will write the pdf document.- Parameters:
doc- The document to write.- Throws:
IOException- If an error occurs while generating the data.
-
write
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:
IOException- If an error occurs while generating the data.
-
write
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:
IOException- If an error occurs while generating the data.IllegalStateException- If the document has an encryption dictionary but no protection policy.
-
write
This will write the fdf document.- Parameters:
doc- The document to write.- Throws:
IOException- If an error occurs while generating the data.
-
writeString
This will output the given byte getString as a PDF object.- Parameters:
string- COSString to be writtenoutput- The stream to write to.- Throws:
IOException- If there is an error writing to the stream.
-
writeString
This 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:
IOException- If there is an error writing to the stream.
-
writeString
private static void writeString(byte[] bytes, boolean forceHex, OutputStream output) throws IOException This will output the given text/byte string as a PDF object.- Parameters:
output- The stream to write to.- Throws:
IOException- If there is an error writing to the stream.
-