iText - Shrinking the Content


Advertisements

In this chapter, we will see how to scale an image on a PDF document using the iText library.

Shrinking the Content in a PDF

Following are the steps to shrink the contents of a PDF page using iText library.

Step 1: Creating a PdfWriter and PdfReader object

The PdfWriter class represents the DocWriter for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of this class accepts a string, representing the path of the file where the PDF is to be created.

Instantiate the PdfWriter class by passing a string value (representing the path where you need to create a PDF) to its constructor, as shown below.

// Creating a PdfWriter object 
String dest = "C:/itextExamples/shrinking.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

To read data from an existing pdf, create a PdfReader object as shown below.

// Creating a PdfReader 
String src = "C:/itextExamples/pdfWithImage.pdf"; 
PdfReader reader = new PdfReader(src); 

Step 2: Creating a PdfDocument object(s)

The PdfDocument class is the class that represents the PDF Document in iText. This class belongs to the package com.itextpdf.kernel.pdf. To instantiate this class (in writing mode), you need to pass an object of the class PdfWriter to its constructor.

Create source and destination PDF documents by passing the PdfWriter and PdfReader objects to the constructors, as shown below.

// Creating a PdfDocument objects 
PdfDocument destpdf = new PdfDocument(writer);         
PdfDocument srcPdf = new PdfDocument(reader); 

Step 3: Opening a page from the existing PDF

Get a page from the source PDF using the getPage() method of the PdfPage class. Using this object, get the size of the page of the source document, as shown below.

// Opening a page from the existing PDF 
PdfPage origPage = srcPdf.getPage(1);       

// Getting the page size 
Rectangle orig = origPage.getPageSizeWithRotation(); 

Step 4: Shrinking the contents of the source pdf

Using the getScaleInstance() method of the AffineTransform class, shrink the contents of a page of the source document, as shown below.

// Shrink original page content using transformation matrix 
AffineTransform transformationMatrix = AffineTransform.getScaleInstance(    
   page.getPageSize().getWidth()/ orig.getWidth()/2,    
   page.getPageSize().getHeight()/ orig.getHeight()/2); 

Step 5: Copying the page

Concatenate the affine transform matrix, created in the previous step, to the matrix of the canvas object of the destination PDF document, as shown below.

// Concatenating the affine transform matrix to the current matrix 
PdfCanvas canvas = new PdfCanvas(page);       
canvas.concatMatrix(transformationMatrix); 

Now, add the page copy to the canvas object of the destination PDF to the source document, as shown below.

// Add the object to the canvas 
PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf); 
canvas.addXObject(pageCopy, 0, 0); 

Step 6: Creating the Document object

The Document class of the package com.itextpdf.layout is the root element while creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument.

Instantiate the Document class by passing the object of the class PdfDocument, as shown below.

// Creating a Document   
Document document = new Document(destpdf); 

Step 7: Closing the Document

Close the document using the close() method of the Document class, as shown below.

// Closing the document 
document.close();

Example

The following Java program demonstrates how to shrink contents of a PDF page using the iText library. It creates a PDF document with name shrinkingPDF.pdf, shrinks the image in the pdf, and saves it in the path C:/itextExamples/

Save this code in a file with name ShrinkingPDF.java.

import com.itextpdf.kernel.geom.AffineTransform; 
import com.itextpdf.kernel.geom.Rectangle; 

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage; 
import com.itextpdf.kernel.pdf.PdfReader; 
import com.itextpdf.kernel.pdf.PdfWriter; 
import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 

import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; 
import com.itextpdf.layout.Document;  

public class ShrinkPDF {    
   public static void main(String args[]) throws Exception {
      // Creating a PdfWriter object
      String dest = "C:/itextExamples/shrinking.pdf";
      PdfWriter writer = new PdfWriter(dest);
      
      // Creating a PdfReader
      String src = "C:/itextExamples/pdfWithImage.pdf";
      PdfReader reader = new PdfReader(src);
      
      // Creating a PdfDocument objects
      PdfDocument destpdf = new PdfDocument(writer);
      PdfDocument srcPdf = new PdfDocument(reader);
         
      // Opening a page from the existing PDF 
      PdfPage origPage = srcPdf.getPage(1);
         
      // Getting the page size
      Rectangle orig = origPage.getPageSizeWithRotation();
         
      // Adding a page to destination Pdf
      PdfPage page = destpdf.addNewPage();
         
      // Scaling the image in a Pdf page     
      AffineTransform transformationMatrix = AffineTransform.getScaleInstance(
         page.getPageSize().getWidth()/orig.getWidth()/2,
         page.getPageSize().getHeight()/ orig.getHeight()/2);
      
      // Shrink original page content using transformation matrix
      PdfCanvas canvas = new PdfCanvas(page);
      canvas.concatMatrix(transformationMatrix);
      
      // Add the object to the canvas
      PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
      canvas.addXObject(pageCopy, 0, 0);
      
      // Creating a Document object
      Document doc = new Document(destpdf);
      
      // Closing the document
      doc.close();
      
      System.out.println("Table created successfully..");
   }
}      

Compile and execute the saved Java file from the command prompt using the following commands −

javac ShrinkingPDF.java 
java ShrinkingPDF

Upon execution, the above program creates a PDF document, displaying the following message.

Table created successfully..

If you verify the specified path, you can find the created PDF document as shown below.

Shrinking
Advertisements