com.qoppa.pdfImages
Class PDFDocument

java.lang.Object
  extended bycom.qoppa.pdfImages.PDFDocument

public class PDFDocument
extends Object

The PDFDocument class provides methods to load PDF documents and export or import images from and to the document. The object can export pages or sections of pages as images and can also create new pages from images. Documents can be loaded from the file system, from a URL or from a Java InputStream, or can be created from scratch.

The class can import and export the images using the file system or an output stream in JPEG, TIFF and PNG formats and can also return a BufferedImage object to the calling application.

Author:
Qoppa Software

Constructor Summary
PDFDocument()
          Creates a blank PDFDocument object.
PDFDocument(InputStream inStream, IPassword password)
          Loads a PDF document from an InputStream.
PDFDocument(String fileName, IPassword password)
          Loads a PDF document from a file.
PDFDocument(URL url, IPassword password)
          Loads a PDF document from a URL.
 
Method Summary
 void appendJPEGAsPage(String jpegFileName)
          Creates a new page in the PDF document and adds the JPEG file to it.
 void appendJPEGAsPage(String jpegFileName, int dpi)
          Creates a new page in the PDF document and adds the JPEG file to it.
 void appendPNGAsPage(String pngFileName)
          Creates a new page in the PDF document and adds the PNG file to it.
 void appendTIFFAsPages(InputStream inStream)
          Appends the pages in a TIFF image from an input stream to the end of this document.
 void appendTIFFAsPages(String tiffFileName)
          Appends the pages in a TIFF file to the end of this document.
 Rectangle2D getCropBox(int pageIndex)
          Returns the PDF page's 'CropBox'.
 double getDisplayHeight(int pageIndex)
          Returns the display height of this page in 72 DPI.
 double getDisplayWidth(int pageIndex)
          Returns the display width of this page in 72 DPI.
 DocumentInfo getDocumentInfo()
          Returns a DocumentInfo object containing the information section of a PDF document (author, title, etc.)
 Rectangle2D getMediaBox(int pageIndex)
          Returns the PDF page's 'MediaBox'.
 int getPageCount()
          Returns the number of pages in this document.
 BufferedImage getPageImage(int pageIndex, double resDPI)
          Returns a BufferedImage containing a rendering of the page at the given resolution in DPI.
 BufferedImage getPageImage(int pageIndex, int width, int height)
          Returns a BufferedImage containing a rendering of the page at the given page size.
 BufferedImage getPageSubImage(int pageIndex, Rectangle2D pageArea, double dpi)
          Returns a BufferedImage containing a rendering of part of the page at a given scale.
 double getPaperHeight(int pageIndex)
          Returns the paper's height of this page in 72 DPI.
 double getPaperWidth(int pageIndex)
          Returns the paper's width of this page in 72 DPI.
static String getVersion()
          Returns version string for jPDFSecure.
 void saveDocument(OutputStream outStream)
          Saves this document to an output stream.
 void saveDocument(String fileName)
          Saves this document to a file.
 void saveDocumentAsTIFF(OutputStream outStream, double resDPI, String tiffCompression)
          Save the entire document as a multi-page TIFF image to the given OutputStream.
 void saveDocumentAsTIFF(OutputStream outStream, TIFFOptions options)
          Save the entire document as a multi-page TIFF image to an OutputStream.
 void saveDocumentAsTIFF(String fileName, double resDPI, String tiffCompression)
          Save the entire document as a multi-page TIFF image to the given file.
 void saveDocumentAsTIFF(String fileName, TIFFOptions options)
          Save the entire document as a multi-page TIFF image to the given file.
 void savePageAsJPEG(int pageIndex, OutputStream outStream, double resDPI, float quality)
          Save the rendered image of the page to the given OutputStream in JPEG format.
 void savePageAsJPEG(int pageIndex, String fileName, double resDPI, float quality)
          Save the rendered image of the page to the given file in JPEG format.
 void savePageAsPNG(int pageIndex, OutputStream outStream, double dpi)
          Save the rendered image of the page to the given OutputStream in PNG format.
 void savePageAsPNG(int pageIndex, String fileName, double dpi)
          Save the rendered image of the page to the given OutputStream in PNG format.
 void savePageAsTIFF(int pageIndex, OutputStream outStream, double resDPI, String tiffCompression)
          Save the rendered image of the page to the given OutputStream in TIFF format.
 void savePageAsTIFF(int pageIndex, OutputStream outStream, TIFFOptions options)
          Render a page to an image and save it in TIFF format.
 void savePageAsTIFF(int pageIndex, String fileName, double resDPI, String tiffCompression)
          Save the rendered image of the page to the given file in TIFF format.
 void savePageAsTIFF(int pageIndex, String fileName, TIFFOptions options)
          Render a page to an image and save it in TIFF format.
static boolean setAppletKey(String key, Applet applet)
          Method to unlock the production version of the library.
static boolean setKey(String key)
          Method to unlock the production version of the library.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PDFDocument

public PDFDocument()
            throws PDFException
Creates a blank PDFDocument object. This constructor can be used to create a new document entirely from images.


PDFDocument

public PDFDocument(String fileName,
                   IPassword password)
            throws PDFException
Loads a PDF document from a file.

Parameters:
fileName - Name of the PDF file.
password - An object that provides passwords to open the document, leave null if not needed. When working with documents that have no passwords, the host application should pass null for the value of this parameter. When documents are known to have passwords, the host application should pass an instance of the PDFPassword class that can hold a single password or a list of passwords.

PDFDocument

public PDFDocument(InputStream inStream,
                   IPassword password)
            throws PDFException
Loads a PDF document from an InputStream.

Parameters:
inStream - InputStream to read the PDF document.
password - An object that provides passwords to open the document, leave null if not needed. When working with documents that have no passwords, the host application should pass null for the value of this parameter. When documents are known to have passwords, the host application should pass an instance of the PDFPassword class that can hold a single password or a list of passwords.

PDFDocument

public PDFDocument(URL url,
                   IPassword password)
            throws PDFException
Loads a PDF document from a URL.

Parameters:
url - Location of the PDF document.
password - An object that provides passwords to open the document, leave null if not needed. When working with documents that have no passwords, the host application should pass null for the value of this parameter. When documents are known to have passwords, the host application should pass an instance of the PDFPassword class that can hold a single password or a list of passwords.
Method Detail

getDocumentInfo

public DocumentInfo getDocumentInfo()
Returns a DocumentInfo object containing the information section of a PDF document (author, title, etc.)

Returns:
DocumentInfo Object containing header information for the document.

setKey

public static boolean setKey(String key)
Method to unlock the production version of the library.

Parameters:
key - Production key.

setAppletKey

public static boolean setAppletKey(String key,
                                   Applet applet)
Method to unlock the production version of the library.

Parameters:
key - Production key.

getVersion

public static String getVersion()
Returns version string for jPDFSecure. If this is the demo version, the return string will contain 'Demo Version' at the end.


getPageImage

public BufferedImage getPageImage(int pageIndex,
                                  double resDPI)
                           throws PDFException
Returns a BufferedImage containing a rendering of the page at the given resolution in DPI.

Parameters:
pageIndex - The 0 based page index.
resDPI - Dots per inch at which to render the image. 72 is the PDF native DPI and 100% scale
Returns:
A BufferedImage object of the page at the requested DPI..
Throws:
PDFException

getPageImage

public BufferedImage getPageImage(int pageIndex,
                                  int width,
                                  int height)
Returns a BufferedImage containing a rendering of the page at the given page size. Use this method when the size of the output image is fixed.

Parameters:
pageIndex - The 0 based page index.
width - The width of the desired image, in pixels
height - The height of the desired image, in pixels

getPageSubImage

public BufferedImage getPageSubImage(int pageIndex,
                                     Rectangle2D pageArea,
                                     double dpi)
Returns a BufferedImage containing a rendering of part of the page at a given scale.

Parameters:
pageIndex - The 0 based page index.
pageArea - Rectangle of the area from which the image should be created. This rectangle should be in the page coordinate system, which is at 72 DPI.
dpi - Dots per inch at which to render the image. 72 is the PDF native DPI and 100% scale.
Returns:
A BufferedImage object of a section of the page.

getPaperHeight

public double getPaperHeight(int pageIndex)
Returns the paper's height of this page in 72 DPI. This values comes from the page's MediaBox.

Parameters:
pageIndex - The 0 based page index.
Returns:
double Height of this page

getPaperWidth

public double getPaperWidth(int pageIndex)
Returns the paper's width of this page in 72 DPI. This values comes from the page's MediaBox.

Parameters:
pageIndex - The 0 based page index.
Returns:
double Width of this page

getMediaBox

public Rectangle2D getMediaBox(int pageIndex)
Returns the PDF page's 'MediaBox'. The MediaBox is a rectangle that defines the size of the Media that this page was designed for, this roughly corresponds to the paper size when printing.

Returns:
Rectangle2D This page's MediaBox

getCropBox

public Rectangle2D getCropBox(int pageIndex)
Returns the PDF page's 'CropBox'. The CropBox is a rectangle that is used to crop content before displaying or printing the page. This rectangle is in PDF native coordinates starting at the bottom left and increasing up to the right.

Returns:
Rectangle2D This page's CropBox

getDisplayHeight

public double getDisplayHeight(int pageIndex)
Returns the display height of this page in 72 DPI. This value comes from the page's CropBox

Returns:
double Width of this page

getDisplayWidth

public double getDisplayWidth(int pageIndex)
Returns the display width of this page in 72 DPI. This value comes from the pages CropBox

Returns:
double Width of this page

getPageCount

public int getPageCount()
Returns the number of pages in this document.


savePageAsPNG

public void savePageAsPNG(int pageIndex,
                          OutputStream outStream,
                          double dpi)
                   throws IOException,
                          PDFException
Save the rendered image of the page to the given OutputStream in PNG format.

Parameters:
pageIndex - The 0 based page index.
outStream - The stream to save the image to.
dpi - Dots per inch at which to render the image. 72 is the native PDF resolution and is equivalent to 100% scale.
Throws:
IOException
PDFException

savePageAsPNG

public void savePageAsPNG(int pageIndex,
                          String fileName,
                          double dpi)
                   throws IOException,
                          PDFException
Save the rendered image of the page to the given OutputStream in PNG format.

Parameters:
pageIndex - The 0 based page index.
fileName - The name of the PNG file.
dpi - Dots per inch at which to render the image. 72 is the native PDF resolution and is equivalent to 100% scale.
Throws:
IOException
PDFException

savePageAsJPEG

public void savePageAsJPEG(int pageIndex,
                           OutputStream outStream,
                           double resDPI,
                           float quality)
                    throws IOException,
                           PDFException
Save the rendered image of the page to the given OutputStream in JPEG format.

Parameters:
pageIndex - The 0 based page index.
outStream - The stream to save the image to.
resDPI - Dots per inch at which to render the image. 72 is the native PDF resolution and is equivalent to 100% scale.
quality - The quality setting to use for JPEG compression. The range for this value is 0.0 to 1.0. Lower values mean higher compression and lower quality. As a guideline, 0.25 is low quality, 0.5 is medium quality and 0.75 is high quality.
Throws:
IOException
PDFException

savePageAsJPEG

public void savePageAsJPEG(int pageIndex,
                           String fileName,
                           double resDPI,
                           float quality)
                    throws IOException,
                           PDFException
Save the rendered image of the page to the given file in JPEG format.

Parameters:
pageIndex - The 0 based page index.
fileName - The name of the file to save to.
resDPI - Dots per inch at which to render the image. 72 is the native PDF resolution and is equivalent to 100% scale.
quality - The quality setting to use for JPEG compression. The range for this value is 0.0 to 1.0. Lower values mean higher compression and lower quality. As a guideline, 0.25 is low quality, 0.5 is medium quality and 0.75 is high quality.
Throws:
IOException
PDFException

savePageAsTIFF

public void savePageAsTIFF(int pageIndex,
                           OutputStream outStream,
                           double resDPI,
                           String tiffCompression)
                    throws IOException,
                           PDFException
Save the rendered image of the page to the given OutputStream in TIFF format. resDPI is the resolution in DPI used to render the page, 72 is the native PDF resolution and is equivalent to 100% scale.
This method requires Java Advanced Imaging (JAI) classes in the class path (jai_codec.jar). See our documentation regarding Distribution and JAR files for more info.

If any of the CCITT compression algorithms are used, the image will be converted to black and white before saving.

Parameters:
pageIndex - The 0 based page index.
outStream - The stream to save the image to.
resDPI - Dots per inch at which to render the image.
tiffCompression - The type of TIFF compression to use. This value needs to be one of these predefined values:

TIFFCompression.TIFF_DEFLATE - Deflate lossless compression (Zip-in-TIFF)
TIFFCompression.TIFF_CCITT_RLE - CCITT Modified Huffman RLE
TIFFCompression.TIFF_TIFF_FAX_GROUP3 - CCITT Group 3 fax encoding
TIFFCompression.TIFF_FAX_GROUP4 - CCITT Group 4 fax encoding
TIFFCompression.TIFF_JPEG - JPEG-in-TIFF compression.
TIFFCompression.TIFF_LZW - LZW Compression
TIFFCompression.TIFF_PACKBITS - Byte-oriented run-length encoding "PackBits" compression.
TIFFCompression.TIFF_ZLIB - ZLib Compression.
TIFFCompression.TIFF_NO_COMPRESSION - No compression.
Throws:
IOException
PDFException

savePageAsTIFF

public void savePageAsTIFF(int pageIndex,
                           String fileName,
                           double resDPI,
                           String tiffCompression)
                    throws IOException,
                           PDFException
Save the rendered image of the page to the given file in TIFF format. resDPI is the resolution in DPI used to render the page, 72 is the native PDF resolution and is equivalent to 100% scale.

If any of the CCITT compression algorithms are used, the image will be converted to black and white before saving.

Parameters:
pageIndex - The 0 based page index.
fileName - The name of the file to save the image to.
resDPI - Dots per inch at which to render the image. 72 is the native PDF resolution and is equivalent to 100% scale.
tiffCompression - The type of TIFF compression to use. This value needs to be one of these predefined values:

TIFFCompression.TIFF_DEFLATE - Deflate lossless compression (Zip-in-TIFF)
TIFFCompression.TIFF_CCITT_RLE - CCITT Modified Huffman RLE
TIFFCompression.TIFF_FAX_GROUP3 - CCITT Group 3 fax encoding
TIFFCompression.TIFF_FAX_GROUP4 - CCITT Group 4 fax encoding
TIFFCompression.TIFF_JPEG - JPEG-in-TIFF compression.
TIFFCompression.TIFF_LZW - LZW Compression
TIFFCompression.TIFF_PACKBITS - Byte-oriented run-length encoding "PackBits" compression.
TIFFCompression.TIFF_ZLIB - ZLib Compression.
TIFFCompression.TIFF_NO_COMPRESSION - No compression.
Throws:
IOException
PDFException

savePageAsTIFF

public void savePageAsTIFF(int pageIndex,
                           String fileName,
                           TIFFOptions options)
                    throws IOException,
                           PDFException
Render a page to an image and save it in TIFF format. If the TIFF compression is one of the fax compressions, the image will be converted to black and white automatically.

Parameters:
pageIndex - The 0 based page index.
fileName - The name of the file to save the image to.
options - Options to use when saving the image, such as the TIFF compression type.
Throws:
IOException
PDFException

savePageAsTIFF

public void savePageAsTIFF(int pageIndex,
                           OutputStream outStream,
                           TIFFOptions options)
                    throws IOException,
                           PDFException
Render a page to an image and save it in TIFF format. If the TIFF compression is one of the fax compressions, the image will be converted to black and white automatically.

Parameters:
pageIndex - The 0 based page index.
outStream - The output stream to write the image to.
options - Options to use when saving the image, such as the TIFF compression type.
Throws:
IOException
PDFException

saveDocumentAsTIFF

public void saveDocumentAsTIFF(OutputStream outStream,
                               double resDPI,
                               String tiffCompression)
                        throws PDFException,
                               IOException
Save the entire document as a multi-page TIFF image to the given OutputStream. dpi is the resolution in DPI used to render the pages, 72 is the native PDF resolution and is equivalent to 100% scale.

If any of the CCITT compression algorithms are used, the image will be converted to black and white before saving.

Parameters:
outStream - The stream to save the image to.
resDPI - Dots per inch at which to render the image, 72 is the native PDF resolution and is equivalent to 100% scale..
tiffCompression - The type of TIFF compression to use. This value needs to be one of these predefined values:

TIFFCompression.TIFF_DEFLATE - Deflate lossless compression (Zip-in-TIFF)
TIFFCompression.TIFF_CCITT_RLE - CCITT Modified Huffman RLE
TIFFCompression.TIFF_FAX_GROUP3 - CCITT Group 3 fax encoding
TIFFCompression.TIFF_FAX_GROUP4 - CCITT Group 4 fax encoding
TIFFCompression.TIFF_JPEG - JPEG-in-TIFF compression.
TIFFCompression.TIFF_LZW - LZW Compression
TIFFCompression.TIFF_PACKBITS - Byte-oriented run-length encoding "PackBits" compression.
TIFFCompression.TIFF_ZLIB - ZLib Compression.
TIFFCompression.TIFF_NO_COMPRESSION - No compression.
Throws:
PDFException
IOException

saveDocumentAsTIFF

public void saveDocumentAsTIFF(String fileName,
                               double resDPI,
                               String tiffCompression)
                        throws PDFException,
                               IOException
Save the entire document as a multi-page TIFF image to the given file. resDPI is the resolution in DPI used to render the page, 72 is the native PDF resolution and is equivalent to 100% scale.

Parameters:
fileName - The name of the file to write the TIFF image to.
resDPI - Dots per inch at which to render the image, 72 is the native PDF resolution and is equivalent to 100% scale..
tiffCompression - The type of TIFF compression to use. This value needs to be one of these predefined values:

TIFFCompression.TIFF_DEFLATE - Deflate lossless compression (Zip-in-TIFF)
TIFFCompression.TIFF_CCITT_RLE - CCITT Modified Huffman RLE
TIFFCompression.TIFF_FAX_GROUP3 - CCITT Group 3 fax encoding
TIFFCompression.TIFF_FAX_GROUP4 - CCITT Group 4 fax encoding
TIFFCompression.TIFF_JPEG - JPEG-in-TIFF compression.
TIFFCompression.TIFF_LZW - LZW Compression
TIFFCompression.TIFF_PACKBITS - Byte-oriented run-length encoding "PackBits" compression.
TIFFCompression.TIFF_ZLIB - ZLib Compression.
TIFFCompression.TIFF_NO_COMPRESSION - No compression.
Throws:
PDFException
IOException

saveDocumentAsTIFF

public void saveDocumentAsTIFF(String fileName,
                               TIFFOptions options)
                        throws PDFException,
                               IOException
Save the entire document as a multi-page TIFF image to the given file. This method takes a TIFFOptions object as an argument to describe various TIFF options when saving the file.

Parameters:
fileName - The name of the file to write the TIFF image to.
options - Options to use when saving the TIFF file.
Throws:
PDFException
IOException

saveDocumentAsTIFF

public void saveDocumentAsTIFF(OutputStream outStream,
                               TIFFOptions options)
                        throws PDFException,
                               IOException
Save the entire document as a multi-page TIFF image to an OutputStream. This method takes a TIFFOptions object as an argument to describe various TIFF options when saving the file.

Parameters:
outStream - The stream to save the image to.
options - Options to use when saving the TIFF file.
Throws:
PDFException
IOException

appendTIFFAsPages

public void appendTIFFAsPages(String tiffFileName)
                       throws PDFException,
                              IOException
Appends the pages in a TIFF file to the end of this document. The method will create a new page for each image in a TIFF file and then draw the image onto the page. The page dimensions will be the same as for the TIFF image, calculated using the TIFF dpi value.

Parameters:
tiffFileName - The name of the TIFF file to add to this document.
Throws:
PDFException
IOException

appendTIFFAsPages

public void appendTIFFAsPages(InputStream inStream)
                       throws PDFException,
                              IOException
Appends the pages in a TIFF image from an input stream to the end of this document. The method will create a new page for each image in a TIFF image and then draw the image onto the page. The page dimensions will be the same as for the TIFF image, calculated using the TIFF dpi value.

Parameters:
inStream - The input stream to read the TIFF image from
Throws:
PDFException
IOException

appendJPEGAsPage

public void appendJPEGAsPage(String jpegFileName)
                      throws PDFException,
                             IOException
Creates a new page in the PDF document and adds the JPEG file to it. The method looks at the DPI value in the JPEG image to determine the size of the new page. If not present, the method defaults to 72 DPI.

Parameters:
jpegFileName - The name of the JPEG file to add to this document.
Throws:
PDFException
IOException

appendJPEGAsPage

public void appendJPEGAsPage(String jpegFileName,
                             int dpi)
                      throws PDFException,
                             IOException
Creates a new page in the PDF document and adds the JPEG file to it. The method ignores the DPI value in the file and uses the dpi passed into the method to determine the page size.

Parameters:
jpegFileName - The name of the JPEG file to add to this document.
Throws:
PDFException
IOException

appendPNGAsPage

public void appendPNGAsPage(String pngFileName)
                     throws PDFException,
                            IOException
Creates a new page in the PDF document and adds the PNG file to it. The method looks at the DPI value in the PNG image to determine the size of the new page. If not present, the method defaults to 72 DPI.

Parameters:
pngFileName - The name of the PNG file to add to this document.
Throws:
PDFException
IOException

saveDocument

public void saveDocument(OutputStream outStream)
                  throws IOException,
                         PDFException
Saves this document to an output stream.

Parameters:
outStream - OutputStream where the document will be written to.
Throws:
IOException
PDFException

saveDocument

public void saveDocument(String fileName)
                  throws IOException,
                         PDFException
Saves this document to a file.

Parameters:
fileName - Name of the file to save the document.
Throws:
IOException
PDFException