📜  iText-平铺PDF页面

📅  最后修改于: 2020-10-16 02:59:21             🧑  作者: Mango


以下Java程序演示了如何使用iText库将PDF页面的内容平铺到其他页面。它创建一个名为tilingPdfPages.pdf的PDF文档,并将其保存在路径C:/ itextExamples /中

将此代码保存在名为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..");
   }
}

使用以下命令从命令提示符处编译并执行保存的Java文件-

javac TilingPDFPages.java 
java TilingPDFPages

执行后,上述程序将创建一个PDF文档,并显示以下消息。

PDF created successfully..

如果验证指定的路径,则可以找到创建的PDF文档,如下所示-

平铺PDF页面