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.
- Assign the path of the empty PDF to a String variable.
- Import PdfWriter from the package com.itextpdf.kernel.pdf. (PdfWriter allows us to write content on our PDF file)
- PdfWriter accepts a String variable as its parameter which represents the destination of the PDF.
- Initialize the PdfWriter object by passing the path of the PDF file.
2. Create a document that represents the empty PDF file.
- Import PdfDocument from the package com.itextpdf.kernel.pdf. (PdfDocument is used to represent the pdf in code. This later can be used to add or modify various features such as font, images, etcetera)
- PdfDocument accepts a PdfWriter or PdfReader object as it’s parameter.
- Initialize the PdfDocument by passing the PdfWriter object.
3. Repeat the above steps for the original PDF.
- Assign the path of the original PDF to a String variable.
- Import PdfReader from the package com.itextpdf.kernel.pdf. (PdfReader allows us to read the content on our PDF file)
- Pass the path of the original PDF to the PdfReader constructor.
- Initialize the PdfDocument by passing the PdfReader object.
4. Get the size of the page from the original PDF.
- Import PdfPage from the package com.itextpdf.kernel.pdf. (PdfPage represents a particular page in a PDF)
- Import Rectangle from the package com.itextpdf.kernel.geom.
- The method getPage(int pageNumber) present in the PdfDocument class returns a PdfPage object of the particular page specified.
- The method getPageSize() present in the PdfPage class returns a Rectangle object of the particular PdfPage object.
5. Get the size of the page from the empty PDF.
- It is not possible to get the page size from an empty PDF.
- So to get the size, we add a new page to the empty PDF using the addNewPage() method present in the PdfDocument class. addNewPage() methods returns a PdfPage object.
- The method getPageSize() present in the PdfPage class returns a Rectangle object of the particular PdfPage object.
6. Creating the shrunken version of the original page.
- Import AffineTransform from the package com.itextpdf.kernel.geom.
- New scaled width can be calculated using (emptyPageWidth/originalPageWidth)/2.
- New scaled height can be calculated using (emptyPageHeight/originalPageHeight)/2.
- The static method getScaleInstance(double width, double height) present in the AffineTransform class returns an AffineTransform object with scaled width and scaled height.
7. Append the shrunken version of the original page to the empty PDF.
- Import PdfCanvas from the package com.itextpdf.kernel.pdf.canvas.
- Initialize the PdfCanvas object by passing in the empty page as the parameter.
- Add the above-created matrix with scaled downsizes to the empty canvas.
- Copy the content from the original page using the method copyAsFormXObject(PdfDocument shrunkenDocument)(returns a PdfFormXObject) present in the PdfPage class.
- Add the copied page to the canvas using the method addXObject(PdfXObject xObject, float x, float y).
8. Create the Document.
- Import Document from the package com.itextpdf.layout.
- A Document object is created to make a readable version of the PDF.
- One of the constructors of the Document class accepts PdfDocument object as it’s parameters.
- Initialize the Document object by passing the shrunkenDocument as it’s parameter.
- The document is closed after creating the object to prevent memory leaks.
Note: External Jar required (Download by clicking here).
Below is the implementation of the shrinking of PDF:
Java
// 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:
After Execution: