Class Image


public class Image extends Element
An image resource can either be an image XObject or inline image found in a page's content stream. An Image Xobject is stored separately from the graphics content stream in a PDF, and so can be used multiple times in the document, even across pages. The image is described in the /Xobject entry in the resources dictionary on the PDF page.

Image size and resoultion: For images that are imported from raster image files, the design width and height are stored in the Image object's Matrix, in the A (width) and D (depth) Matrix members, when the image is imported. These are stored in PDF units.

To derive the effective resolution of an image imported from a raster image file:

Horizontal resolution: divide the Image Width by the Image's Matrix A member and multiply by 72 to scale to DPI (dots per inch).

Vertical resolution: divide the Image Height by the Image's Matrix D member and multiply by 72 to scale to DPI (dots per inch).

  • Constructor Details

    • Image

      public Image(ImageInputStream stream) throws IOException
      Allows to create Image object from ImageInputStream.
      Throws:
      IOException
    • Image

      public Image(String fileName)
      Create a new Image from an existing physical file. This file can be a bitmap, gif, jpeg, png, or a tiff file. An exception will be thrown if the image file is not any of the above.

      NOTICE on tiff files: If the tiff file is multi-paged, then the first "page" only of the file will be converted to an Image. See ImageCollection to convert each page in a multi-page tiff file to an Image.

      Parameters:
      fileName - the filename of the image file.
    • Image

      public Image(String fileName, Document document)
      Create a new Image from an existing physical file. This file can be a bitmap, gif, jpeg, png, or a tiff file. An exception will be thrown if the image file is not any of the above.

      Specifying the document optimizes data usage for this image within that document.

      NOTICE on tiff files: If the tiff file is multi-paged, then the first "page" only of the file will be converted to an Image. See ImageCollection to convert each page in a multi-page tiff file to an Image.

      Parameters:
      fileName - the filename of the image file.
      document - the document in which the image is intended to be used.
    • Image

      public Image(byte[] buffer, int width, int height, ColorSpace colorSpace)
      Create an image from byte array. This constructor expects that input buffer is non-padded. If buffer doesn't correspond to the expected format, it will be stripped.
      • For example: The bitmap data generated by Page's drawContents(DrawParams) function is 32-bit aligned. The PDF image operator expects, however, 8-bit aligned image data. To remedy this difference, we check to see if the 32-bit aligned width is different from the 8-bit aligned width. If so, we fix the image data by stripping off the padding at the end.

      Parameters:
      buffer - contains Image's data
      width - width of the image
      height - height of the image
      colorSpace - colorspace with wich Image object will be created
  • Method Details

    • delete

      public void delete()
      Overrides:
      delete in class Element
    • getBufferedImage

      public BufferedImage getBufferedImage()
      Return this PDF image as a Java BufferedImage. Images in Gray or RGB color spaces are returned without conversion, while images in other spaces are first converted to RGB.
    • getData

      public byte[] getData()
      The actual bytes that make up the image, exactly as they appear in the image data.

    • getLargeData

      public List<ImageData> getLargeData()
      Gets the decompressed image data from very large images.

      NOTE: This function is only supported in 64-bit, only if enough contiguous memory is available. The data size must exceed nearly 2GB, otherwise an exception is thrown and you would use the Data member instead to access the image data. The first members of the list returned contain a close to 2GB chunk of the data, but the last member containing the remaining data will be smaller in size. This faciliates retrieving the data in .NET and Java which have 2GB limits on object sizes.

    • getWidth

      public int getWidth()
      The width of the image in pixels.

    • getHeight

      public int getHeight()
      The height of the image in pixels.

    • getBitsPerComponent

      public int getBitsPerComponent()
      The number of bits per component in the image.

    • getNumberComponents

      public int getNumberComponents()
      The number of components

    • getIntent

      public String getIntent()
      The color rendering intent for the image

    • setIntent

      public void setIntent(String intent)
      SetIntent allows to set color rendering intent for the image.
      Parameters:
      intent - the intent name
    • getGraphicState

      public GraphicState getGraphicState()
      Get the graphic state of the current element.
    • setGraphicState

      public void setGraphicState(GraphicState gstate)
      Set the Graphic State of the current element.
      Parameters:
      gstate - the new/updated Graphic State for the element.
    • changeResolution

      public Image changeResolution(int dpi)
      Create a new image from an existing one modifying the resolution (dots per inch)
      Parameters:
      dpi - the new resolution
      Returns:
      newimage - the original image modified to the new resolution.
    • translate

      public void translate(double tx, double ty)
      Translate an image by (tx,ty) units using the matrix [ 1 0 0 1 tx ty ]. For images, this translation is done without regard to the scaling and rotation of the original image.

      To ensure that this translation can be done, do the translation before any further rotation or scaling.

      Overrides:
      translate in class Element
    • rotate

      public void rotate(double angle)
      Rotate an image on (angle) degree using the matrix [cos(a), sin(a), -sin(a), cos(a), 0, 0]. For images, this rotate is done without regard to the primary scaling of the original image
      Overrides:
      rotate in class Element
    • save

      public void save(String imagefilename, ImageType imageType, ImageSaveParams saveparams)
      Save an image into a particular file format with the given parameters for saving.
      Parameters:
      imagefilename - The physical image file which will be created.
      imageType - The ImageType used for saving the image.
      saveparams - The ImageSaveParams for saving the image.
    • save

      public void save(String imagefilename, ImageType imageType)
      Save an image into a particular file format with default parameters for saving.
      Parameters:
      imagefilename - The physical image file which will be created.
      imageType - The IMAGETYPE used for saving the image.
    • getStream

      public PDFStream getStream()
      The PDFStream object or null if there is no stream associated with the image
    • getColorSpace

      public ColorSpace getColorSpace()
      return the ColorSpace with which this Image was created.
    • getCompression

      public CompressionCode getCompression()
      Returns:
      A compression code which represents the filter used in the image data.
    • setCompression

      public void setCompression(CompressionCode code)
      Set a Compression scheme for the image data.
      Parameters:
      code - The new new compression scheme to be applied.
    • getSoftMask

      public Image getSoftMask()
      Gets, for this image, the soft mask (also an image).

      Returns:
      image - an image, the soft mask
    • setSoftMask

      public void setSoftMask(Image softMask)
      Sets, for this image, the soft mask (also an image).

      Parameters:
      softMask - an image, the soft mask. If set to NULL, the soft mask is removed.
    • getIsImageMask

      public boolean getIsImageMask()
      Check that Image is ImageMask object
    • clone

      public Image clone()
      Makes a copy of the image.

      Make a copy of an image to obtain a new image that has a distinct set of attributes.

      Overrides:
      clone in class Element
      Returns:
      The copied image.
    • clone

      public Image clone(EnumSet<ElementCloneFlags> cloneFlags)
      Makes a copy of the image.

      Make a copy of an image to obtain a new image that has a distinct set of attributes.

      Overrides:
      clone in class Element
      Parameters:
      cloneFlags - Flags that modify the cloning operation.
      Returns:
      The copied image.
    • colorConvert

      public Image colorConvert(Document document, ColorProfile prof, RenderIntent intent, boolean embed)
      Applies the specified ICC ColorProfile to the Image

      Parameters:
      document - The document the element belongs to.
      prof - The ICC ColorProfile to be applied.
      intent - The ICC Rendering Intent for the ColorProfile
      embed - If true, embed the color space and make the object calibrated. If it is false and the target profile is CMYK, RGB, or Gray, the colors space of the resulting object, after conversion, will be DeviceCMYK, DeviceRGB, or DeviceGray, respectively.
      Returns:
      A new Image with the applied ICC ColorProfile.
    • getDidWarningsOccurDuringImport

      public boolean getDidWarningsOccurDuringImport()
      Gets whether or not warnings occurred during import of the raster image file.