Class Image
- java.lang.Object
-
- com.pdf_tools.fourheights.pdftoolbox.internal.NativeBase
-
- com.pdf_tools.fourheights.pdftoolbox.internal.NativeObject
-
- com.pdf_tools.fourheights.pdftoolbox.pdf.content.Image
-
public class Image extends NativeObject
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Image
create(Document targetDocument, Stream stream)
Create an image object from image data.void
extract(Stream stream)
Extract embedded image from PDFvoid
extract(Stream stream, ImageType imageType)
Extract embedded image from PDFint
getBitsPerComponent()
the number of bits per component.ColorSpace
getColorSpace()
the color space in which image samples are specified.ImageType
getDefaultImageType()
Default extracted image type.byte[]
getSamples()
The raw content of the image.Size
getSize()
The size of the image in samples.-
Methods inherited from class com.pdf_tools.fourheights.pdftoolbox.internal.NativeObject
equals, hashCode
-
-
-
-
Method Detail
-
create
public static Image create(Document targetDocument, Stream stream) throws java.io.IOException, UnknownFormatException, CorruptException
Create an image object from image data.
Supported formats are:- BMP
- DIB
- JPEG
- JPEG2000
- JBIG2
- PNG
- GIF
- Parameters:
targetDocument
- the output document with which the returned object is associatedstream
- the image data stream- Returns:
- the newly created image object
- Throws:
java.io.IOException
- Error reading from the image or writing to the documentUnknownFormatException
- The image data has an unknown formatCorruptException
- The image data is corruptjava.lang.IllegalArgumentException
- if thetargetDocument
argument has already been closedjava.lang.IllegalArgumentException
- if thetargetDocument
argument is read-onlyjava.lang.IllegalArgumentException
- if thestream
argument isnull
java.lang.IllegalArgumentException
- iftargetDocument
isnull
java.lang.IllegalArgumentException
- ifstream
isnull
-
extract
public void extract(Stream stream) throws GenericException
Extract embedded image from PDF
Facilitate the extraction of images from a specified page within a PDF, outputting them in the
imageType
format.By default, the method determines the format of the extracted image based on the embedded image data present within the PDF. Users can ascertain the default image format through
getDefaultImageType()
. It's important to note that not all image types or conversion processes are universally supported, hence adhering to the defaultImageType
is advisable for optimal compatibility.Key considerations include:
- The extraction process isolates the image from the page's resources, neglecting any contextual attributes from the PDF page. Consequently, the original resolution and modifications—such as scaling, rotation, or cropping—that influence the image's appearance on the page are not preserved in the extracted image.
-
In instances where a
GenericException
error arises, the output file may be compromised and rendered unusable.
This method is designed to efficiently retrieve images without their page-specific modifications, ensuring a straightforward extraction process.
- Parameters:
stream
- The image data stream.- Throws:
java.lang.IllegalArgumentException
- if thestream
argument is nulljava.lang.IllegalStateException
- if the image has already been closedGenericException
- if image extraction failsjava.lang.IllegalArgumentException
- ifstream
isnull
-
extract
public void extract(Stream stream, ImageType imageType) throws GenericException
Extract embedded image from PDF
Facilitate the extraction of images from a specified page within a PDF, outputting them in the
imageType
format.By default, the method determines the format of the extracted image based on the embedded image data present within the PDF. Users can ascertain the default image format through
getDefaultImageType()
. It's important to note that not all image types or conversion processes are universally supported, hence adhering to the defaultImageType
is advisable for optimal compatibility.Key considerations include:
- The extraction process isolates the image from the page's resources, neglecting any contextual attributes from the PDF page. Consequently, the original resolution and modifications—such as scaling, rotation, or cropping—that influence the image's appearance on the page are not preserved in the extracted image.
-
In instances where a
GenericException
error arises, the output file may be compromised and rendered unusable.
This method is designed to efficiently retrieve images without their page-specific modifications, ensuring a straightforward extraction process.
- Parameters:
stream
- The image data stream.imageType
- The desired image type of the extracted image stream. If the embedded image data cannot be directly extracted to the chosen ImageType, the data is first recompressed and then extracted to the chosen ImageType. In this case, extraction is slower and there can be some loss of image quality. The default image type can be retrieved by callinggetDefaultImageType()
.- Throws:
java.lang.IllegalArgumentException
- if thestream
argument is nulljava.lang.IllegalStateException
- if the image has already been closedGenericException
- if image extraction failsjava.lang.IllegalArgumentException
- ifstream
isnull
-
getDefaultImageType
public ImageType getDefaultImageType()
Default extracted image type. (Getter)
The default image type that will be extracted, based on the way that the image data is compressed and stored in the PDF file. The type of the output image isImageType.JPEG
for embedded JPEG and JPEG2000 images. In all other cases the image type will beImageType.TIFF
.- Throws:
java.lang.IllegalStateException
- if the image has already been closed
-
getSize
public Size getSize()
The size of the image in samples. (Getter)
Samples are often also called pixels.- Throws:
java.lang.IllegalStateException
- if the image has already been closed
-
getSamples
public byte[] getSamples()
The raw content of the image. (Getter)
The samples (pixels) are given in order, top to bottom, left to right. Each sample is given component by component. There is no padding between components or samples, except that each row of sample data begins on a byte boundary. If the number of data bits per row is not a multiple of 8, the end of the row is padded with extra bits to fill out the last byte. Padding bits should be ignored.
Most often, each component is 8 bits, so there's no packing/unpacking or alignment/padding. Components with 2 or 4 bits are very rare.
If the image is compressed, it will be decompressed in order to get the samples. For very large images, this may take some time.
- Throws:
java.lang.IllegalStateException
- if the image has already been closed
-
getBitsPerComponent
public int getBitsPerComponent()
the number of bits per component. (Getter)
The number of bits used to represent each color component. Only a single value may be specified; the number of bits is the same for all color components. Valid values are 1, 2, 4, and 8.- Throws:
java.lang.IllegalStateException
- if the image has already been closed
-
getColorSpace
public ColorSpace getColorSpace()
the color space in which image samples are specified. (Getter)
- Throws:
java.lang.IllegalStateException
- if the image has already been closed
-
-