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.
todo no support for incremental updates
todo single xref section only
todo no linearization
- Author:
- Michael Traut, Ben Litchfield
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byte[]
The array close token.static final byte[]
The array open token.static final byte[]
The start to a PDF comment.static 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.static final byte[]
Garbage bytes used to create the PDF header.static final byte[]
The starting object token.static final byte[]
The reference token.static final byte[]
space character.static 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.static final byte[]
The XREF token.static final byte[]
The xref free token.static final byte[]
The xref used token. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor.COSWriter
(OutputStream os, InputStream is) COSWriter constructor for incremental updates. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addXRefEntry
(COSWriterXRefEntry entry) add an entry in the x ref table for later dump.void
close()
This will close the stream.protected void
doWriteBody
(COSDocument doc) This will write the body of the document.protected void
doWriteHeader
(COSDocument doc) This will write the header to the PDF document.void
doWriteObject
(COSBase obj) This will write a COS object.protected void
This will write the trailer to the PDF document.protected void
doWriteXRef
(COSDocument doc) write the x ref section for the pdf file currently, the pdf is reconstructed from the scratch, so we write a single section todo support for incremental writing?protected long
This will get the current object number.This will get all available object keys.protected OutputStream
This will get the output stream.protected COSStandardOutputStream
This will get the standard output stream.protected long
This will get the current start xref.protected List<COSWriterXRefEntry>
This will get the xref entries.protected Integer[]
getXRefRanges
(List<COSWriterXRefEntry> xRefEntriesList) check the xref entries and write out the ranges.protected void
setNumber
(long newNumber) This will set the current object number.protected void
setStartxref
(long newStartxref) This will set the start xref.visitFromArray
(COSArray obj) visitFromArray method comment.visitFromBoolean method comment.visitFromDictionary method comment.The visit from document method.visitFromFloat
(COSFloat obj) visitFromFloat method comment.visitFromInt
(COSInteger obj) visitFromFloat method comment.visitFromName
(COSName obj) visitFromName method comment.visitFromNull
(COSNull obj) visitFromNull method comment.visitFromStream
(COSStream obj) visitFromStream method comment.visitFromString
(COSString obj) visitFromString method comment.void
write
(COSDocument doc) This will write the pdf document.void
write
(PDDocument doc) This will write the pdf document.void
writeReference
(COSBase obj) visitFromObjRef method comment.
-
Field Details
-
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.
-
-
Constructor Details
-
COSWriter
Constructor.- Parameters:
os
- The wrapped output stream.
-
COSWriter
COSWriter constructor for incremental updates.- Parameters:
os
- The wrapped output stream.is
- input stream
-
-
Method Details
-
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:
close
in interfaceAutoCloseable
- Specified by:
close
in 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.
-
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.COSVisitorException
- If there is an error generating the data.
-
doWriteObject
This will write a COS object.- Parameters:
obj
- The object to write.- Throws:
COSVisitorException
- If there is an error visiting objects.
-
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.COSVisitorException
- If there is an error while generating the data.
-
doWriteXRef
write the x ref section for the pdf file currently, the pdf is reconstructed from the scratch, so we write a single section todo support for incremental writing?- Parameters:
doc
- The document to write the xref from.- Throws:
IOException
- If there is an error writing the data to the stream.
-
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
-
visitFromArray
visitFromArray method comment.- Specified by:
visitFromArray
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromBoolean
visitFromBoolean method comment.- Specified by:
visitFromBoolean
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromDictionary
visitFromDictionary method comment.- Specified by:
visitFromDictionary
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromDocument
The visit from document method.- Specified by:
visitFromDocument
in interfaceICOSVisitor
- Parameters:
doc
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromFloat
visitFromFloat method comment.- Specified by:
visitFromFloat
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromInt
visitFromFloat method comment.- Specified by:
visitFromInt
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromName
visitFromName method comment.- Specified by:
visitFromName
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromNull
visitFromNull method comment.- Specified by:
visitFromNull
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
writeReference
visitFromObjRef method comment.- Parameters:
obj
- The object that is being visited.- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromStream
visitFromStream method comment.- Specified by:
visitFromStream
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
visitFromString
visitFromString method comment.- Specified by:
visitFromString
in interfaceICOSVisitor
- Parameters:
obj
- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException
- If there is an exception while visiting this object.
-
write
This will write the pdf document.- Parameters:
doc
- The document to write.- Throws:
COSVisitorException
- If an error occurs while generating the data.
-
write
This will write the pdf document.- Parameters:
doc
- The document to write.- Throws:
COSVisitorException
- If an error occurs while generating the data.IllegalStateException
- If the document has an encryption dictionary but no protection policy.
-