- iText - Overview
- iText - Creating a PDF Document
- iText - Adding an AreaBreak
- iText - Adding a Paragraph
- iText - Adding a List
- iText Tables
- iText - Adding a Table
- iText - Formatting Cell Contents
- Formatting the Borders of a Cell
- iText - Adding Image to a Table
- iText - Nested Table
- iText - Adding Lists to a Table
- iText Images
- iText - Adding Image to a PDF
- iText - Setting Position of the Image
- iText - Scaling an Image
- iText - Rotating an Image
- iText Annotations
- iText - Text Annotation
- iText - Link Annotation
- iText - Line Annotation
- iText - Markup Annotation
- iText - Circle Annotation
- iText Miscellaneous
- iText - Setting Font
- iText - Shrinking the Content
- iText - Tiling PDF Pages
- iText - N-up
- iText Useful Resources
- iText - Quick Guide
- iText - Useful Resources
- iText - Discussion
iText - Tiling PDF Pages
The following Java program demonstrates how to tile the contents of a PDF page to different pages using the iText library. It creates a PDF document with the name tilingPdfPages.pdf and saves it in the path C:/itextExamples/.
Save this code in a file with the name TilingPDFPages.java.
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;
public class TilingPDFPages {
public static void main(String args[]) throws Exception {
// Creating a PdfWriter object
String dest = "C:/itextExamples/tilingPdfPages.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();
// Getting the size of the page
PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
// Tile size
Rectangle tileSize = PageSize.A4.rotate();
AffineTransform transformationMatrix =
AffineTransform.getScaleInstance(tileSize.getWidth() / orig.getWidth() *
2f, tileSize.getHeight() / orig.getHeight() * 2f);
// The first tile
PdfPage page =
destpdf.addNewPage(PageSize.A4.rotate());
PdfCanvas canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, 0, -orig.getHeight() / 2f);
// The second tile
page = destpdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, -orig.getWidth() / 2f, -orig.getHeight() / 2f);
// The third tile
page = destpdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, 0, 0);
// The fourth tile
page = destpdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, -orig.getWidth() / 2f, 0);
// closing the documents
destpdf.close();
srcPdf.close();
System.out.println("PDF created successfully..");
}
}
Compile and execute the saved Java file from the Command prompt using the following commands −
javac TilingPDFPages.java java TilingPDFPages
Upon execution, the above program creates a PDF document, displaying the following message.
PDF created successfully..
If you verify the specified path, you can find the created PDF document, as shown below −
Advertisements