org.faceless.pdf2
Class PDFImage

java.lang.Object
  extended byorg.faceless.pdf2.PDFObject
      extended byorg.faceless.pdf2.PDFStream
          extended byorg.faceless.pdf2.PDFImage
All Implemented Interfaces:
Cloneable

public final class PDFImage
extends org.faceless.pdf2.PDFStream

The PDFImage class encapsulates a bitmap image, like a JPEG or PNG file, which can be inserted directly into the PDF document.

Example
   PDFPage page = pdf.newPage(PAGESIZE_A4);
   PDFImage img = new PDFImage(new FileInputStream("mypicture.jpg"));
   page.drawImage(img, 100, 100, i.getWidth(), i.getHeight());
 

Images embedded into the document can be stretched to any size, but it's important to remember that there is a loss in quality as they're stretched. A thumbnail-size image scaled to full page will look bad on a 72dpi screen, and worse on a 600dpi printer.

Transparency as supported by the GIF, PNG, TIFF and JPEG2000 file formats can be used - since version 2.0 this includes the alpha transparency available in the PNG file format (and alpha information in TIFF image since version 2.1.1). Note that this feature is only supported in Acrobat 5 or later, and even the "mask" transparency used in GIF and 8-bit PNG images requires PostScript level 3 support in order to print correctly. For this reason, we can't guarantee correct results when viewing documents with translucent images in Acrobat 4 or earlier. As best as we can tell, the worst that will happen is the transparency information will be ignored..

Any ICC Color profiles embedded in PNG, TIFF, JPEG and JPEG2000 images will be automatically embedded in the resulting PDF.

Since:
1.0

Constructor Summary
PDFImage(byte[] in)
          Load a PDFimage from a byte array.
PDFImage(Image img)
          Create a new PDFimage from the specified java.awt.Image.
PDFImage(InputStream in)
          Load a PDFimage from an InputStream.
PDFImage(int w, int h, int bpc, double dpix, double dpiy, boolean photo, boolean alpha, ColorSpace space, InputStream[] planes)
           Create a PDFImage from the raw bitmap data provided.
 
Method Summary
 void close()
          Compress the image and close it, preventing any further changes.
 double getDPIX()
          Return the dots-per-inch of the image in the X direction (horizontally).
 double getDPIY()
          Return the dots-per-inch of the image in the Y direction (vertically) Not every image contains this information (for example, it's not part of the GIF specification), in which case this method returns the default resolution of 72, which means 1 pixel = 1 point.
 float getHeight()
           Return the height in points of the image.
 Reader getMetaData()
           Return any XML metadata associated with this object.
 float getWidth()
           Return the width in points of the image.
 void setMetaData(String xmldata)
          Set the XML metadata associated with this object.
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PDFImage

public PDFImage(Image img)
         throws InterruptedException,
                IllegalArgumentException
Create a new PDFimage from the specified java.awt.Image. The image must be fully loaded, ie. the width and height are known, otherwise an IllegalArgumentException is thrown.

Parameters:
img - the image to load
Throws:
InterruptedException - if the library is unable to read the pixels from the image before being interrupted
IllegalArgumentException - if the image is invalid - ie. the width or height is zero or not defined, or if the image is not an embeddable image type

PDFImage

public PDFImage(InputStream in)
         throws IOException,
                IllegalArgumentException
Load a PDFimage from an InputStream. PDF documents can embed these types of images with the following restrictions:
PNGAll except for 16 bit PNG
JPEGProgressive JPEG images can only be viewed in Acrobat 4 or later. Additionally, CMYK JPEG images created with Adobe tools like Photoshop may print incorrectly unless you're running Java 1.4 or later.
GIFIf the GIF is animated, only the first frame is embedded
TIFFSince 2.1.1 we can handle CCITT, Flate, LZW, CMYK, alpha transparency, most YCbCr and (if you're running Java 1.4 or later) new-style JPEG images, but old-style JPEG, JBIG, SGI, 16-bit and Lab images remain unsupported. For multi-page TIFF support see the PDFImageSet class.
PBM, PGMOnly standard 8-bit PBM and PGM images are supported. Support was added in version 2.0.6
JPEG 2000These can only be displayed in Acrobat 6 or later. Support added in version 2.1.1
Note the InputStream is not closed by this constructor - it should be closed by the user

Parameters:
in - the InputStream to read the image from
Throws:
IOException - if the method is unable to read or parse the image
IllegalArgumentException - if the image is invalid or can't be embedded.

PDFImage

public PDFImage(byte[] in)
         throws IOException,
                IllegalArgumentException
Load a PDFimage from a byte array. This method is identical to the PDFImage(InputStream) constructor, but takes a byte array containing the image as a parameter instead

Since:
1.2

PDFImage

public PDFImage(int w,
                int h,
                int bpc,
                double dpix,
                double dpiy,
                boolean photo,
                boolean alpha,
                ColorSpace space,
                InputStream[] planes)
         throws IOException

Create a PDFImage from the raw bitmap data provided. The vast majority of users will be better off either parsing an encoded image format or calling the PDFImage(Image) constructor, but for special cases it's possible to use this constructor to pass the raw bitmap data in for each plane.

Parameters:
w - the width of the image in pixels
h - the height of the image in pixels
bpc - the number of bits for each component of the image. Must be 1, 2, 4, 8 or 16 - although 16 is only supported by Acrobat 6 or later.
dpix - the horizontal dots-per-inch of the image
dpiy - the vertical dots-per-inch of the image
photo - for 8 bit images, whether to use JPEG compression instead of the normal Flate compression. For non 8-bit images this has no effect
alpha - whether or not an alpha plane is being passed in as the last plane
space - The ColorSpace this image is in. The number of components in the colorspace must match the number of planes passed in.
planes - The planes of the image. Each InputStream represents a single plane (for example, a CMYK image would have four planes passed in, the first representing Cyan, the second Magenta and so on). Each InputStream must contain w*h*bpc bits of image data in the form of a number of horizontal scanlines starting at the top of the image. Each scanline must start on a byte boundary. Note the InputStreams are not closed by this constructor.
Throws:
IOException - if one of the InputStreams throws an IOException while being read, or if one of them returns a -1 from it's read() method.
Since:
2.2
Method Detail

getWidth

public float getWidth()

Return the width in points of the image. This may be different to the width in pixels, depending on whether the image contains information about it's resolution.

See Also:
getDPIX()

getHeight

public float getHeight()

Return the height in points of the image. This may be different to the height in pixels, depending on whether the image contains information about it's resolution.

See Also:
getDPIY()

getDPIX

public double getDPIX()
Return the dots-per-inch of the image in the X direction (horizontally). Not every image contains this information (for example, it's not part of the GIF specification), in which case this method returns the default resolution of 72, which means 1 pixel = 1 point.

Since:
1.2

getDPIY

public double getDPIY()
Return the dots-per-inch of the image in the Y direction (vertically) Not every image contains this information (for example, it's not part of the GIF specification), in which case this method returns the default resolution of 72, which means 1 pixel = 1 point.

Since:
1.2

setMetaData

public void setMetaData(String xmldata)
Set the XML metadata associated with this object. See PDF.setMetaData for more information.

Parameters:
xmldata - the XML data to embed into the document, or null to clear any existing metadata. No validation is performed on this input.
Since:
1.1.12

getMetaData

public Reader getMetaData()
                   throws IOException

Return any XML metadata associated with this object. See the PDF.getMetaData() for more information.

Note that JPEG2000 images may have more than one MetaData stream embedded in them. If this is the case, in order to present only a single root node to the XML Parser, the XML objects are all wrapped in a single <JPEG2000> node

Returns:
a Reader containing the source of the XML or null if no metadata is available.
Throws:
IOException - if the metadata can't be extracted
Since:
1.1.12

close

public void close()
Compress the image and close it, preventing any further changes. Unless you're going to add MetaData to the image, it's always a good idea to call this method so you can claw back some memory.

Since:
2.0
See Also:
Cache

toString

public String toString()


Copyright © 2001-2004 Big Faceless Organization