Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Edited on

     

Compress PDF Images in Java

The high quality of the images is often the cause for the large size of a PDF document. Compressing the high-quality images can dramatically reduce the file size by 50% to 90%. In this article, I am going to introduce how to compress PDF images in the following two aspects:

  • Automatically compress high-quality images only
  • Compress images to a certain extent

Installing Spire.Pdf.jar

If you use Maven, you can easily import the Spire.Pdf.jar in your application by adding the following code to your project’s pom.xml file. For non-Maven projects, download the jar file fromthis link and manually add it as a dependency in your application.

<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>http://repo.e-iceblue.com/nexus/content/groups/public/</url></repository></repositories><dependencies><dependency><groupId>e-iceblue</groupId><artifactId>spire.pdf</artifactId><verson>4.8.7</version></dependency></dependencies>
Enter fullscreen modeExit fullscreen mode

Example 1. Compress high-quality images only

Spire.PDF offers PdfPageBase.getImagesInfo() method to retrieve image information from a specific page. The image information includes the image index which is returned by PdfImageInfo.getIndex() method. Then, you can use PdfPageBase.tryCompressImage(int imgIndex) method to compress the specified image.

Kindly note that tryCompressImage() method will compress the high-resolution images only. Low-quality images will no longer be compressed.

importcom.spire.pdf.PdfDocument;importcom.spire.pdf.PdfPageBase;importcom.spire.pdf.exporting.PdfImageInfo;publicclassCompressHighQualityImage{publicstaticvoidmain(String[]args){//Load the sample PDF documentPdfDocumentdoc=newPdfDocument("C:\\Users\\Administrator\\Desktop\\Images.pdf");//Set IncrementalUpdate to falsedoc.getFileInfo().setIncrementalUpdate(false);//Declare a PdfPageBase variablePdfPageBasepage;//Loop through the pagesfor(inti=0;i<doc.getPages().getCount();i++){//Get the specific pagepage=doc.getPages().get(i);if(page!=null){if(page.getImagesInfo()!=null){//Loop through the images in the pagefor(PdfImageInfoinfo:page.getImagesInfo()){//Use tryCompressImage method the compress high-resolution imagespage.tryCompressImage(info.getIndex());}}}}//Save to filedoc.saveToFile("output/Compressed.pdf");}}
Enter fullscreen modeExit fullscreen mode

CompressHighQuality

Example 2. Compress images to a certain extent

After the image information is obtained from a PDF page, we’re able to create a PdfBitmap object based on a specific image. Use the PdfBitmap.setQuality() method to reduce the image quality to a certain extent, and then use the PdfPageBase.replaceImage() method to replace the old image with the compressed image in your document.

importcom.spire.license.LicenseProvider;importcom.spire.pdf.*;importcom.spire.pdf.exporting.PdfImageInfo;importcom.spire.pdf.graphics.PdfBitmap;publicclassCompressAllImages{publicstaticvoidmain(String[]args){//Load the sample PDF documentPdfDocumentdoc=newPdfDocument("C:\\Users\\Administrator\\Desktop\\Images.pdf");//Set IncrementalUpdate to falsedoc.getFileInfo().setIncrementalUpdate(false);//Loop through the pagesfor(inti=0;i<doc.getPages().getCount();i++){//Get the specific pagePdfPageBasepage=doc.getPages().get(i);//Get image info from the pagePdfImageInfo[]images=page.getImagesInfo();//Determine if there is any image in the pgaeif(images!=null&&images.length>0)//Loop through the imagesfor(intj=0;j<images.length;j++){//Get the specific imagePdfImageInfoimage=images[j];PdfBitmapbp=newPdfBitmap(image.getImage());//Reset the image qualitybp.setQuality(40);//Replace the old image with the compressed imagepage.replaceImage(j,bp);}}//Save to filedoc.saveToFile("output/CompressAll.pdf",FileFormat.PDF);doc.close();}}
Enter fullscreen modeExit fullscreen mode

CompressToExtent

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your Office Development Master
  • Location
    Chengdu, SiChuan, China
  • Work
    Dev at E-iceblue
  • Joined

More fromE-iceblue Product Family

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp