Package org.apache.pdfbox.pdfwriter
Class COSWriter
- java.lang.Object
-
- org.apache.pdfbox.pdfwriter.COSWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,ICOSVisitor
public class COSWriter extends java.lang.Object implements ICOSVisitor, java.io.Closeable
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
Fields Modifier and Type Field Description static byte[]ARRAY_CLOSEThe array close token.static byte[]ARRAY_OPENThe array open token.static byte[]COMMENTThe start to a PDF comment.static 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.static byte[]GARBAGEGarbage bytes used to create the PDF header.static byte[]OBJThe starting object token.static byte[]REFERENCEThe reference token.static byte[]SPACEspace character.static byte[]STARTXREFThe start xref token.static byte[]STREAMThe open stream token.static byte[]TRAILERThe trailer token.static byte[]VERSIONThe output version of the PDF.static byte[]XREFThe XREF token.static byte[]XREF_FREEThe xref free token.static byte[]XREF_USEDThe xref used token.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddXRefEntry(COSWriterXRefEntry entry)add an entry in the x ref table for later dump.voidclose()This will close the stream.protected voiddoWriteBody(COSDocument doc)This will write the body of the document.protected voiddoWriteHeader(COSDocument doc)This will write the header to the PDF document.voiddoWriteObject(COSBase obj)This will write a COS object.protected voiddoWriteTrailer(COSDocument doc)This will write the trailer to the PDF document.protected voiddoWriteXRef(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 longgetNumber()This will get the current object number.java.util.Map<COSBase,COSObjectKey>getObjectKeys()This will get all available object keys.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<COSWriterXRefEntry>getXRefEntries()This will get the xref entries.protected java.lang.Integer[]getXRefRanges(java.util.List<COSWriterXRefEntry> xRefEntriesList)check the xref entries and write out the ranges.protected voidsetNumber(long newNumber)This will set the current object number.protected voidsetStartxref(long newStartxref)This will set the start xref.java.lang.ObjectvisitFromArray(COSArray obj)visitFromArray method comment.java.lang.ObjectvisitFromBoolean(COSBoolean obj)visitFromBoolean method comment.java.lang.ObjectvisitFromDictionary(COSDictionary obj)visitFromDictionary method comment.java.lang.ObjectvisitFromDocument(COSDocument doc)The visit from document method.java.lang.ObjectvisitFromFloat(COSFloat obj)visitFromFloat method comment.java.lang.ObjectvisitFromInt(COSInteger obj)visitFromFloat method comment.java.lang.ObjectvisitFromName(COSName obj)visitFromName method comment.java.lang.ObjectvisitFromNull(COSNull obj)visitFromNull method comment.java.lang.ObjectvisitFromStream(COSStream obj)visitFromStream method comment.java.lang.ObjectvisitFromString(COSString obj)visitFromString method comment.voidwrite(COSDocument doc)This will write the pdf document.voidwrite(PDDocument doc)This will write the pdf document.voidwriteReference(COSBase obj)visitFromObjRef method comment.
-
-
-
Field Detail
-
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.
-
-
Constructor Detail
-
COSWriter
public COSWriter(java.io.OutputStream os)
Constructor.- Parameters:
os- The wrapped output stream.
-
COSWriter
public COSWriter(java.io.OutputStream os, java.io.InputStream is)COSWriter constructor for incremental updates.- Parameters:
os- The wrapped output stream.is- input stream
-
-
Method Detail
-
addXRefEntry
protected void addXRefEntry(COSWriterXRefEntry entry)
add an entry in the x ref table for later dump.- Parameters:
entry- The new entry to add.
-
close
public void close() throws java.io.IOExceptionThis will close the stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.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
public java.util.Map<COSBase,COSObjectKey> getObjectKeys()
This will get all available object keys.- Returns:
- A map of all object keys.
-
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<COSWriterXRefEntry> 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
protected void doWriteBody(COSDocument doc) throws java.io.IOException, COSVisitorException
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.COSVisitorException- If there is an error generating the data.
-
doWriteObject
public void doWriteObject(COSBase obj) throws COSVisitorException
This will write a COS object.- Parameters:
obj- The object to write.- Throws:
COSVisitorException- If there is an error visiting objects.
-
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, COSVisitorException
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.COSVisitorException- If there is an error while generating the data.
-
doWriteXRef
protected void doWriteXRef(COSDocument doc) throws java.io.IOException
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:
java.io.IOException- If there is an error writing the data to the stream.
-
getXRefRanges
protected java.lang.Integer[] getXRefRanges(java.util.List<COSWriterXRefEntry> 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
-
visitFromArray
public java.lang.Object visitFromArray(COSArray obj) throws COSVisitorException
visitFromArray method comment.- Specified by:
visitFromArrayin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromBoolean
public java.lang.Object visitFromBoolean(COSBoolean obj) throws COSVisitorException
visitFromBoolean method comment.- Specified by:
visitFromBooleanin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromDictionary
public java.lang.Object visitFromDictionary(COSDictionary obj) throws COSVisitorException
visitFromDictionary method comment.- Specified by:
visitFromDictionaryin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromDocument
public java.lang.Object visitFromDocument(COSDocument doc) throws COSVisitorException
The visit from document method.- Specified by:
visitFromDocumentin interfaceICOSVisitor- Parameters:
doc- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromFloat
public java.lang.Object visitFromFloat(COSFloat obj) throws COSVisitorException
visitFromFloat method comment.- Specified by:
visitFromFloatin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromInt
public java.lang.Object visitFromInt(COSInteger obj) throws COSVisitorException
visitFromFloat method comment.- Specified by:
visitFromIntin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromName
public java.lang.Object visitFromName(COSName obj) throws COSVisitorException
visitFromName method comment.- Specified by:
visitFromNamein interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromNull
public java.lang.Object visitFromNull(COSNull obj) throws COSVisitorException
visitFromNull method comment.- Specified by:
visitFromNullin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
writeReference
public void writeReference(COSBase obj) throws COSVisitorException
visitFromObjRef method comment.- Parameters:
obj- The object that is being visited.- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromStream
public java.lang.Object visitFromStream(COSStream obj) throws COSVisitorException
visitFromStream method comment.- Specified by:
visitFromStreamin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
visitFromString
public java.lang.Object visitFromString(COSString obj) throws COSVisitorException
visitFromString method comment.- Specified by:
visitFromStringin interfaceICOSVisitor- Parameters:
obj- The object that is being visited.- Returns:
- null
- Throws:
COSVisitorException- If there is an exception while visiting this object.
-
write
public void write(COSDocument doc) throws COSVisitorException
This will write the pdf document.- Parameters:
doc- The document to write.- Throws:
COSVisitorException- If an error occurs while generating the data.
-
write
public void write(PDDocument doc) throws COSVisitorException
This will write the pdf document.- Parameters:
doc- The document to write.- Throws:
COSVisitorException- If an error occurs while generating the data.java.lang.IllegalStateException- If the document has an encryption dictionary but no protection policy.
-
-