Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Tile a Page Content in a PDF

  • Last Updated : 23 Jun, 2021

In order to tile the pages, we are going to use the iText open-source library here because iText is a world-leading F/OSS PDF library. For Tiling a page content in a PDF, we need some classes iText library. The following are the components used in creating Tiling page content.

  1. PdfReader class which belongs to the package com.itextpdf.text.pdf and is used to read the existing PDF Document.
  2. PdfWriter class belongs to the package com.itextpdf.text.pdf and is used to write content to the document.
  3. PdfDocument class which belongs to the package com.itextpdf.kernel.pdf. By creating this class you have to pass either a writer argument or reader argument.
  4. Document class belongs to the package com.itextpdf.layout. It is one of the core classes in IText. com.itextpdf.text.Document class is used to create the instance.
  5. Rectangle class which belongs to the package com.itextpdf.text. This class is used for setting up border color, filing a particular color, adjusting text to fit inside the rectangle.
  6. pdfCanvas class which belongs to the package com.itextpdf.kernel.pdf.canvas.PdfCanvas. pdfCanvas is used for writing data into page content. call pdfCanvas.release() method after you finished your use. It will save some memory.
  7. Close the document using the close() method as it will save some memory.

Procedure:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  1. Download iText library in the workspace.
  2. Create Java project
  3. Go to configurations and convert this project into a maven project
  4. Add the dependencies to the pom.xml file.
  5. After that create a Java class and build this code by giving a specific path of your PDF files.

Implementation:

PDF before tiling the page content is as follows:



Example

Java




// Java Program to Tile a Page Content in a PDF
 
// Importing input output classes
// Importing required classes from itextpdf
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.PageSize;
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 java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[]) throws Exception
    {
 
        // Creating an object Pdf Writer
        String writing_pdf
            = "C:\Users\HP\Downloads\document.pdf";
        PdfWriter pdf_writer = new PdfWriter(writing_pdf);
 
        // Creating an object Pdf Reader
        String reading_pdf
            = "C:\Users\HP\Downloads\image.pdf";
        PdfReader pdf_reader = new PdfReader(reading_pdf);
 
        // Creating a PdfDocument objects by
        // creating both objects of PdfDocument class
        PdfDocument write = new PdfDocument(pdf_writer);
        PdfDocument read = new PdfDocument(pdf_reader);
 
        // Step 1: Opening a page from the existing PDF
        PdfPage origPage = read.getPage(1); // srcPdf= read
 
        // Step 2: Getting the page size
        Rectangle orig = origPage.getPageSizeWithRotation();
 
        // Step 3: Getting the size of the page
        PdfFormXObject pageCopy
            = origPage.copyAsFormXObject(write);
 
        // Step 4: Getting the size of the tile
        Rectangle Size = PageSize.A4.rotate();
 
        AffineTransform transformationMatrix
            = AffineTransform.getScaleInstance(
                Size.getWidth() / orig.getWidth() * 2f,
                Size.getHeight() / orig.getHeight() * 2f);
 
        // Step 5: Now creating the titles
        PdfPage page
            = write.addNewPage(PageSize.A4.rotate());
 
        // 1st tile
        PdfCanvas obj = new PdfCanvas(page);
        obj.concatMatrix(transformationMatrix);
        obj.addXObject(pageCopy, 0, -orig.getHeight() / 2f);
 
        // 2nd tile
        page = write.addNewPage(PageSize.A4.rotate());
 
        obj = new PdfCanvas(page);
        obj.concatMatrix(transformationMatrix);
        obj.addXObject(pageCopy, -orig.getWidth() / 2f,
                       -orig.getHeight() / 2f);
 
        // 3rd tile
        page = write.addNewPage(PageSize.A4.rotate());
 
        obj = new PdfCanvas(page);
        obj.concatMatrix(transformationMatrix);
        obj.addXObject(pageCopy, 0, 0);
 
        // 4th tile
        page = write.addNewPage(PageSize.A4.rotate());
 
        obj = new PdfCanvas(page);
        obj.concatMatrix(transformationMatrix);
        obj.addXObject(pageCopy, -orig.getWidth() / 2f, 0);
 
        // Step 6: Closing the pdfdocument objects
        // using close() methods, optimizing program by
        // releasing the memory space
        write.close();
        read.close();
 
        // Display message on console only illustrating
        // successful execution of the program
        System.out.println(
            "PDF document is created successfully.");
    }
}

Output: 

Message on the output console.

PDF document is created successfully.

Also, after opening the saved PDF at the specified location you will see tiling the page content in that PDF, the new generated PDF is as follows:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!