Class ConvertPagesToImages

java.lang.Object
org.jpedal.examples.images.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 Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
    used as part of test to limit pages to first 10 - please do not use
  • Constructor Summary

    Constructors
    Constructor
    Description
    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)
    Sets up an ConvertPagesToImages instance to open a PDF File
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    ensure PDF file is closed once no longer needed and all resources released
    getPageAsImage(int page)
     
    getPageAsImage(int page, boolean isBackgroundTransparent)
     
    int
    number of pages in PDF file (starting at 1)
    static void
    main(String[] args)
    main routine which checks parameters passed in and runs the conversion
    boolean
    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
    void
    setFitToSize(int[] dimensions)
    output image to fit a certain size (respecting aspect ratio) Value set will override any setPageScaling() setting
    void
    setPageScaling(float pageScaling)
    alter page scaling (default is 1.33f which gives same size as Acrobat at 100)
    void
    setPassword(String password)
    sets the Owner or User Password to use when opening encrypted PDF file
    static void
    writeAllPagesAsImagesToDir(String inputDir, String outDir, String format, float pageScaling)
    convenience static method to convert PDF file or directory of files
    static void
    writeAllPagesAsImagesToDir(String inputDir, String outDir, String format, float pageScaling, String password)
    convenience static method to convert PDF file with password or directory of files
    static void
    writeAllPagesAsImagesToDir(String inputDir, String outDir, String format, int[] dimensions)
    convenience static method to convert PDF file or directory of files to a set output size

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • maxPageCount

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

    • 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 Details

    • 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
    • writeAllPagesAsImagesToDir

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

      public static void writeAllPagesAsImagesToDir(String inputDir, String outDir, String format, int[] dimensions) throws PdfException
      convenience static method to convert PDF file or directory of files to a set output size
      Parameters:
      inputDir - directory of files to convert
      outDir - directory of output
      format - format of images
      dimensions - int[]{width, height}
      Throws:
      PdfException - PdfException
    • setPageScaling

      public void setPageScaling(float pageScaling)
      alter page scaling (default is 1.33f which gives same size as Acrobat at 100)

      if setFitToSize(final int[] dimensions) is also set, this value will be ignored

      Parameters:
      pageScaling - scaling
    • setFitToSize

      public void setFitToSize(int[] dimensions)
      output image to fit a certain size (respecting aspect ratio) Value set will override any setPageScaling() setting
      Parameters:
      dimensions - int[]{width, heigth} in pixels
    • 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