Class PdfWriter
java.lang.Object
java.io.OutputStream
com.itextpdf.io.source.HighPrecisionOutputStream<PdfOutputStream>
com.itextpdf.kernel.pdf.PdfOutputStream
com.itextpdf.kernel.pdf.PdfWriter
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
- Direct Known Subclasses:
MemoryFirstPdfWriter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<PdfIndirectReference, PdfIndirectReference> Is used to avoid duplications on object copying.private static final byte[]protected booleanprivate static final byte[](package private) PdfObjectStreamCurrently active object stream.private OutputStreamprotected WriterPropertiesprivate final SmartModePdfObjectsSerializerIs used in smart mode to serialize and store serialized objects content.Fields inherited from class PdfOutputStream
crypto, documentFields inherited from class HighPrecisionOutputStream
closeStream, currentPos, outputStream -
Constructor Summary
ConstructorsConstructorDescriptionCreate a PdfWriter writing to the passed File and with default writer properties.Create a PdfWriter writing to the passed outputstream and with default writer properties.PdfWriter(OutputStream os, WriterProperties properties) CreatesPdfWriterinstance, which writes to the passedOutputStream, using providedWriterProperties.Create a PdfWriter writing to the passed filename and with default writer properties.PdfWriter(String filename, WriterProperties properties) Create a PdfWriter writing to the passed filename and using the passed writer properties. -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleancheckTypeOfPdfDictionary(PdfObject dictionary, PdfName expectedType) private voidprotected PdfObjectcopyObject(PdfObject obj, PdfDocument documentTo, boolean allowDuplicating) Copies a PdfObject either stand alone or as part of the PdfDocument passed as documentTo.protected PdfObjectcopyObject(PdfObject obj, PdfDocument documentTo, boolean allowDuplicating, ICopyFilter copyFilter) Copies a PdfObject either stand alone or as part of the PdfDocument passed as documentTo.(package private) void(package private) voidfinish()(package private) voidflushCopiedObjects(long docId) Flush all copied objects.protected voidflushModifiedWaitingObjects(Set<PdfIndirectReference> forbiddenToFlush) Flushes all modified objects which have not been flushed yet.protected voidflushObject(PdfObject pdfObject, boolean canBeInObjStm) Flushes the object.protected voidflushWaitingObjects(Set<PdfIndirectReference> forbiddenToFlush) Flushes all objects which have not been flushed yet.intGets default compression level for @see PdfStream.(package private) PdfObjectStreamGets the current object stream.Gets defined pdf version for the document.Gets the writer properties.protected voidinitCryptoIfSpecified(PdfVersion version) InitializesPdfEncryptionobject if any encryption is specified inWriterProperties.private booleanbooleanIndicates if to use full compression mode.private voidmarkArrayContentToFlush(PdfArray array) private voidmarkDictionaryContentToFlush(PdfDictionary dictionary) private voidmarkObjectToFlush(PdfObject pdfObject) setCompressionLevel(int compressionLevel) Sets default compression level for @see PdfStream.setSmartMode(boolean smartMode) Sets the smart mode.protected voidWrites PDF header.protected voidwriteToBody(PdfObject pdfObj) Writes object to body of PDF document.Methods inherited from class PdfOutputStream
checkEncryption, containsFlateFilter, decodeFlateBytes, updateCompressionFilter, write, write, writeMethods inherited from class HighPrecisionOutputStream
assignBytes, close, flush, getCurrentPos, getHighPrecision, getLocalHighPrecision, getOutputStream, isCloseStream, reset, setCloseStream, setHighPrecision, setLocalHighPrecision, write, write, write, writeByte, writeByte, writeBytes, writeBytes, writeDouble, writeDouble, writeFloat, writeFloat, writeFloats, writeInteger, writeLong, writeNewLine, writeSpace, writeStringMethods inherited from class OutputStream
nullOutputStream
-
Field Details
-
OBJ
private static final byte[] OBJ -
ENDOBJ
private static final byte[] ENDOBJ -
properties
-
isUserWarnedAboutAcroFormCopying
protected boolean isUserWarnedAboutAcroFormCopying -
objectStream
PdfObjectStream objectStreamCurrently active object stream. Objects are written to the object stream if fullCompression set to true. -
copiedObjects
Is used to avoid duplications on object copying. It stores hashes of the indirect reference from the source document and the corresponding indirect references of the copied objects from the new document. -
smartModeSerializer
Is used in smart mode to serialize and store serialized objects content. -
originalOutputStream
-
-
Constructor Details
-
PdfWriter
Create a PdfWriter writing to the passed File and with default writer properties.- Parameters:
file- File to write to.- Throws:
FileNotFoundException- if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reasonIOException
-
PdfWriter
Create a PdfWriter writing to the passed outputstream and with default writer properties.- Parameters:
os- Outputstream to write to.
-
PdfWriter
CreatesPdfWriterinstance, which writes to the passedOutputStream, using providedWriterProperties.- Parameters:
os-OutputStreamin which writing should happenproperties-WriterPropertiesto be used during the writing
-
PdfWriter
Create a PdfWriter writing to the passed filename and with default writer properties.- Parameters:
filename- filename of the resulting pdf.- Throws:
FileNotFoundException- if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reasonIOException
-
PdfWriter
Create a PdfWriter writing to the passed filename and using the passed writer properties.- Parameters:
filename- filename of the resulting pdf.properties- writerproperties to use.- Throws:
FileNotFoundException- if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reasonIOException
-
-
Method Details
-
isFullCompression
public boolean isFullCompression()Indicates if to use full compression mode.- Returns:
- true if to use full compression, false otherwise.
-
getCompressionLevel
public int getCompressionLevel()Gets default compression level for @see PdfStream. For more details @seeDeflaterOutputStream.- Returns:
- compression level.
-
setCompressionLevel
Sets default compression level for @see PdfStream. For more details @seeDeflaterOutputStream.- Parameters:
compressionLevel- compression level.- Returns:
- this
PdfWriterinstance
-
getPdfVersion
Gets defined pdf version for the document.- Returns:
- version for the document
-
getProperties
Gets the writer properties.- Returns:
- The
WriterPropertiesof the current PdfWriter.
-
setSmartMode
Sets the smart mode.
In smart mode when resources (such as fonts, images,...) are encountered, a reference to these resources is saved in a cache, so that they can be reused. This requires more memory, but reduces the file size of the resulting PDF document.- Parameters:
smartMode- True for enabling smart mode.- Returns:
- this
PdfWriterinstance
-
initCryptoIfSpecified
InitializesPdfEncryptionobject if any encryption is specified inWriterProperties.- Parameters:
version-PdfVersionversion of the document in question
-
flushObject
Flushes the object. Override this method if you want to define custom behaviour for object flushing.- Parameters:
pdfObject- object to flush.canBeInObjStm- indicates whether object can be placed into object stream.
-
copyObject
Copies a PdfObject either stand alone or as part of the PdfDocument passed as documentTo.- Parameters:
obj- object to copydocumentTo- optional target documentallowDuplicating- allow that some objects will become duplicated by this action- Returns:
- the copies object
-
copyObject
protected PdfObject copyObject(PdfObject obj, PdfDocument documentTo, boolean allowDuplicating, ICopyFilter copyFilter) Copies a PdfObject either stand alone or as part of the PdfDocument passed as documentTo.- Parameters:
obj- object to copydocumentTo- optional target documentallowDuplicating- allow that some objects will become duplicated by this actioncopyFilter-ICopyFiltera filter to apply while copying arrays and dictionaries * UseNullCopyFilterfor no filtering- Returns:
- the copies object
-
writeToBody
Writes object to body of PDF document.- Parameters:
pdfObj- object to write.
-
writeHeader
protected void writeHeader()Writes PDF header. -
flushWaitingObjects
Flushes all objects which have not been flushed yet.- Parameters:
forbiddenToFlush- aSetofreferencesthat are forbidden to be flushed automatically.
-
flushModifiedWaitingObjects
Flushes all modified objects which have not been flushed yet. Used in case incremental updates.- Parameters:
forbiddenToFlush- aSetofreferencesthat are forbidden to be flushed automatically.
-
finish
- Throws:
IOException
-
getObjectStream
-
flushCopiedObjects
void flushCopiedObjects(long docId) Flush all copied objects.- Parameters:
docId- id of the source document
-
enableByteArrayWritingMode
void enableByteArrayWritingMode() -
completeByteArrayWritingMode
- Throws:
IOException
-
isByteArrayWritingMode
private boolean isByteArrayWritingMode() -
markArrayContentToFlush
-
markDictionaryContentToFlush
-
markObjectToFlush
-
checkTypeOfPdfDictionary
-