Class PDFImageSet


  • public final class PDFImageSet
    extends java.lang.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 Summary

      Constructors 
      Constructor Description
      PDFImageSet​(java.io.File file)
      Create a new PDFImageSet from the specified File.
      PDFImageSet​(java.io.InputStream in)
      Create a new PDFImageSet from the specified InputStream.
      PDFImageSet​(java.net.URL url)
      Create a new PDFImageSet from the specified URL.
      PDFImageSet​(java.net.URLConnection con)
      Create a new PDFImageSet from an already opened URLConnection.
      PDFImageSet​(java.util.Collection<java.awt.image.BufferedImage> imagelist)
      Createa a PDFImageSet from a pre-existing collection of BufferedImage 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 a PDFImage.
      int getNumImages()
      Return the number of sub images, or "pages" in this image set.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PDFImageSet

        public PDFImageSet​(java.io.InputStream in)
                    throws java.io.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:
        java.io.IOException - if the image cannot be loaded or the format cannot be parsed
        java.lang.IllegalArgumentException - if the image cannot be parsed
      • PDFImageSet

        public PDFImageSet​(java.io.File file)
                    throws java.io.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:
        java.io.IOException
        Since:
        2.21
      • PDFImageSet

        public PDFImageSet​(java.net.URL url)
                    throws java.io.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:
        java.io.IOException
        Since:
        2.21
      • PDFImageSet

        public PDFImageSet​(java.net.URLConnection con)
                    throws java.io.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:
        java.io.IOException
        Since:
        2.21
      • PDFImageSet

        public PDFImageSet​(java.util.Collection<java.awt.image.BufferedImage> imagelist)
        Createa a PDFImageSet from a pre-existing collection of BufferedImage 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 java.io.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:
        java.io.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