Java/ Compress PDF Document

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
import com.spire.pdf.*;

public class CompressPDF {
public static void main(String[] args) {

//Load a sample PDF document
PdfDocument document = new PdfDocument();
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

//Compress the content in the PDF document
document.getFileInfo().setIncrementalUpdate(false);
document.setCompressionLevel(PdfCompressionLevel.Best);

//Save the document to file
document.saveToFile("output/CompressPDFcontent.pdf", FileFormat.PDF);
}
}
import com.spire.pdf.*;
import com.spire.pdf.exporting.PdfImageInfo;
import com.spire.pdf.graphics.PdfBitmap;

public class CompressPDFImage {
public static void main(String[] args) {

//Load a sample PDF document
PdfDocument document = new PdfDocument();
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

//Compress the image quality in the PDF document
document.getFileInfo().setIncrementalUpdate(false);

for (int i = 0; i < document.getPages().getCount(); i++) {

PdfPageBase page = document.getPages().get(i);
PdfImageInfo[] images = page.getImagesInfo();
if (images != null && images.length > 0)
for (int j = 0; j < images.length; j++) {
PdfImageInfo image = images[j];
PdfBitmap bp = new PdfBitmap(image.getImage());
bp.setQuality(20);
page.replaceImage(j, bp);

}
}

//Save the document to file
document.saveToFile("output/CompressPDFImage.pdf", FileFormat.PDF);
}
}

--

--

--

Sharing Java Code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Leet Code] Rings and Rods

QIR Optimization Assessment Tool

How to dockerize a Rails application

New QOL updates for Overlays, AlertBox and ChatBot

1. Why Kotlin for Android?

Functional retry handling with cats-retry in Scala

Crodo. What can I get tokens for?

Sinatra — Movies catalog (Part-1)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

multi threading in java

Create simple Spring-Boot application

Introduction to Java Programming(JVM,JRE,JDK)

Database connectivity with java 🤯, using mysql…