Class PDFImageSet


  • public final class PDFImageSet
    extends Object

    The PDFImageSet 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 a PDFImage directly.

    Example
       // 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 Detail

      • PDFImageSet

        public PDFImageSet​(InputStream in)
                    throws IOException
        Create a new PDFImageSet from the specified InputStream. The stream must contain a recognized Image format - see the PDFImage 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 parsed
        IllegalArgumentException - if the image cannot be parsed
      • PDFImageSet

        public PDFImageSet​(File file)
                    throws IOException
        Create a new PDFImageSet 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 new PDFImageSet 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 new PDFImageSet 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, the PDFImageSet(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
    • 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 a PDFImage. If the requested page is out of range this method returns null. If the specified subimage is corrupt or cannot be parsed, throws an IOException
        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 the PDFImage.close() method for all of the images.
        Since:
        2.2.3