Package org.faceless.pdf2
Class PDFImageSet
- java.lang.Object
-
- org.faceless.pdf2.PDFImageSet
-
public final class PDFImageSet extends Object
The
ExamplePDFImageSet
class is a thin wrapper around a multi-page image format (currently only TIFF images). Although it can be used with single page images, it's simpler just to create aPDFImage
directly.// Convert a multi-page TIFF to a multi-page PDF PDF pdf = new PDF(); PDFImageSet tiff = new PDFImageSet(new FileInputStream("multipage.tif")); int count = 0; PDFImage image; while ((image = tiff.getImage(count++)) != null) { PDFPage page = pdf.newPage(image.getWidth(), image.getHeight()); page.drawImage(image, 0, 0, image.getWidth(), image.getHeight()); } pdf.render(new FileOutputStream("out.pdf"));
Note prior to 2.21, this method threw an Exception if the numbe supplied to getImage was greater than the number of images. In 2.21 the internal architecture changed to load the images on demand, which removed the presumption that we know the number of pages in advance.- Since:
- 1.1.13
- See Also:
PDFImage
-
-
Constructor Summary
Constructors Constructor Description PDFImageSet(File file)
Create a newPDFImageSet
from the specified File.PDFImageSet(InputStream in)
Create a newPDFImageSet
from the specified InputStream.PDFImageSet(URL url)
Create a newPDFImageSet
from the specified URL.PDFImageSet(URLConnection con)
Create a newPDFImageSet
from an already opened URLConnection.PDFImageSet(Collection<BufferedImage> imagelist)
Createa aPDFImageSet
from a pre-existing collection ofBufferedImage
objects
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close all of the images in this Image set.PDFImage
getImage(int page)
Return the specified sub-image from this image as aPDFImage
.int
getNumImages()
Return the number of sub images, or "pages" in this image set.
-
-
-
Constructor Detail
-
PDFImageSet
public PDFImageSet(InputStream in) throws IOException
Create a newPDFImageSet
from the specified InputStream. The stream must contain a recognized Image format - see thePDFImage
class for a list of formats and restrictions. The InputStream is left open, and for streams containing multiple images (i.e. TIFF), it should be left open until all the required images from the file are loaded.- Throws:
IOException
- if the image cannot be loaded or the format cannot be parsedIllegalArgumentException
- if the image cannot be parsed
-
PDFImageSet
public PDFImageSet(File file) throws IOException
Create a newPDFImageSet
from the specified File. The file will not be loaded entirely into memory, but will be kept on disk as a "backing store".- Throws:
IOException
- Since:
- 2.21
-
PDFImageSet
public PDFImageSet(URL url) throws IOException
Create a newPDFImageSet
from the specified URL. If the URL is an HTTP or HTTPS URL and the webserver supports the "Range" header, only the parts of the Image that are required will be downloaded.- Throws:
IOException
- Since:
- 2.21
-
PDFImageSet
public PDFImageSet(URLConnection con) throws IOException
Create a newPDFImageSet
from an already opened URLConnection. This constructor should be used if the connection has already been opened but the connection InputStream not yet read; if the connection hasn't yet been opened, thePDFImageSet(java.net.URL)
constructor is more convenient. Typically you would be calling this after sniffing the content type; for example:URL url = new URL("http://bfo.com/resource"); URLConnection con = url.openConnection(); String type = con.getContentType(); if ("image/tiff".equals(type)) { return new PDFImageSet(con); }
- Throws:
IOException
- Since:
- 2.21
-
PDFImageSet
public PDFImageSet(Collection<BufferedImage> imagelist)
Createa aPDFImageSet
from a pre-existing collection ofBufferedImage
objects- Since:
- 2.26.5
-
-
Method Detail
-
getNumImages
public int getNumImages()
Return the number of sub images, or "pages" in this image set. For all formats but TIFF this will return 1.- Returns:
- the number of pages in this image
-
getImage
public PDFImage getImage(int page) throws IOException
Return the specified sub-image from this image as aPDFImage
. If the requested page is out of range this method returnsnull
. If the specified subimage is corrupt or cannot be parsed, throws anIOException
- Parameters:
page
- the page number, starting at zero- Throws:
IOException
- if the file is corrupt or the image cannot be used
-
close
public void close()
Close all of the images in this Image set. Simply calls thePDFImage.close()
method for all of the images.- Since:
- 2.2.3
-
-