Class Image

All Implemented Interfaces:
Element
Direct Known Subclasses:
ImgJBIG2, ImgRaw

public abstract class Image extends Rectangle
An Image is the representation of a graphic element (JPEG, PNG or GIF) that has to be inserted into the document Security consideration: This method accepts input and processes it without built-in validation, filtering, or access restrictions. It may load resources such as local files or external URLs, depending on the input. This can introduce security risks including unauthorized file access, directory traversal, or server-side request forgery (SSRF). It is the responsibility of the calling application to validate and sanitize all input before passing it to this method, especially when handling user-controlled or external data. Refer to established secure coding guidelines to ensure safe usage.
See Also:
  • Field Details

    • DEFAULT

      public static final int DEFAULT
      Image alignment: Default alignment (same as LEFT). The image will be placed on the left side of the page.
      See Also:
    • LEFT

      public static final int LEFT
      Image alignment: Left-align the image. The image will be placed on the left side of the page. Can be combined with TEXTWRAP or UNDERLYING using bitwise OR. Example: image.setAlignment(Image.LEFT | Image.TEXTWRAP);
      See Also:
    • AX

      public static final int AX
      This represents a coordinate in the transformation matrix.
      See Also:
    • AY

      public static final int AY
      This represents a coordinate in the transformation matrix.
      See Also:
    • BX

      public static final int BX
      This represents a coordinate in the transformation matrix.
      See Also:
    • BY

      public static final int BY
      This represents a coordinate in the transformation matrix.
      See Also:
    • CX

      public static final int CX
      This represents a coordinate in the transformation matrix.
      See Also:
    • CY

      public static final int CY
      This represents a coordinate in the transformation matrix.
      See Also:
    • DX

      public static final int DX
      This represents a coordinate in the transformation matrix.
      See Also:
    • DY

      public static final int DY
      This represents a coordinate in the transformation matrix.
      See Also:
    • ORIGINAL_NONE

      public static final int ORIGINAL_NONE
      type of image
      See Also:
    • type

      protected int type
      The image type.
    • url

      protected URL url
      The URL of the image.
    • rawData

      protected byte[] rawData
      The raw data of the image.
    • bpc

      protected int bpc
      The bits per component of the raw image. It also flags a CCITT image.
    • template

      protected PdfTemplate[] template
      The template to be treated as an image.
    • alignment

      protected int alignment
      The alignment of the Image.
    • alt

      protected String alt
      Text that can be shown instead of the image.
    • absoluteX

      protected float absoluteX
      This is the absolute X-position of the image.
    • absoluteY

      protected float absoluteY
      This is the absolute Y-position of the image.
    • plainWidth

      protected float plainWidth
      This is the width of the image without rotation.
    • plainHeight

      protected float plainHeight
      This is the width of the image without rotation.
    • scaledWidth

      protected float scaledWidth
      This is the scaled width of the image taking rotation into account.
    • scaledHeight

      protected float scaledHeight
      This is the original height of the image taking rotation into account.
    • compressionLevel

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

      protected Long mySerialId
      an iText attributed unique id for this image.
    • rotationRadians

      protected float rotationRadians
      This is the rotation of the image in radians.
    • indentationLeft

      protected float indentationLeft
      the indentation to the left.
    • indentationRight

      protected float indentationRight
      the indentation to the right.
    • spacingBefore

      protected float spacingBefore
      The spacing before the image.
    • spacingAfter

      protected float spacingAfter
      The spacing after the image.
    • interpolation

      protected boolean interpolation
      Holds value of property interpolation.
    • originalType

      protected int originalType
      Holds value of property originalType.
    • originalData

      protected byte[] originalData
      Holds value of property originalData.
    • deflated

      protected boolean deflated
      Holds value of property deflated.
    • dpiX

      protected int dpiX
      Holds value of property dpiX.
    • dpiY

      protected int dpiY
      Holds value of property dpiY.
    • colorspace

      protected int colorspace
      this is the colorspace of a jpeg-image.
    • invert

      protected boolean invert
      Image color inversion
    • mask

      protected boolean mask
      Is this image a mask?
    • imageMask

      protected Image imageMask
      The image that serves as a mask for this image.
    • transparency

      protected int[] transparency
      this is the transparency information of the raw image
  • Constructor Details

    • Image

      public Image(URL url)
      Constructs an Image -object, using an url .
      Parameters:
      url - the URL where the image can be found.
    • Image

      protected Image(Image image)
      Constructs an Image -object, using an url .
      Parameters:
      image - another Image object.
  • Method Details

    • getInstance

      public static Image getInstance(int width, int height, int components, int bpc, byte[] data) throws BadElementException
      Gets an instance of an Image in raw mode.
      Parameters:
      width - the width of the image in pixels
      height - the height of the image in pixels
      components - 1,3 or 4 for GrayScale, RGB and CMYK
      bpc - bits per component
      data - the image data
      Returns:
      an object of type ImgRaw
      Throws:
      BadElementException - on error
    • getSerialId

      protected static Long getSerialId()
      Creates a new serial id.
      Returns:
      a new serial id
    • getDirectReference

      public PdfIndirectReference getDirectReference()
      Getter for property directReference.
      Returns:
      Value of property directReference.
    • type

      public int type()
      Returns the type.
      Specified by:
      type in interface Element
      Overrides:
      type in class Rectangle
      Returns:
      a type
    • isNestable

      public boolean isNestable()
      Description copied from interface: Element
      Checks if this element is nestable.
      Specified by:
      isNestable in interface Element
      Overrides:
      isNestable in class Rectangle
      Returns:
      true if this element can be nested inside other elements.
      Since:
      iText 2.0.8
      See Also:
    • isImgRaw

      public boolean isImgRaw()
      Returns true if the image is a ImgRaw -object.
      Returns:
      a boolean
    • isImgTemplate

      public boolean isImgTemplate()
      Returns true if the image is an ImgTemplate -object.
      Returns:
      a boolean
    • getRawData

      public byte[] getRawData()
      Gets the raw data for the image.

      Remark: this only makes sense for Images of the type RawImage .

      Returns:
      the raw data
    • getBpc

      public int getBpc()
      Gets the bpc for the image.

      Remark: this only makes sense for Images of the type RawImage .

      Returns:
      a bpc value
    • getTemplateData

      public PdfTemplate getTemplateData()
      Gets the template to be used as an image.

      Remark: this only makes sense for Images of the type ImgTemplate .

      Returns:
      the template
    • setAbsolutePosition

      public void setAbsolutePosition(float absoluteX, float absoluteY)
      Sets the absolute position of the Image.
      Parameters:
      absoluteX - absolute x position
      absoluteY - absolute y position
    • getAbsoluteX

      public float getAbsoluteX()
      Returns the absolute X position.
      Returns:
      a position
    • hasAbsoluteY

      public boolean hasAbsoluteY()
      Checks if the Images has to be added at an absolute position.
      Returns:
      a boolean
    • getAbsoluteY

      public float getAbsoluteY()
      Returns the absolute Y position.
      Returns:
      a position
    • matrix

      public float[] matrix()
      Returns the transformation matrix of the image.
      Returns:
      an array [AX, AY, BX, BY, CX, CY, DX, DY]
    • getMySerialId

      public Long getMySerialId()
      Returns a serial id for the Image (reuse the same image more than once)
      Returns:
      a serialId
    • isInterpolation

      public boolean isInterpolation()
      Getter for property interpolation.
      Returns:
      Value of property interpolation.
    • isDeflated

      public boolean isDeflated()
      Getter for property deflated.
      Returns:
      Value of property deflated.
    • getColorspace

      public int getColorspace()
      Gets the colorspace for the image.

      Remark: this only makes sense for Images of the type Jpeg.

      Returns:
      a colorspace value
    • isInverted

      public boolean isInverted()
      Getter for the inverted value
      Returns:
      true if the image is inverted
    • getAdditional

      public PdfDictionary getAdditional()
      Getter for the dictionary with additional information.
      Returns:
      a PdfDictionary with additional information.
    • isMask

      public boolean isMask()
      Returns true if this Image is a mask.
      Returns:
      true if this Image is a mask
    • makeMask

      public void makeMask() throws DocumentException
      Make this Image a mask.
      Throws:
      DocumentException - if this Image can not be a mask
    • isMaskCandidate

      public boolean isMaskCandidate()
      Returns true if this Image has the requisites to be a mask.
      Returns:
      true if this Image can be a mask
    • getImageMask

      public Image getImageMask()
      Gets the explicit masking.
      Returns:
      the explicit masking
    • setImageMask

      public void setImageMask(Image mask) throws DocumentException
      Sets the explicit masking.
      Parameters:
      mask - the mask to be applied
      Throws:
      DocumentException - on error
    • isSmask

      public boolean isSmask()
      Getter for property smask.
      Returns:
      Value of property smask.
    • getTransparency

      public int[] getTransparency()
      Returns the transparency.
      Returns:
      the transparency values
    • getCompressionLevel

      public int getCompressionLevel()
      Returns the compression level used for images written as a compressed stream.
      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 if the image is written as a compressed stream.
      Parameters:
      compressionLevel - a value between 0 (best speed) and 9 (best compression)
      Since:
      2.1.3
    • getInstance

      public static Image getInstance(int width, int height, int components, int bpc, byte[] data, int[] transparency) throws BadElementException
      Gets an instance of an Image in raw mode.
      Parameters:
      width - the width of the image in pixels
      height - the height of the image in pixels
      components - 1,3 or 4 for GrayScale, RGB and CMYK
      bpc - bits per component
      data - the image data
      transparency - transparency information in the Mask format of the image dictionary
      Returns:
      an object of type ImgRaw
      Throws:
      BadElementException - on error