Package com.itextpdf.kernel.pdf
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:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
- Direct Known Subclasses:
MemoryFirstPdfWriter
public class PdfWriter extends PdfOutputStream
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<PdfIndirectReference,PdfIndirectReference>
copiedObjects
Is used to avoid duplications on object copying.private static byte[]
ENDOBJ
protected boolean
isUserWarnedAboutAcroFormCopying
private static byte[]
OBJ
(package private) PdfObjectStream
objectStream
Currently active object stream.private java.io.OutputStream
originalOutputStream
protected WriterProperties
properties
private SmartModePdfObjectsSerializer
smartModeSerializer
Is used in smart mode to serialize and store serialized objects content.-
Fields inherited from class com.itextpdf.kernel.pdf.PdfOutputStream
crypto, document
-
Fields inherited from class com.itextpdf.io.source.HighPrecisionOutputStream
closeStream, currentPos, outputStream
-
-
Constructor Summary
Constructors Constructor Description PdfWriter(java.io.File file)
Create a PdfWriter writing to the passed File and with default writer properties.PdfWriter(java.io.OutputStream os)
Create a PdfWriter writing to the passed outputstream and with default writer properties.PdfWriter(java.io.OutputStream os, WriterProperties properties)
CreatesPdfWriter
instance, which writes to the passedOutputStream
, using providedWriterProperties
.PdfWriter(java.lang.String filename)
Create a PdfWriter writing to the passed filename and with default writer properties.PdfWriter(java.lang.String filename, WriterProperties properties)
Create a PdfWriter writing to the passed filename and using the passed writer properties.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean
checkTypeOfPdfDictionary(PdfObject dictionary, PdfName expectedType)
private void
completeByteArrayWritingMode()
protected PdfObject
copyObject(PdfObject obj, PdfDocument documentTo, boolean allowDuplicating)
Copies a PdfObject either stand alone or as part of the PdfDocument passed as documentTo.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.(package private) void
enableByteArrayWritingMode()
(package private) void
finish()
(package private) void
flushCopiedObjects(long docId)
Flush all copied objects.protected void
flushModifiedWaitingObjects(java.util.Set<PdfIndirectReference> forbiddenToFlush)
Flushes all modified objects which have not been flushed yet.protected void
flushObject(PdfObject pdfObject, boolean canBeInObjStm)
Flushes the object.protected void
flushWaitingObjects(java.util.Set<PdfIndirectReference> forbiddenToFlush)
Flushes all objects which have not been flushed yet.int
getCompressionLevel()
Gets default compression level for @see PdfStream.(package private) PdfObjectStream
getObjectStream()
Gets the current object stream.PdfVersion
getPdfVersion()
Gets defined pdf version for the document.WriterProperties
getProperties()
Gets the writer properties.protected void
initCryptoIfSpecified(PdfVersion version)
InitializesPdfEncryption
object if any encryption is specified inWriterProperties
.private boolean
isByteArrayWritingMode()
boolean
isFullCompression()
Indicates if to use full compression mode.private void
markArrayContentToFlush(PdfArray array)
private void
markDictionaryContentToFlush(PdfDictionary dictionary)
private void
markObjectToFlush(PdfObject pdfObject)
PdfWriter
setCompressionLevel(int compressionLevel)
Sets default compression level for @see PdfStream.PdfWriter
setSmartMode(boolean smartMode)
Sets the smart mode.protected void
writeHeader()
Writes PDF header.protected void
writeToBody(PdfObject pdfObj)
Writes object to body of PDF document.-
Methods inherited from class com.itextpdf.kernel.pdf.PdfOutputStream
checkEncryption, containsFlateFilter, decodeFlateBytes, updateCompressionFilter, write, write, write
-
Methods inherited from class com.itextpdf.io.source.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, writeString
-
-
-
-
Field Detail
-
OBJ
private static final byte[] OBJ
-
ENDOBJ
private static final byte[] ENDOBJ
-
properties
protected WriterProperties properties
-
isUserWarnedAboutAcroFormCopying
protected boolean isUserWarnedAboutAcroFormCopying
-
objectStream
PdfObjectStream objectStream
Currently active object stream. Objects are written to the object stream if fullCompression set to true.
-
copiedObjects
private final java.util.Map<PdfIndirectReference,PdfIndirectReference> 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
private final SmartModePdfObjectsSerializer smartModeSerializer
Is used in smart mode to serialize and store serialized objects content.
-
originalOutputStream
private java.io.OutputStream originalOutputStream
-
-
Constructor Detail
-
PdfWriter
public PdfWriter(java.io.File file) throws java.io.IOException
Create a PdfWriter writing to the passed File and with default writer properties.- Parameters:
file
- File to write to.- Throws:
java.io.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 reasonjava.io.IOException
-
PdfWriter
public PdfWriter(java.io.OutputStream os)
Create a PdfWriter writing to the passed outputstream and with default writer properties.- Parameters:
os
- Outputstream to write to.
-
PdfWriter
public PdfWriter(java.io.OutputStream os, WriterProperties properties)
CreatesPdfWriter
instance, which writes to the passedOutputStream
, using providedWriterProperties
.- Parameters:
os
-OutputStream
in which writing should happenproperties
-WriterProperties
to be used during the writing
-
PdfWriter
public PdfWriter(java.lang.String filename) throws java.io.IOException
Create a PdfWriter writing to the passed filename and with default writer properties.- Parameters:
filename
- filename of the resulting pdf.- Throws:
java.io.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 reasonjava.io.IOException
-
PdfWriter
public PdfWriter(java.lang.String filename, WriterProperties properties) throws java.io.IOException
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:
java.io.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 reasonjava.io.IOException
-
-
Method Detail
-
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
public PdfWriter setCompressionLevel(int compressionLevel)
Sets default compression level for @see PdfStream. For more details @seeDeflaterOutputStream
.- Parameters:
compressionLevel
- compression level.- Returns:
- this
PdfWriter
instance
-
getPdfVersion
public PdfVersion getPdfVersion()
Gets defined pdf version for the document.- Returns:
- version for the document
-
getProperties
public WriterProperties getProperties()
Gets the writer properties.- Returns:
- The
WriterProperties
of the current PdfWriter.
-
setSmartMode
public PdfWriter setSmartMode(boolean smartMode)
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
PdfWriter
instance
-
initCryptoIfSpecified
protected void initCryptoIfSpecified(PdfVersion version)
InitializesPdfEncryption
object if any encryption is specified inWriterProperties
.- Parameters:
version
-PdfVersion
version of the document in question
-
flushObject
protected void flushObject(PdfObject pdfObject, boolean canBeInObjStm)
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
protected PdfObject copyObject(PdfObject obj, PdfDocument documentTo, boolean allowDuplicating)
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
-ICopyFilter
a filter to apply while copying arrays and dictionaries * UseNullCopyFilter
for no filtering- Returns:
- the copies object
-
writeToBody
protected void writeToBody(PdfObject pdfObj)
Writes object to body of PDF document.- Parameters:
pdfObj
- object to write.
-
writeHeader
protected void writeHeader()
Writes PDF header.
-
flushWaitingObjects
protected void flushWaitingObjects(java.util.Set<PdfIndirectReference> forbiddenToFlush)
Flushes all objects which have not been flushed yet.- Parameters:
forbiddenToFlush
- aSet
ofreferences
that are forbidden to be flushed automatically.
-
flushModifiedWaitingObjects
protected void flushModifiedWaitingObjects(java.util.Set<PdfIndirectReference> forbiddenToFlush)
Flushes all modified objects which have not been flushed yet. Used in case incremental updates.- Parameters:
forbiddenToFlush
- aSet
ofreferences
that are forbidden to be flushed automatically.
-
finish
void finish() throws java.io.IOException
- Throws:
java.io.IOException
-
getObjectStream
PdfObjectStream getObjectStream()
Gets the current object stream.- Returns:
- object stream.
-
flushCopiedObjects
void flushCopiedObjects(long docId)
Flush all copied objects.- Parameters:
docId
- id of the source document
-
enableByteArrayWritingMode
void enableByteArrayWritingMode()
-
completeByteArrayWritingMode
private void completeByteArrayWritingMode() throws java.io.IOException
- Throws:
java.io.IOException
-
isByteArrayWritingMode
private boolean isByteArrayWritingMode()
-
markArrayContentToFlush
private void markArrayContentToFlush(PdfArray array)
-
markDictionaryContentToFlush
private void markDictionaryContentToFlush(PdfDictionary dictionary)
-
markObjectToFlush
private void markObjectToFlush(PdfObject pdfObject)
-
-