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.
- PdfReader class which belongs to the package com.itextpdf.text.pdf and is used to read the existing PDF Document.
- PdfWriter class belongs to the package com.itextpdf.text.pdf and is used to write content to the document.
- 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.
- 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.
- 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.
- 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.
- Close the document using the close() method as it will save some memory.
Procedure:
- Download iText library in the workspace.
- Create Java project
- Go to configurations and convert this project into a maven project
- Add the dependencies to the pom.xml file.
- 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: