English 中文(简体)
iText - N-up
  • 时间:2024-12-27

iText - N-up


Previous Page Next Page  

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.

Nupping Advertisements