Open In App

Shrinking the Contents in a PDF using Java

Program to shrink the contents of a PDF document. The external jar file is required to import in the program. Below is the implementation for the same.

Approach:



1. Create an empty PDF file.

2. Create a document that represents the empty PDF file.



3. Repeat the above steps for the original PDF.

4. Get the size of the page from the original PDF.

5. Get the size of the page from the empty PDF.

6. Creating the shrunken version of the original page.

7. Append the shrunken version of the original page to the empty PDF.

8. Create the Document.

Note: External Jar required (Download by clicking here).

Below is the implementation of the shrinking of PDF:




// Java program to shrink the contents of a PDF
  
// Importing the necessary libraries required
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
public class Main {
  
    public static void main(String[] args)
    {
  
        // Try catch block is used to handle File Exceptions
        try {
            // Destination of the empty PDF
            String shrunkenPath = "/home/mayur/newGFG.pdf";
            // Creating PDF writer object
            PdfWriter pdfWriter
                = new PdfWriter(shrunkenPath);
            // Creating a PdfDocument object for empty pdf
            PdfDocument shrunkenDocument
                = new PdfDocument(pdfWriter);
  
            // Destination of the original PDF
            String originalPath = "/home/mayur/GFG.pdf";
            // Creating PDF reader object
            PdfReader pdfReader
                = new PdfReader(originalPath);
            // Creating a PdfDocument object for original
            // pdf
            PdfDocument originalDocument
                = new PdfDocument(pdfReader);
  
            // Opening the first page of the original PDF
            PdfPage orignalPage
                = originalDocument.getPage(1);
            // Getting the height and width of the original
            // PDF
            Rectangle originalPDFSizes
                = orignalPage.getPageSize();
  
            // Adding a new page to the empty PDF
            PdfPage emptyPage
                = shrunkenDocument.addNewPage();
            // Getting the height and width of the empty PDF
            Rectangle emptyPDFsizes
                = emptyPage.getPageSize();
  
            // Scaling down the original Pdf page
            double width = emptyPDFsizes.getWidth()
                           / originalPDFSizes.getWidth();
            double height = emptyPDFsizes.getHeight()
                            / originalPDFSizes.getHeight();
            // Calculating the new width and height
            double newWidth = width / 2;
            double newHeight = height / 2;
            // Creating a matrix with new width and new
            // height
            AffineTransform affineTransform
                = AffineTransform.getScaleInstance(
                    newWidth, newHeight);
  
            // Creating an empty canvas
            PdfCanvas canvas = new PdfCanvas(emptyPage);
            // Adding the matrix created to the empty canvas
            canvas.concatMatrix(affineTransform);
  
            // Copying the content from the original PDF
            PdfFormXObject pageCopy
                = orignalPage.copyAsFormXObject(
                    shrunkenDocument);
            // Adding the copied page to the canvas
            canvas.addXObject(pageCopy, (float)newWidth,
                              (float)newHeight);
  
            // Creating a Document object to make the PDF
            // readable
            Document doc = new Document(shrunkenDocument);
  
            // Closing the documents to prevent memory leaks
            doc.close();
            originalDocument.close();
            System.out.println(
                "Shrunken PDF successfully created");
        }
        // Catching any unwanted Exceptions
        catch (Exception e) {
            System.err.println(e);
        }
    }
}

Before Execution:

Original PDF

After Execution:

PDF after contents are shrunk


Article Tags :