DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Get Plain Text From Common Documents in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create and Edit PDF Annotations in Java
  • How To Add, Remove, or Rotate Pages in a PDF Document Using Java

Trending

  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Rasterize PDFs in Java

How to Rasterize PDFs in Java

This article discusses the advantages and disadvantages of rasterizing PDF documents, and it provides a free API solution to help convert vector PDFs to raster format.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Oct. 06, 23 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

Incorporating a method for converting vector PDFs to raster PDFs in any file upload/download application is a great way to expand its utility. This will make it possible for users to share or download smaller and more secure versions of their PDFs on command — a process that particularly benefits signed contracts and invoices, confidential reports, and other such sensitive materials.

Further down the page, I've provided a free-to-use API solution to help make this conversion in Java. Before we proceed, however, let's first understand the nature of the operation.

What Is PDF Rasterization?

At a high level, rasterization refers to the process of converting any two-dimensional digital content to a pixel-based image display, and it can be used to describe the process of scanning physical documents into a digital format. PDF rasterization involves replacing PDF vector data (meaning data created from a computer-aided design program with individual lines, curves, etc., charted on a graph) and PDF text data (plain text objects) with a pixel-based (dots-per-inch) version of that content. This process differs from PDF to PNG, PDF to JPG, and other common PDF-to-image conversions by rendering a bitmap image within a new PDF file.

What Are the Pros and Cons of Rasterizing PDFs?

There are a few pros and cons to rasterizing a PDF, and making this conversion depends entirely on the use case. For example, one downside of rasterizing PDF content is the drop in quality that we’ll notice when we zoom in on (i.e., increase the size of) our document contents, which makes this operation a poor choice for documents displaying things like architectural layouts or any other fine-detail, interaction-oriented content. Vector images retain quality in closeup view because they don’t rely on static pixel displays to render their visuals, whereas raster images lose quality and appear “pixelated” when they're enlarged in much the same way any bitmap image does. Additionally, once our content is rasterized, we’ll lose the ability to convert that content directly back into its original format; we’ll have to rely on OCR (optical character recognition) solutions to extract text components instead, and we won’t be able to access our original image files, hyperlinks, or other multimedia components.

On the other hand, as a direct consequence of removing text, links, images, and other individual components from the file, rasterization will increase the security of our PDF document considerably. It won’t be possible for unsolicited third parties to make changes to our original content in PDF editing applications because there won’t be any real text, image, or hyperlink information for them to access. The only changes that can be made are regular image editing operations such as cropping, resizing, filtering, adjusting brightness/colors, and so on. Further, raster formatting will increase the compatibility range of our document by making it viewable outside of PDF editing applications, and — particularly in the case of extremely large PDF files — it’ll greatly reduce the document's overall file size, which will make our PDF much less costly to store and share.

Demonstration

In the remainder of this article, I’ll briefly demonstrate a free API that we can use to make vector PDF to raster PDF conversions at scale in our applications. This API will process all document data in memory and release that data upon completion of the conversion to ensure document security.

We can easily structure our PDF Rasterization API call using the ready-to-run Java code examples provided further down the page, and we can authorize our request with a free-tier API key to make up to 800 conversions per month.

Our first step is to install the Java SDK. We can install with Maven by first adding a reference to the repository in pom.xml:

XML
 
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>


And then adding a reference to the dependency in pom.xml:

XML
 
<dependencies>
<dependency>
    <groupId>com.github.Cloudmersive</groupId>
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
    <version>v4.25</version>
</dependency>
</dependencies>


We can now add the imports to the top of our file and call the function directly after. We can include our API key in the Apikey.setApiKey line:

Java
 
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditPdfApi;

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");

EditPdfApi apiInstance = new EditPdfApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
    byte[] result = apiInstance.editPdfRasterize(inputFile);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling EditPdfApi#editPdfRasterize");
    e.printStackTrace();
}


When the operation is complete, we’ll receive the encoding for our new file, and we can write that to a new PDF document.

API Document PDF Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Get Plain Text From Common Documents in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create and Edit PDF Annotations in Java
  • How To Add, Remove, or Rotate Pages in a PDF Document Using Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!