Class ConvertPagesToImages


  • public class ConvertPagesToImages
    extends Object

    Image Extraction from PDF files


    This class provides a simple Java API to extract pages as images from a PDF file and also a static convenience method if you just want to dump all the pages as images from a PDF file or directory containing PDF files isBackgroundTransparent MUST be false for generating JPEG images

    Note: It is recommended to resort to ConverPagesToHiResImages as the first choice for generating better quality images when converting with non-default settings.
    See our Support Page for Examples on Convert PDF pages to Images
    There is a simpler example (org.jpedal.examples.images.ConvertPagesToHiResImages) for producing higher res images of pages (but likely to be slower).
    • Field Detail

      • maxPageCount

        public static int maxPageCount
        used as part of test to limit pages to first 10 - please do not use
    • Constructor Detail

      • ConvertPagesToImages

        public ConvertPagesToImages​(String fileName)
        Sets up an ConvertPagesToImages instance to open a PDF File
        Parameters:
        fileName - full path to a single PDF file
      • ConvertPagesToImages

        public ConvertPagesToImages​(byte[] byteArray)
        Sets up an ConvertPagesToImages instance to open a PDF file contained as a BLOB within a byte[] stream (do not pad with additional empty bytes)
        Parameters:
        byteArray - file's BLOB
    • Method Detail

      • writeAllPagesAsImagesToDir

        public static void writeAllPagesAsImagesToDir​(String inputDir,
                                                      String outDir,
                                                      String format,
                                                      float pageScaling)
                                               throws PdfException
        convenience static method to convert PDF file or directory of files
        Parameters:
        inputDir - directory of files to convert
        outDir - directory of output
        format - format of images
        pageScaling - scaling
        Throws:
        PdfException - PdfException
      • setPageScaling

        public void setPageScaling​(float pageScaling)
        alter page scaling (default is 1.33f which gives same size as Acrobat at 100)
        Parameters:
        pageScaling - scaling
      • main

        public static void main​(String[] args)
        main routine which checks parameters passed in and runs the conversion
        Parameters:
        args - arguments
      • getPageAsImage

        public BufferedImage getPageAsImage​(int page,
                                            boolean isBackgroundTransparent)
                                     throws PdfException
        Parameters:
        page - Logical page number in PDF (first page is 1)
        isBackgroundTransparent - defines if BufferedImage has a white or transparent background
        Returns:
        BufferedImage of PDF page
        Throws:
        PdfException - is any issues decoding PDF file
      • getPageAsImage

        public BufferedImage getPageAsImage​(int page)
                                     throws PdfException
        Parameters:
        page - Logical page number in PDF (first page is 1)
        Returns:
        BufferedImage of PDF page with white background
        Throws:
        PdfException - is any issues decoding PDF file
      • setPassword

        public void setPassword​(String password)
        sets the Owner or User Password to use when opening encrypted PDF file
        Parameters:
        password - the USER or OWNER password for the PDF file
      • getPageCount

        public int getPageCount()
        number of pages in PDF file (starting at 1)
        Returns:
        page count
      • openPDFFile

        public boolean openPDFFile()
                            throws PdfException
        routine to open the PDF File so we can access - needs to be checked as will be false if file cannot be opened for any reason
        Returns:
        true if successful
        Throws:
        PdfException - is problem opening file
      • closePDFfile

        public void closePDFfile()
        ensure PDF file is closed once no longer needed and all resources released