利用itext生成复杂pdf

  • 格式:docx
  • 大小:24.22 KB
  • 文档页数:7

利用itext生成复杂pdf内容包括:复杂表格的生成(单元格合并,边框,位置布局,背景色)页脚输出,中文字体输出划直线下载包iText-5.0.3.jarimport java.io.FileOutputStream;import java.io.IOException;import .MalformedURLException;import com.itextpdf.text.*;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.CMYKColor;import com.itextpdf.text.pdf.ColumnText;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import com.itextpdf.text.pdf.draw.LineSeparator;public class ITextTest{//public void onEndPage(PdfWriter pdfWriter, Document document) {// ColumnText.showTextAligned(pdfWriter.getDirectContent(),// Element.ALIGN_CENTER,new Phrase(String.format("page %d", //设置页码// pdfWriter.getPageNumber())),300,50,0);// }public static void main(String[] args) throws DocumentException,MalformedURLException, IOException {// 1、实例化文档对象Document document = new Document(PageSize.A4, 37, 30, 50, 40);// 2、创建PdfWriter 对象(其他编写器包括HtmlWriter、RtfWriter、XmlWriter)PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("d:\\new.pdf"));//生成的pdf存放位置document.open();//中文字体格式//BaseFont bfChinese = BaseFont.createFont("SIMYOU.TTF", //SIMYOU.TTF在本地目录下去拷贝C:/WINDOWS/Fonts/SIMYOU.TTF//BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);//Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);Font font1=FontFactory.getFont(FontFactory.COURIER,8, Font.BOLD,BaseColor.BLACK);Font font2=FontFactory.getFont(FontFactory.COURIER,8, Font.NORMAL,BaseColor.BLACK);Font font3=FontFactory.getFont(FontFactory.COURIER,9, Font.NORMAL,BaseColor.GRAY);Font font5=FontFactory.getFont(FontFactory.COURIER,8, Font.NORMAL,BaseColor.BLACK);//设置页脚LineSeparator line=new LineSeparator(0.5f,1.3f,BaseColor.CYAN,Element.ALIGN_CENTER,0);//直线Chunk c=new Chunk(line); //line不能直接放入Paragraph或Phrase中ColumnText.showTextAligned(writer.getDirectContent(),1,new Paragraph(String.format("Notes"),font3),55,55,0);//通过坐标控制显示位置ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER,new Paragraph(c),300,50,0);//设置页码//ColumnText.showTextAligned(writer.getDirectContent(),//Element.ALIGN_CENTER,new Phrase(String.format("page %d",//writer.getPageNumber())),300,50,0);//document.newPage();PdfPTable t = new PdfPTable(2);//创建表格t.setWidthPercentage(100);//百分比t.setSpacingBefore(20);//表格元素1Image image1 = Image.getInstance("1.jpg");image1.scaleAbsolute(170f, 100f);//宽、高PdfPCell c1 = new PdfPCell();c1.addElement(image1);Paragraph []parr=new Paragraph[16];parr[0] = new Paragraph("Zycoo EU",font1);parr[1] = new Paragraph("LS 21 Armstrong House,First Avenue",font2);parr[2] = new Paragraph("Doncaster south yorkshire DN9 3GA",font2);parr[3] = new Paragraph("United Kingdom",font2);parr[4] = new Paragraph("VAT GB2065852514",font2);for(int i=0;i<5;i++){c1.addElement(parr[i]);}c1.setBorder(0); //设置单元格边框的无颜色t.addCell(c1);//表格元素2Image image2 = Image.getInstance("2.jpg");image2.setAlignment(Image.RIGHT);image2.scaleAbsolute(110f, 110f);//宽、高,值为像素PdfPCell c2 = new PdfPCell();c2.addElement(image2); //设置图像对齐方式c2.setBorder(0); //t.addCell(c2);//表格元素3parr[5] = new Paragraph("Bill To",font2);parr[6]= new Paragraph("A Time 2 Change T/A VoIP.PT",font2);parr[7]= new Paragraph("Rua Consiglieri Pedroso, 123 (Iberfar)",font2); parr[8]= new Paragraph("Edificio H ",font2);PdfPCell c3 = new PdfPCell();c3.addElement(new Paragraph(" "));for(int i=5;i<8;i++)c3.addElement(parr[i]);c3.setBorder(0);t.addCell(c3);//元素4PdfPCell c4 = new PdfPCell();c4.setBorder(0);t.addCell(c4);//元素5PdfPCell c5 = new PdfPCell();parr[9]= new Paragraph("Queluz de Baixo",font2);parr[10]= new Paragraph("2730-501 Barcarena ",font2);parr[11]= new Paragraph("Portugal",font2);c5.addElement(new Paragraph(" "));for(int i=9;i<12;i++)c5.addElement(parr[i]);c5.setBorder(0);t.addCell(c5);//元素6PdfPCell c6 = new PdfPCell();c6.setBorder(0);t.addCell(c6);//元素7PdfPCell c7 = new PdfPCell();parr[12]= new Paragraph("Ship To",font2);parr[13]= new Paragraph("Rua Consiglieri Pedroso, 123",font2);parr[14] = new Paragraph("(Iberfar)",font2);parr[15]= new Paragraph("Edificio H",font2);c7.addElement(new Paragraph(" "));for(int i=12;i<16;i++)c7.addElement(parr[i]);c7.setBorder(0);t.addCell(c7);//元素8PdfPCell c8 = new PdfPCell();PdfPTable tt = new PdfPTable(2);//向表格单项中镶嵌表格tt.getDefaultCell().setBorder(0);tt.addCell(" ");tt.addCell(" ");tt.addCell(" ");tt.addCell(" ");Paragraph []pars=new Paragraph[10];PdfPCell []cells = new PdfPCell[10];pars[0]= new Paragraph("Invoice Date :",font2);pars[1]=new Paragraph("09/03/2015",font2);pars[2]=new Paragraph("Terms :",font2);pars[3]=new Paragraph("Due on Receipt",font2);pars[4]=new Paragraph("Due Date :",font2);pars[5]=new Paragraph("09/03/2015",font2);pars[6]=new Paragraph("P.O.# :",font2);pars[7]=new Paragraph("EU VAT - PT507441338",font2);pars[8]=new Paragraph("EU VAT No. :",font2);pars[9]=new Paragraph("PT 507441338",font2);for(int i=0;i<10;i++){pars[i].setAlignment(2);//设置该段落为靠右显示cells[i]=new PdfPCell();cells[i].setBorder(0);//cell必须有,再addElement添加才能设置对齐方式//cell1.setPhrase(para);是错的cells[i].addElement(pars[i]);tt.addCell(cells[i]);}tt.setWidthPercentage(100);//百分比c8.addElement(tt);c8.setVerticalAlignment(Element.ALIGN_RIGHT);c8.setRowspan(2); //向下合并单元格c8.setBorder(0);t.addCell(c8);//元素9PdfPCell c9 = new PdfPCell();Paragraph par17 = new Paragraph("Queluz de Baixo",font2); Paragraph par18 = new Paragraph("2730-501 Barcarena",font2); Paragraph par19 = new Paragraph("Portugal",font2);c9.addElement(new Paragraph(" "));c9.addElement(par17);c9.addElement(par18);c9.addElement(par19);c9.setBorder(0);t.addCell(c9);//第二张大表PdfPTable t2 = new PdfPTable(5);//创建表格int width[] = {8,47,10,10,25};//设置每列宽度比例t2.setWidths(width);t2.setWidthPercentage(100);//百分比t2.getDefaultCell().setMinimumHeight(25);t2.getDefaultCell().setBorder(0);//设置默认最低高度(再加入空格行时候可以直接用);t2.addCell(" ");显示为默认高度//向第二张大表中加入元素Font font4=FontFactory.getFont(FontFactory.COURIER,8, Font.BOLD,BaseColor.WHITE);PdfPCell [][]ccs1 = new PdfPCell[5][5];Paragraph [][]ppars1 = new Paragraph[5][5];ppars1[0][0]=new Paragraph("#",font4);ppars1[0][1]=new Paragraph("Item & Description",font4);ppars1[0][2]= new Paragraph("Qty",font4);ppars1[0][3]=new Paragraph("Rate",font4);ppars1[0][4]=new Paragraph("Amount",font4);ppars1[1][0]=new Paragraph("1",font2);ppars1[1][1]=new Paragraph("U20-A202",font2);ppars1[1][2]= new Paragraph("1,00",font2);ppars1[1][3]= new Paragraph("140,00",font2);ppars1[1][4]=new Paragraph("140,00",font2);ppars1[2][0]=new Paragraph("2",font2);ppars1[2][1]=new Paragraph("2GSM",font2);ppars1[2][2]=new Paragraph("1,00",font2);ppars1[2][3]=new Paragraph("117,00",font2);ppars1[2][4]=new Paragraph("117,00",font2);ppars1[3][0]=new Paragraph("3",font2);ppars1[3][1]= new Paragraph("Coofone D60",font2);ppars1[3][2]= new Paragraph("5,00",font2);ppars1[3][3]= new Paragraph("46,80",font2);ppars1[3][4]= new Paragraph("234,00",font2);ppars1[4][0]= new Paragraph("4",font2);ppars1[4][1]= new Paragraph("Shipping",font2);ppars1[4][2]= new Paragraph("1,00",font2);ppars1[4][3]=new Paragraph("38,60",font2);ppars1[4][4]=new Paragraph("38,60",font2);for(int i=0;i<5;i++){for(int j=0;j<5;j++){ccs1[i][j]=new PdfPCell();if(i==0){ccs1[i][j].setBackgroundColor(BaseColor.DARK_GRAY);}if(i==0){ccs1[i][j].setBorder(0);//无边框}else{ccs1[i][j].setBorderColor(BaseColor.CYAN);ccs1[i][j].setBorder(Rectangle.BOTTOM);//设置只有下边框NO_BORDER(无边框)、TOP上边框、}if(j==0){ccs1[i][j].setMinimumHeight(25);// 设置行高(每一行的第一个设置就OK)ppars1[i][j].setAlignment(1);//居中显示}else if(j==1){ppars1[i][j].setAlignment(0);}else{ppars1[i][j].setAlignment(2);//靠右显示}ccs1[i][j].addElement(ppars1[i][j]);t2.addCell(ccs1[i][j]);}}PdfPCell [][]ccs2 = new PdfPCell[5][2];Paragraph [][]ppars2 = new Paragraph[5][2];ppars2[0][0]= new Paragraph("Sub Total",font2);ppars2[0][1]=new Paragraph("529,60",font2);ppars2[1][0]= new Paragraph("Zero Rate (0%)",font2);ppars2[1][1]=new Paragraph("0,00",font2);ppars2[2][0]= new Paragraph("Total",font1);ppars2[2][1]=new Paragraph("€529,60",font1);ppars2[3][0]= new Paragraph("Payment Made",font2);ppars2[3][1]=new Paragraph("(-) 529,60",font2);ppars2[4][0]= new Paragraph("Balance Due",font1);ppars2[4][1]=new Paragraph("€0,00",font1);for(int i=0;i<5;i++){t2.addCell("");t2.addCell("");for(int j=0;j<2;j++){ccs2[i][j]=new PdfPCell();if(j==0)ccs2[i][j].setColspan(2); //合并单元格(2列)ccs2[i][j].setBorder(0); //无边框ppars2[i][j].setAlignment(2); //靠右显示ccs2[i][j].addElement(ppars2[i][j]);t2.addCell(ccs2[i][j]);}}Paragraph parag =new Paragraph("", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new CMYKColor(0,255, 255, 17)));parag.add(t);parag.add(t2);document.add(parag);document.newPage();//生成新页ColumnText.showTextAligned(writer.getDirectContent(),//设置页脚Element.ALIGN_CENTER,new Paragraph(c),300,50,0);document.add(new Paragraph("Bank Details",font5));document.add(new Paragraph("Bank of Scotland, 600, GORGIE ROAD, EDINBURGH, EH11 3XP.",font5));document.add(new Paragraph("BAN - GB57BOFS80226013044960",font5));document.add(new Paragraph("Swift -BOFSGB2S",font5));document.add(new Paragraph(" "));document.add(new Paragraph(" "));document.add(new Paragraph("Terms & Conditions",font3));document.add(new Paragraph("Payment in advance of shipmen",font5));document.close();}}。