Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

New APIs to handle PDF files in JPedal 6 – Images

1 min read

JPedal6A new API to make JPedal easier to use

When we first wrote JPedal, we added some examples to show how to use the library. Time moves on, and we have continued to add features, making these examples overly complex. So we have now rewritten these to provide easy to use APIs in the Javadoc for JPedal.

In this first article I will cover PDF files and Image handling.

Simple PDF page to image Conversion in JPedal

The most popular use for JPedal is on a server for rasterizing the pages of a PDF file to BufferedImage, for saving as images. Here is how to do this conversion (Javadoc).

ConvertPagesToImages extract=new ConvertPagesToImages("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
     int pageCount=extract.getPageCount();
     for (int page=1; page<=pageCount; page++) {
        BufferedImage image=extract.getPageAsImage(page, isBackgroundTransparent);
     }
 }
 
 extract.closePDFfile();

Highly Configurable PDF page to image Conversion in JPedal

Many of our customers also ask for additional options to create bigger images, create a specific sized image, or make use of any high resolution images to create a higher quality image output. Here is how to do this conversion (Javadoc). The additional HashMap option allows for a large number of additional configuration settings.

ConvertPagesToImages extract=new ConvertPagesToHiResImages extract=new ConvertPagesToHiResImages("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 
//see  https://javadoc.idrsolutions.com/org/jpedal/constants/JPedalSettings.html
 HashMap options=new HashMap(); 
 
 if (extract.openPDFFile()) {
     int pageCount=extract.getPageCount();
     for (int page=1; page<=pageCount; page++) {
 
        BufferedImage image=extract.getPageAsHiResImage(page, isBackgroundTransparent,  options);
     }
 }
 
 extract.closePDFfile();

Extract images from  PDF files with JPedal

PDF files can contain embedded images which are drawn when the PDF is displayed. JPedal provides direct image extraction  (Javadoc).

ExtractImages extract=new ExtractImages("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
     int pageCount=extract.getPageCount();
     for (int page=1; page<=pageCount; page++) {
 
        int imagesOnPageCount=extract.getImageCount(page);
        for (int image=0; image<imagesOnPageCount; image++) {
             BufferedImage image=extract.getImage(page, image, true);
         }
     }
 }
 
 extract.closePDFfile();

Extract Clipped images from  PDF files with JPedal

Images in a PDF often have a clip applied. JPedal allows for the clipped image to be extracted  (Javadoc).

ExtractClippedImages extract=new ExtractClippedImages("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
     int pageCount=extract.getPageCount();
     for (int page=1; page<=pageCount; page++) {
 
        int imagesOnPageCount=extract.getImageCount(page);
        for (int image=0; image<imagesOnPageCount; image++) {
             BufferedImage image=extract.getClippedImage(page, image);
        }
     }
 }
 
 extract.closePDFfile();

As you can see the JPedal API provides a great deal of easy to use functionality with PDF files and Image handling. Are there any other additional features you would like to see?

Next time we look at text in PDF files

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *