Jacob Collins Jacob is a Java developer and the product manager of JPedal

How to remove unused objects from PDF file (Tutorial)

42 sec read

How to view pdf metadata using Java (PDF logo)

PDF files sometimes contain objects which are never used. This increases the file size and is wasteful. This tutorial shows you how to reduce the file size by removing unused objects from PDF in Java or using the command line.
 
The PDF file format is not natively supported by Java, so to remove unused objects from a PDF file you will need to download an external library. This tutorial uses JPedal.
 

How to remove unwanted objects from a PDF file programmatically

  1. Add JPedal to your class or module path (download the trial jar)
  2. Create a File handle pointing to the PDF file
  3. Run the below code:

 

Using the command line

  1. Download the JPedal trial jar
  2. Run the following command:
    java -cp jpedal.jar org.jpedal.tools.PdfOptimizer --removeUnusedObjects inputFile outputFile


You can expand your understanding of the PDF format by reading our other articles. Similarly, if there is a specific term for PDF you would like to know more about, our PDF Glossary has an extensive list of common terms.



The JPedal PDF library allows you to solve these problems in Java


Why do developers choose JPedal over alternatives?

  1. Actively developed commercial library with full support and no third party dependencies.
  2. Simple licensing options and source code access for OEM users.
  3. Process PDF files up to 3x faster than alternative Java PDF libraries.

Jacob Collins Jacob is a Java developer and the product manager of JPedal