iText Tutorial
iText Introduction
iText Tables
iText Images
iText Annotations
iText Canvas
iText Miscellaneous
iText Useful Resources
Selected Reading
iText Introduction
- iText - Adding a List
- iText - Adding a Paragraph
- iText - Adding an AreaBreak
- iText - Creating a PDF Document
- iText - Overview
iText Tables
- iText - Adding Lists to a Table
- iText - Nested Table
- iText - Adding Image to a Table
- Formatting the Borders of a Cell
- iText - Formatting Cell Contents
- iText - Adding a Table
iText Images
- iText - Rotating an Image
- iText - Scaling an Image
- iText - Setting Position of the Image
- iText - Adding Image to a PDF
iText Annotations
- iText - Circle Annotation
- iText - Markup Annotation
- iText - Line Annotation
- iText - Link Annotation
- iText - Text Annotation
iText Canvas
iText Miscellaneous
iText Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
iText - N-up
iText - N-up
The following Java program demonstrates how to perform N-up on a PDF page using the iText pbrary. It creates a PDF document with the name nUppingPDF.pdf and saves it in the path C:/itextExamples/
Save this code in a file with the name NUppingPDF.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; pubpc class NUppingPDF { pubpc static void main(String args[]) throws Exception { // Creating a PdfWriter object String dest = "C:/itextExamples/nUppingPDF.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); Rectangle orig = origPage.getPageSize(); PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf); // N-up page PageSize nUpPageSize = PageSize.A4.rotate(); PdfPage page = destpdf.addNewPage(nUpPageSize); PdfCanvas canvas = new PdfCanvas(page); // Scale page AffineTransform transformationMatrix = AffineTransform.getScaleInstance( nUpPageSize.getWidth() / orig.getWidth() / 2f, nUpPageSize.getHeight() / orig.getHeight() / 2f); canvas.concatMatrix(transformationMatrix); // Add pages to N-up page canvas.addXObject(pageCopy, 0, orig.getHeight()); canvas.addXObject(pageCopy, orig.getWidth(), orig.getHeight()); canvas.addXObject(pageCopy, 0, 0); canvas.addXObject(pageCopy, orig.getWidth(), 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 NUppingPDF.java java NUppingPDF
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