PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

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
    PdfDocument doc1 = new PdfDocument();    
    doc1.load("input_doc.pdf");
    int n = doc1.getPageCount();
 
    
    // Create output document in current directory
    PdfDocument doc2 = new PdfDocument();
        
    // 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
    doc1.close();
        
    // Save changes to output document and 
    // release its resources
    doc2.save("input_doc_pages_converted_to_images.pdf");    
    doc2.close();
    
    // 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.

Did you add JAI and JAI Image IO libraries?

Java framework supports only a few image formats. For converting to other formats, you need to add the Java Advanced Imaging and Java Advanced Imaging Image IO libraries to your project.

You can download the JARs from:

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our Java developer tools
XtremeDocumentStudio (for Java)

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

PDFOne (for Java)

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

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

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