PDFOne (for Java™)
Powerful all-in-one PDF library for Java
Compatibility
J2SE J2EE

How To Rasterize A PDF Document In Java

Learn to convert text, shapes, and images in a PDF page as a single graphic item in PDFOne (for Java).
By V. Subhash.

A page in a PDF document can contain any combination of text, images, shapes, and other page elements. Sometimes, it becomes necessary to convert all these page elements as a single image element, say, to prevent text from being copied word by word.

PDFOne (for the Java™ platform) can export PDF pages as images. It can also draw images on PDF pages. Combining these two capabilities, you can convert your existing PDF documents with heterogeneous content to PDF documents with just images. Such documents are rasterized documents.

Here is the code that will do this.

import java.io.File;
import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfReader;
import com.gnostice.pdfone.PdfWriter;


/*
 * PDFOne Java example source code to convert PDF pages to 
 * images and save them back to a PDF.
 * 
 */
public class Store_PDF_Pages_As_Images {

  static {
    PDFOne.activate("your-pdfone-activation-key",
                    "your-pdfone-product-key");
  }    
    
  public static void main(String[] args) 
                       throws IOException, PdfException {

    // Load input document from current directory and 
    // get page count
    PdfReader reader = PdfReader.fileReader(
                         "input_doc.pdf");
    PdfDocument doc1 = new PdfDocument(reader);    
    int n = doc1.getPageCount();
 
    
    // Create output document in current directory
    PdfWriter writer = PdfWriter.fileWriter(
                        "input_doc_pages_converted_to_images.pdf");
    PdfDocument doc2 = new PdfDocument(writer);
        
    // Iterate pages of input document
    for (int i = 1; i <= n; ++i) {
      // Export current page as a JPEG image
      doc1.saveAsImage(
          "jpg",              // format
          String.valueOf(i),  // page number
          "image_of_page",    // image name suffix
          ".");               // save images to current directory
      
      // Access current page
      PdfPage page1 = doc1.getPage(i);

      // Create a new page instance with current page dimensions
      PdfPage page2 = new PdfPage(page1.getWidth(), page1.getHeight());
      
      // Draw exported image on to the new page
      page2.drawImage(
          "image_of_page_" + i + ".jpg", 
          0, 
          0);
      
      // Add new page to output document
      doc2.add(page2);
    }
    
    // Release input document
    reader.dispose();
        
    // Save changes to output document and 
    // release its resources
    doc2.write();    
    writer.dispose();
    
    // Clean up - delete exported images    
    for (int i = 1; i <= n; ++i) {
      File img = new File("image_of_page_" + i + ".jpg");
      
      if (img.exists()) {
        img.delete();
      }
    }
  }
}

This is just one of several ways to do in PDFOne, as there are several overloads and one event handler for the job. The above code example assumes no errors. Please add appropriate try-catch blocks if you move it to production system. Another thing to note is that converting PDF pages to graphics will inevitably increase the PDF file size.

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).