Class PdfOptimizer

java.lang.Object
org.jpedal.tools.PdfOptimizer

public class PdfOptimizer extends Object
Utility class with static methods to optimize PDF files
  • Constructor Details

    • PdfOptimizer

      public PdfOptimizer()
  • Method Details

    • loadFile

      public void loadFile(File input) throws IOException
      Load a PDF file to be optimized
      Parameters:
      input - File object for the PDF to be optimized
      Throws:
      IOException - Thrown if the file is corrupted or invalid in some way
    • closePDF

      public void closePDF() throws IOException
      Closes the PDF file that is current loaded
      Throws:
      IOException - Thrown if an I/O error occurs
    • getoptimizedFileAsBytes

      public byte[] getoptimizedFileAsBytes() throws IOException
      Create a new PDF from the result of the original final after any optimizations have been performed.
      Returns:
      The optimized file as a byte array
      Throws:
      IOException - Thrown if an I/O error occurs
    • writeoptimizedFileToStream

      public void writeoptimizedFileToStream(OutputStream outputStream) throws IOException
      Write the optimized file to the provided stream.
      Parameters:
      outputStream - OutputStream the optimised file is written to
      Throws:
      IOException - Thrown if an I/O error occurs
    • optimizePDF

      public static void optimizePDF(File input, File output, List<PdfOptimizer.Optimization> optimizations) throws IOException
      Save a copy of the specified file after optimization. The file will be optimized using the specified optimizations
      Parameters:
      input - File object representing the PDF containing forms
      output - File object representing where the modified PDF should be saved
      optimizations - a list of all the optimizations to perform
      Throws:
      IOException - if an I/O error occurs during writing
    • optimizePDF

      public static void optimizePDF(File input, File output, PdfOptimizer.Optimization optimization) throws IOException
      Save a copy of the specified file after optimization. The file will be optimized using the specified optimization
      Parameters:
      input - File object representing the PDF containing forms
      output - File object representing where the modified PDF should be saved
      optimization - the optimization to perform
      Throws:
      IOException - if an I/O error occurs during writing
    • optimizePDF

      public static void optimizePDF(File input, File output) throws IOException
      Save a copy of the specified file after optimization. The file will be optimized using all currently available optimization options which may change over time.
      Parameters:
      input - File object representing the PDF containing forms
      output - File object representing where the modified PDF should be saved
      Throws:
      IOException - if an I/O error occurs during writing
    • removeUnusedObjects

      public void removeUnusedObjects() throws IOException
      Remove any unused object from the PDF and flag the objects as free in the files xref table
      Throws:
      IOException - if an I/O error occurs
    • main

      public static void main(String[] args)
      Optimize a PDF and save the output to the specified file. The file will be optimized using all currently available optimization options which may change over time.
      Parameters:
      args - Expects a String array of 2 values. the first being the filename for the input PDF, and the second being the filename for the output