Class PDImageXObject
- java.lang.Object
-
- com.tom_roush.pdfbox.pdmodel.graphics.PDXObject
-
- com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject
-
- All Implemented Interfaces:
COSObjectable,PDImage
public final class PDImageXObject extends PDXObject implements PDImage
An Image XObject.
-
-
Constructor Summary
Constructors Constructor Description PDImageXObject(PDStream stream, PDResources resources)Creates an Image XObject with the given stream as its contents and current color spaces.PDImageXObject(PDDocument document)Creates an Image XObject in the given document.PDImageXObject(PDDocument document, InputStream encodedStream, COSBase cosFilter, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace)Creates an Image XObject in the given document using the given filtered stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static PDImageXObjectcreateFromFile(File file, PDDocument doc)Create a PDImageXObject from an image file.static PDImageXObjectcreateFromFile(String imagePath, PDDocument doc)Create a PDImageXObject from an image file, seecreateFromFile(File, PDDocument)for more details.InputStreamcreateInputStream()Returns an InputStream containing the image data, irrespective of whether this is an inline image or an image XObject.InputStreamcreateInputStream(List<String> stopFilters)Returns an InputStream containing the image data, irrespective of whether this is an inline image or an image XObject.static PDImageXObjectcreateThumbnail(COSStream cosStream)Creates a thumbnail Image XObject from the given COSBase and name.intgetBitsPerComponent()Returns bits per component of this image, or -1 if one has not been set.COSArraygetColorKeyMask()Returns the color key mask array associated with this image, or null if there is none.PDColorSpacegetColorSpace()Returns the image's color space.COSArraygetDecode()Returns the decode array.intgetHeight()Returns height of this image, or -1 if one has not been set.android.graphics.BitmapgetImage()Returns the content of this image as a Bitmap with ARGB_888.booleangetInterpolate()Returns true if the image should be interpolated when rendered.PDImageXObjectgetMask()Returns the Mask Image XObject associated with this image, or null if there is none.PDMetadatagetMetadata()Returns the metadata associated with this XObject, or null if there is none.android.graphics.BitmapgetOpaqueImage()Returns an RGB buffered image containing the opaque image stream without any masks applied.PDImageXObjectgetSoftMask()Returns the Soft Mask Image XObject associated with this image, or null if there is none.android.graphics.BitmapgetStencilImage(android.graphics.Paint paint)Returns an ARGB image filled with the given paint and using this image as a mask.intgetStructParent()Returns the key of this XObject in the structural parent tree.StringgetSuffix()This will get the suffix for this image type, e.g.intgetWidth()Returns the width of this image, or -1 if one has not been set.booleanisEmpty()Returns true if the image has no data.booleanisStencil()Returns true if the image is a stencil mask.voidsetBitsPerComponent(int bpc)Set the number of bits per component.voidsetColorSpace(PDColorSpace cs)Sets the color space for this image.voidsetDecode(COSArray decode)Sets the decode array.voidsetHeight(int h)Sets the height of the image.voidsetInterpolate(boolean value)Sets the Interpolate flag, true for high-quality image scaling.voidsetMetadata(PDMetadata meta)Sets the metadata associated with this XObject, or null if there is none.voidsetStencil(boolean isStencil)Sets whether or not the image is a stencil.voidsetStructParent(int key)Sets the key of this XObject in the structural parent tree.voidsetWidth(int w)Sets the width of the image.-
Methods inherited from interface com.tom_roush.pdfbox.pdmodel.common.COSObjectable
getCOSObject
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from class com.tom_roush.pdfbox.pdmodel.graphics.PDXObject
createXObject, getCOSObject, getCOSStream, getPDStream, getStream
-
-
-
-
Constructor Detail
-
PDImageXObject
public PDImageXObject(PDDocument document) throws IOException
Creates an Image XObject in the given document.- Parameters:
document- the current document- Throws:
IOException- if there is an error creating the XObject.
-
PDImageXObject
public PDImageXObject(PDDocument document, InputStream encodedStream, COSBase cosFilter, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace) throws IOException
Creates an Image XObject in the given document using the given filtered stream.- Parameters:
document- the current documentencodedStream- an encoded stream of image datacosFilter- the filter or a COSArray of filterswidth- the image widthheight- the image heightbitsPerComponent- the bits per componentinitColorSpace- the color space- Throws:
IOException- if there is an error creating the XObject.
-
PDImageXObject
public PDImageXObject(PDStream stream, PDResources resources) throws IOException
Creates an Image XObject with the given stream as its contents and current color spaces.- Parameters:
stream- the XObject stream to readresources- the current resources- Throws:
IOException- if there is an error creating the XObject.
-
-
Method Detail
-
createThumbnail
public static PDImageXObject createThumbnail(COSStream cosStream) throws IOException
Creates a thumbnail Image XObject from the given COSBase and name.- Parameters:
cosStream- the COS stream- Returns:
- an XObject
- Throws:
IOException- if there is an error creating the XObject.
-
createFromFile
public static PDImageXObject createFromFile(String imagePath, PDDocument doc) throws IOException
Create a PDImageXObject from an image file, seecreateFromFile(File, PDDocument)for more details.- Parameters:
imagePath- the image file path.doc- the document that shall use this PDImageXObject.- Returns:
- a PDImageXObject.
- Throws:
IOException- if there is an error when reading the file or creating the PDImageXObject, or if the image type is not supported.
-
createFromFile
public static PDImageXObject createFromFile(File file, PDDocument doc) throws IOException
Create a PDImageXObject from an image file. The file format is determined by the file name suffix. The following suffixes are supported: jpg, jpeg, tif, tiff, gif, bmp and png. This is a convenience method that callsJPEGFactory.createFromStream(com.tom_roush.pdfbox.pdmodel.PDDocument, java.io.InputStream),CCITTFactory.createFromFile(com.tom_roush.pdfbox.pdmodel.PDDocument, java.io.File)orImageIO#readcombined withLosslessFactory.createFromImage(com.tom_roush.pdfbox.pdmodel.PDDocument, android.graphics.Bitmap). (The later can also be used to create a PDImageXObject from a BufferedImage).- Parameters:
file- the image file.doc- the document that shall use this PDImageXObject.- Returns:
- a PDImageXObject.
- Throws:
IOException- if there is an error when reading the file or creating the PDImageXObject.IllegalArgumentException- if the image type is not supported.
-
getMetadata
public PDMetadata getMetadata()
Returns the metadata associated with this XObject, or null if there is none.- Returns:
- the metadata associated with this object.
-
setMetadata
public void setMetadata(PDMetadata meta)
Sets the metadata associated with this XObject, or null if there is none.- Parameters:
meta- the metadata associated with this object
-
getStructParent
public int getStructParent()
Returns the key of this XObject in the structural parent tree.- Returns:
- this object's key the structural parent tree
-
setStructParent
public void setStructParent(int key)
Sets the key of this XObject in the structural parent tree.- Parameters:
key- the new key for this XObject
-
getImage
public android.graphics.Bitmap getImage() throws IOExceptionReturns the content of this image as a Bitmap with ARGB_888. The size of the returned image is the larger of the size of the image itself or its mask. The returned images are cached via a SoftReference.- Specified by:
getImagein interfacePDImage- Returns:
- content of this image as a buffered image.
- Throws:
IOException
-
getStencilImage
public android.graphics.Bitmap getStencilImage(android.graphics.Paint paint) throws IOExceptionReturns an ARGB image filled with the given paint and using this image as a mask. The returned images are not cached.- Specified by:
getStencilImagein interfacePDImage- Parameters:
paint- the paint to fill the visible portions of the image with- Returns:
- a masked image filled with the given paint
- Throws:
IOException- if the image cannot be read
-
getOpaqueImage
public android.graphics.Bitmap getOpaqueImage() throws IOExceptionReturns an RGB buffered image containing the opaque image stream without any masks applied. If this Image XObject is a mask then the buffered image will contain the raw mask.- Returns:
- the image without any masks applied
- Throws:
IOException- if the image cannot be read
-
getMask
public PDImageXObject getMask() throws IOException
Returns the Mask Image XObject associated with this image, or null if there is none.- Returns:
- Mask Image XObject
- Throws:
IOException
-
getColorKeyMask
public COSArray getColorKeyMask()
Returns the color key mask array associated with this image, or null if there is none.- Returns:
- Mask Image XObject
-
getSoftMask
public PDImageXObject getSoftMask() throws IOException
Returns the Soft Mask Image XObject associated with this image, or null if there is none.- Returns:
- the SMask Image XObject, or null.
- Throws:
IOException
-
getBitsPerComponent
public int getBitsPerComponent()
Description copied from interface:PDImageReturns bits per component of this image, or -1 if one has not been set.- Specified by:
getBitsPerComponentin interfacePDImage
-
setBitsPerComponent
public void setBitsPerComponent(int bpc)
Description copied from interface:PDImageSet the number of bits per component.- Specified by:
setBitsPerComponentin interfacePDImage- Parameters:
bpc- The number of bits per component.
-
getColorSpace
public PDColorSpace getColorSpace() throws IOException
Description copied from interface:PDImageReturns the image's color space.- Specified by:
getColorSpacein interfacePDImage- Throws:
IOException- If there is an error getting the color space.
-
createInputStream
public InputStream createInputStream() throws IOException
Description copied from interface:PDImageReturns an InputStream containing the image data, irrespective of whether this is an inline image or an image XObject.- Specified by:
createInputStreamin interfacePDImage- Returns:
- Decoded stream\
- Throws:
IOException- if the data could not be read.
-
createInputStream
public InputStream createInputStream(List<String> stopFilters) throws IOException
Description copied from interface:PDImageReturns an InputStream containing the image data, irrespective of whether this is an inline image or an image XObject. The given filters will not be decoded.- Specified by:
createInputStreamin interfacePDImage- Returns:
- Decoded stream
- Throws:
IOException- if the data could not be read.
-
isEmpty
public boolean isEmpty()
Description copied from interface:PDImageReturns true if the image has no data.
-
setColorSpace
public void setColorSpace(PDColorSpace cs)
Description copied from interface:PDImageSets the color space for this image.- Specified by:
setColorSpacein interfacePDImage- Parameters:
cs- The color space for this image.
-
getHeight
public int getHeight()
Description copied from interface:PDImageReturns height of this image, or -1 if one has not been set.
-
setHeight
public void setHeight(int h)
Description copied from interface:PDImageSets the height of the image.
-
getWidth
public int getWidth()
Description copied from interface:PDImageReturns the width of this image, or -1 if one has not been set.
-
setWidth
public void setWidth(int w)
Description copied from interface:PDImageSets the width of the image.
-
getInterpolate
public boolean getInterpolate()
Description copied from interface:PDImageReturns true if the image should be interpolated when rendered.- Specified by:
getInterpolatein interfacePDImage
-
setInterpolate
public void setInterpolate(boolean value)
Description copied from interface:PDImageSets the Interpolate flag, true for high-quality image scaling.- Specified by:
setInterpolatein interfacePDImage
-
setDecode
public void setDecode(COSArray decode)
Description copied from interface:PDImageSets the decode array.
-
getDecode
public COSArray getDecode()
Description copied from interface:PDImageReturns the decode array.
-
isStencil
public boolean isStencil()
Description copied from interface:PDImageReturns true if the image is a stencil mask.
-
setStencil
public void setStencil(boolean isStencil)
Description copied from interface:PDImageSets whether or not the image is a stencil. This corresponds to theImageMaskentry in the image stream's dictionary.- Specified by:
setStencilin interfacePDImage- Parameters:
isStencil- True to make the image a stencil.
-
-