package com.slin.utils; /** * Created by Administrator on 2015/5/4. */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class T { public static void main(String[] args) throws Exception{ Document pdfDoc = new Document(); FileOutputStream pdfFile = new FileOutputStream(new File("D:/Temp/firstPdf.pdf")); Image image = Image.getInstance("D:/Temp/1.jpg"); PdfWriter.getInstance(pdfDoc, pdfFile); pdfDoc.open(); PdfPTable pdfPTable=null; int index=2; int a=index%5; int b=index/5; System.out.println("a==="+a); System.out.println("b==="+b); if(index<5){ pdfPTable=new PdfPTable(index); pdfPTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER); for(int i=0;i<index;i++){ pdfPTable.addCell(image); pdfDoc.add(pdfPTable); } }else { for(int i=0;i<index;i++){ if(i%5==0){ pdfPTable=new PdfPTable(5); pdfPTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER); } if(b*5==i){ System.out.println("i="+i); pdfPTable=new PdfPTable(a); pdfPTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER); } pdfPTable.addCell(image); pdfDoc.add(pdfPTable); } } pdfDoc.close(); } }
itext 创建pdf
最新推荐文章于 2024-04-25 15:32:23 发布