Class PdfWriter

java.lang.Object
org.openpdf.text.DocWriter
org.openpdf.text.pdf.PdfWriter
All Implemented Interfaces:
AutoCloseable, EventListener, DocListener, ElementListener, PdfRunDirection, PdfVersion

public class PdfWriter extends DocWriter implements PdfVersion, PdfRunDirection
A DocWriter class for PDF.

When this PdfWriter is added to a certain PdfDocument, the PDF representation of every Element added to this Document will be written to the outputstream.

  • Field Details

    • GENERATION_MAX

      public static final int GENERATION_MAX
      The highest generation number possible.
      Since:
      iText 2.1.6
      See Also:
    • VERSION_1_2

      public static final String VERSION_1_2
      possible PDF version (header)
      See Also:
    • VERSION_1_3

      public static final String VERSION_1_3
      possible PDF version (header)
      See Also:
    • VERSION_1_4

      public static final String VERSION_1_4
      possible PDF version (header)
      See Also:
    • VERSION_1_5

      public static final String VERSION_1_5
      possible PDF version (header)
      See Also:
    • VERSION_1_6

      public static final String VERSION_1_6
      possible PDF version (header)
      See Also:
    • VERSION_1_7

      public static final String VERSION_1_7
      possible PDF version (header)
      See Also:
    • VERSION_2_0

      public static final String VERSION_2_0
      PDF version 2.0.
      See Also:
    • PDF_VERSION_1_2

      public static final PdfName PDF_VERSION_1_2
      possible PDF version (catalog)
    • PDF_VERSION_1_3

      public static final PdfName PDF_VERSION_1_3
      possible PDF version (catalog)
    • PDF_VERSION_1_4

      public static final PdfName PDF_VERSION_1_4
      possible PDF version (catalog)
    • PDF_VERSION_1_5

      public static final PdfName PDF_VERSION_1_5
      possible PDF version (catalog)
    • PDF_VERSION_1_6

      public static final PdfName PDF_VERSION_1_6
      possible PDF version (catalog)
    • PDF_VERSION_1_7

      public static final PdfName PDF_VERSION_1_7
      possible PDF version (catalog)
    • PDF_VERSION_2_0

      public static final PdfName PDF_VERSION_2_0
      possible PDF version (catalog)
    • pdf

      protected PdfDocument pdf
      the pdfdocument object.
    • directContent

      protected PdfContentByte directContent
      The direct content in this document.
    • directContentUnder

      protected PdfContentByte directContentUnder
      The direct content under in this document.
    • body

      protected PdfWriter.PdfBody body
      body of the PDF document
    • extraCatalog

      protected PdfDictionary extraCatalog
      Holds value of property extraCatalog this is used for Output Intents.
    • root

      protected PdfPages root
      The root of the page tree.
    • pageReferences

      protected ArrayList<PdfIndirectReference> pageReferences
      The PdfIndirectReference to the pages.
    • currentPageNumber

      protected int currentPageNumber
      The current page number.
    • tabs

      protected PdfName tabs
      The value of the Tabs entry in the page dictionary.
      Since:
      2.1.5
    • prevxref

      protected int prevxref
      A number referring to the previous Cross-Reference Table.
    • pdf_version

      protected PdfVersionImp pdf_version
      Stores the version information for the header and the catalog.
    • xmpMetadata

      protected byte[] xmpMetadata
      XMP Metadata for the document.
    • fullCompression

      protected boolean fullCompression
      Holds value of property fullCompression.
    • compressionLevel

      protected int compressionLevel
      The compression level of the content streams.
      Since:
      2.1.3
    • documentFonts

      protected LinkedHashMap<BaseFont,org.openpdf.text.pdf.FontDetails> documentFonts
      The fonts of this document
    • fontNumber

      protected int fontNumber
      The font number counter for the fonts in the document.
    • formXObjects

      protected LinkedHashMap<PdfIndirectReference,Object[]> formXObjects
      The form XObjects in this document. The key is the xref and the value is Object[]{PdfName, template}.
    • formXObjectsCounter

      protected int formXObjectsCounter
      The name counter for the form XObjects name.
    • colorNumber

      protected int colorNumber
      The color number counter for the colors in the document.
    • documentExtGState

      protected HashMap<PdfDictionary,PdfObject[]> documentExtGState
      The patterns of this document
    • documentProperties

      protected HashMap<Object,PdfObject[]> documentProperties
    • group

      protected PdfDictionary group
      A group attributes dictionary specifying the attributes of the page's page group for use in the transparent imaging model
    • userunit

      protected float userunit
    • defaultColorspace

      protected PdfDictionary defaultColorspace
    • imageDictionary

      protected PdfDictionary imageDictionary
      Dictionary, containing all the images of the PDF document
    • JBIG2Globals

      protected HashMap<PdfStream,PdfIndirectReference> JBIG2Globals
      A HashSet with Stream objects containing JBIG2 Globals
      Since:
      2.1.5
  • Constructor Details

    • PdfWriter

      protected PdfWriter(PdfDocument document, OutputStream os)
      Constructs a PdfWriter.

      Remark: a PdfWriter can only be constructed by calling the method getInstance(Document document, OutputStream os).

      Parameters:
      document - The PdfDocument that has to be written
      os - The OutputStream the writer has to write to.
  • Method Details

    • setFixedFileId

      public void setFixedFileId(byte[] fileId)
      Makes the file identifier (/ID) of the trailer the given bytes (16 are used), instead of a random value, so that the same document gives the same file.
      Parameters:
      fileId - the identifier, or null for a random one (the default)
    • getInstance

      public static PdfWriter getInstance(Document document, OutputStream os) throws DocumentException
      Use this method to get an instance of the PdfWriter.
      Parameters:
      document - The Document that has to be written
      os - The OutputStream the writer has to write to.
      Returns:
      a new PdfWriter
      Throws:
      DocumentException - on error
    • getInfo

      public PdfDictionary getInfo()
      Use this method to get the info dictionary if you want to change it directly (add keys and values to the info dictionary).
      Returns:
      the info dictionary
    • getDirectContent

      public PdfContentByte getDirectContent()
      Use this method to get the direct content for this document. There is only one direct content, multiple calls to this method will allways retrieve the same object.
      Returns:
      the direct content
    • getDirectContentUnder

      public PdfContentByte getDirectContentUnder()
      Use this method to get the direct content under for this document. There is only one direct content, multiple calls to this method will always retrieve the same object.
      Returns:
      the direct content
    • addToBody

      public PdfIndirectObject addToBody(PdfObject object) throws IOException
      Use this method to add a PDF object to the PDF body. Use this method only if you know what you're doing!
      Parameters:
      object - the PDF object to add
      Returns:
      a PdfIndirectObject
      Throws:
      IOException - thrown when an I/O operation fails
    • addToBody

      public PdfIndirectObject addToBody(PdfObject object, boolean inObjStm) throws IOException
      Use this method to add a PDF object to the PDF body. Use this method only if you know what you're doing!
      Parameters:
      object - the PDF object to add
      inObjStm - a boolean
      Returns:
      a PdfIndirectObject
      Throws:
      IOException - thrown when an I/O operation fails
    • addToBody

      public PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref) throws IOException
      Use this method to add a PDF object to the PDF body. Use this method only if you know what you're doing!
      Parameters:
      object - the object to add
      ref - the IndirectReference of that object
      Returns:
      a PdfIndirectObject
      Throws:
      IOException - thrown when an I/O operation fails
    • getPdfIndirectReference

      public PdfIndirectReference getPdfIndirectReference()
      Use this to get an PdfIndirectReference for an object that will be created in the future. Use this method only if you know what you're doing!
      Returns:
      the PdfIndirectReference
    • getCatalog

      protected PdfDictionary getCatalog(PdfIndirectReference rootObj)
    • getPageReference

      public PdfIndirectReference getPageReference(int page)
      Use this method to get a reference to a page existing or not. If the page does not exist yet the reference will be created in advance. If on closing the document, a page number greater than the total number of pages was requested, an exception is thrown.
      Parameters:
      page - the page number. The first page is 1
      Returns:
      the reference to the page
    • getCurrentPage

      public PdfIndirectReference getCurrentPage()
      Retrieves a reference to the current page of the document. The current page is typically the last page that was modified or accessed.
      Returns:
      a PdfIndirectReference object pointing to the current page in the PDF document.
    • getTabs

      public PdfName getTabs()
      Returns the value to be used for the Tabs entry in the page tree.
      Returns:
      a PdfName of the value of the Tabs entry in the page dictionnary
      Since:
      2.1.5
    • open

      public void open()
      Signals that the Document has been opened and that Elements can be added.

      When this method is called, the PDF-document header is written to the outputstream.

      Specified by:
      open in interface DocListener
      Overrides:
      open in class DocWriter
      See Also:
    • close

      public void close()
      Signals that the Document was closed and that no other Elements will be added.

      The pages-tree is built and written to the outputstream. A Catalog is constructed, as well as an Info-object, the reference table is composed and everything is written to the outputstream embedded in a Trailer.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface DocListener
      Overrides:
      close in class DocWriter
      See Also:
    • addSharedObjectsToBody

      protected void addSharedObjectsToBody() throws IOException
      Throws:
      IOException
    • addAnnotation

      public void addAnnotation(PdfAnnotation annot)
    • isFullCompression

      public boolean isFullCompression()
      Use this method to find out if 1.5 compression is on.
      Returns:
      the 1.5 compression status
    • getCompressionLevel

      public int getCompressionLevel()
      Returns the compression level used for streams written by this writer.
      Returns:
      the compression level (0 = best speed, 9 = best compression, -1 is default)
      Since:
      2.1.3
    • setCompressionLevel

      public void setCompressionLevel(int compressionLevel)
      Sets the compression level to be used for streams written by this writer.
      Parameters:
      compressionLevel - a value between 0 (best speed) and 9 (best compression)
      Since:
      2.1.3
    • getUserunit

      public float getUserunit()
      Use this method to get the user unit. A user unit is a value that defines the default user space unit. The minimum UserUnit is 1 (1 unit = 1/72 inch). The maximum UserUnit is 75,000. Note that this userunit only works starting with PDF1.6!
      Returns:
      Returns the userunit.
    • getDefaultColorspace

      public PdfDictionary getDefaultColorspace()
      Use this method to get the default colorspaces.
      Returns:
      the default colorspaces
    • getReferenceJBIG2Globals

      protected PdfIndirectReference getReferenceJBIG2Globals(byte[] content)
      Gets an indirect reference to a JBIG2 Globals stream. Adds the stream if it hasn't already been added to the writer.
      Parameters:
      content - a byte array that may already been added to the writer inside a stream object.
      Returns:
      the PdfIndirectReference
      Since:
      2.1.5
    • isRgbTransparencyBlending

      public boolean isRgbTransparencyBlending()
      Gets the transparency blending colorspace.
      Returns:
      true if the transparency blending colorspace is RGB, false if it is the default blending colorspace
      Since:
      2.1.0
    • addDirectImageSimple

      public PdfName addDirectImageSimple(Image image) throws DocumentException
      Use this method to adds an image to the document but not to the page resources. It is used with templates and Document.add(Image). Use this method only if you know what you're doing!
      Parameters:
      image - the Image to add
      Returns:
      the name of the image added
      Throws:
      PdfException - on error
      DocumentException - on error
    • addDirectImageSimple

      public PdfName addDirectImageSimple(Image image, PdfIndirectReference fixedRef) throws DocumentException
      Adds an image to the document but not to the page resources. It is used with templates and Document.add(Image). Use this method only if you know what you're doing!
      Parameters:
      image - the Image to add
      fixedRef - the reference to used. It may be null, a PdfIndirectReference or a PRIndirectReference.
      Returns:
      the name of the image added
      Throws:
      PdfException - on error
      DocumentException - on error