《2022年2022年利用itext生成复杂pd》由会员分享,可在线阅读,更多相关《2022年2022年利用itext生成复杂pd(7页珍藏版)》请在金锄头文库上搜索。
1、利用 itext 生成复杂pdf 内容包括:复杂表格的生成(单元格合并,边框,位置布局,背景色)页脚输出,中文字体输出划直线下载包iText-5.0.3.jar import 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.
2、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.showTextAli
3、gned(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, 3
4、0, 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
5、 / 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); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - Font
6、 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,
7、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 Par
8、agraph(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); / 表格元素1 Imag
9、e image1 = Image.getInstance(1.jpg); image1.scaleAbsolute(170f, 100f);/ 宽、高PdfPCell c1 = new PdfPCell(); c1.addElement(image1); Paragraph parr=new Paragraph16; parr0 = new Paragraph(Zycoo EU,font1); parr1 = new Paragraph(LS 21 Armstrong House,First Avenue,font2); parr2 = new Paragraph(Doncaster sout
10、h yorkshire DN9 3GA,font2); parr3 = new Paragraph(United Kingdom,font2); parr4 = new Paragraph(VAT GB2065852514,font2); for(int i=0;i5;i+) c1.addElement(parri); c1.setBorder(0); / 设置单元格边框的无颜色t.addCell(c1); / 表格元素2 Image image2 = Image.getInstance(2.jpg); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - -
11、- - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - image2.setAlignment(Image.RIGHT); image2.scaleAbsolute(110f, 110f);/ 宽、高,值为像素PdfPCell c2 = new PdfPCell(); c2.addElement(image2); / 设置图像对齐方式c2.setBorder(0); / t.addCell(c2); / 表格元素3 parr5 = new Paragraph(Bill To,font2); parr6= new Paragr
12、aph(A Time 2 Change T/A VoIP.PT,font2); parr7= new Paragraph(Rua Consiglieri Pedroso, 123 (Iberfar),font2); parr8= new Paragraph(Edificio H ,font2); PdfPCell c3 = new PdfPCell(); c3.addElement(new Paragraph( ); for(int i=5;i8;i+) c3.addElement(parri); c3.setBorder(0); t.addCell(c3); / 元素 4 PdfPCell
13、c4 = new PdfPCell(); c4.setBorder(0); t.addCell(c4); / 元素 5 PdfPCell c5 = new PdfPCell(); parr9= new Paragraph(Queluz de Baixo,font2); parr10= new Paragraph(2730-501 Barcarena ,font2); parr11= new Paragraph(Portugal,font2); c5.addElement(new Paragraph( ); for(int i=9;i12;i+) c5.addElement(parri); c5
14、.setBorder(0); t.addCell(c5); / 元素 6 PdfPCell c6 = new PdfPCell(); c6.setBorder(0); t.addCell(c6); / 元素 7 PdfPCell c7 = new PdfPCell(); parr12= new Paragraph(Ship To,font2); parr13= new Paragraph(Rua Consiglieri Pedroso, 123,font2); parr14 = new Paragraph(Iberfar),font2); parr15= new Paragraph(Edifi
15、cio H,font2); c7.addElement(new Paragraph( ); for(int i=12;i16;i+) c7.addElement(parri); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - c7.setBorder(0); t.addCell(c7); / 元素 8 PdfPCell c8 = new PdfPCell(); PdfPTable tt = new PdfPTable(2);/向
16、表格单项中镶嵌表格tt.getDefaultCell().setBorder(0); tt.addCell( );tt.addCell( ); tt.addCell( );tt.addCell( ); Paragraph pars=new Paragraph10; PdfPCell cells = new PdfPCell10; pars0= new Paragraph(Invoice Date :,font2); pars1=new Paragraph(09/03/2015,font2); pars2=new Paragraph(Terms :,font2); pars3=new Parag
17、raph(Due on Receipt,font2); pars4=new Paragraph(Due Date :,font2); pars5=new Paragraph(09/03/2015,font2); pars6=new Paragraph(P.O.# :,font2); pars7=new Paragraph(EU VAT - PT507441338,font2); pars8=new Paragraph(EU VAT No. :,font2); pars9=new Paragraph(PT 507441338,font2); for(int i=0;i10;i+) parsi.s
18、etAlignment(2);/设置该段落为靠右显示cellsi=new PdfPCell(); cellsi.setBorder(0); /cell 必须有 ,再 addElement 添加才能设置对齐方式/cell1.setPhrase(para); 是错的cellsi.addElement(parsi); tt.addCell(cellsi); tt.setWidthPercentage(100);/百分比c8.addElement(tt); c8.setVerticalAlignment(Element.ALIGN_RIGHT); c8.setRowspan(2); / 向下合并单元格
19、c8.setBorder(0); t.addCell(c8); / 元素 9 PdfPCell 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
20、(par18); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - - 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);/
21、百分比t2.getDefaultCell().setMinimumHeight(25); t2.getDefaultCell().setBorder(0); / 设置默认最低高度(再加入空格行时候可以直接用);t2.addCell( ); 显示为默认高度/ 向第二张大表中加入元素Font font4=FontFactory.getFont(FontFactory.COURIER,8, Font.BOLD,BaseColor.WHITE); PdfPCell ccs1 = new PdfPCell55; Paragraph ppars1 = new Paragraph55; ppars100=n
22、ew Paragraph(#,font4); ppars101=new Paragraph(Item & Description,font4); ppars102= new Paragraph(Qty,font4); ppars103=new Paragraph(Rate,font4); ppars104=new Paragraph(Amount,font4); ppars110=new Paragraph(1,font2); ppars111=new Paragraph(U20-A202,font2); ppars112= new Paragraph(1,00,font2); ppars11
23、3= new Paragraph(140,00,font2); ppars114=new Paragraph(140,00,font2); ppars120=new Paragraph(2,font2); ppars121=new Paragraph(2GSM,font2); ppars122=new Paragraph(1,00,font2); ppars123=new Paragraph(117,00,font2); ppars124=new Paragraph(117,00,font2); ppars130=new Paragraph(3,font2); ppars131= new Pa
24、ragraph(Coofone D60,font2); ppars132= new Paragraph(5,00,font2); ppars133= new Paragraph(46,80,font2); ppars134= new Paragraph(234,00,font2); ppars140= new Paragraph(4,font2); ppars141= new Paragraph(Shipping,font2); ppars142= new Paragraph(1,00,font2); ppars143=new Paragraph(38,60,font2); ppars144=
25、new Paragraph(38,60,font2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - for(int i=0;i5;i+) for(int j=0;j5;j+) ccs1ij=new PdfPCell(); if(i=0) ccs1ij.setBackgroundColor(BaseColor.DARK_GRAY); if(i=0) ccs1ij.setBorder(0);/ 无边框 else ccs1ij.s
26、etBorderColor(BaseColor.CYAN); ccs1ij.setBorder(Rectangle.BOTTOM);/设置只有下边框NO_BORDER (无边框)、 TOP上边框、 if(j=0) ccs1ij.setMinimumHeight(25);/ 设置行高 (每一行的第一个设置就OK)ppars1ij.setAlignment(1);/居中显示 else if(j=1) ppars1ij.setAlignment(0); else ppars1ij.setAlignment(2);/靠右显示 ccs1ij.addElement(ppars1ij); t2.addCel
27、l(ccs1ij); PdfPCell ccs2 = new PdfPCell52; Paragraph ppars2 = new Paragraph52; ppars200= new Paragraph(Sub Total,font2); ppars201=new Paragraph(529,60,font2); ppars210= new Paragraph(Zero Rate (0%),font2); ppars211=new Paragraph(0,00,font2); ppars220= new Paragraph(Total,font1); ppars2*2+*1+=new Par
28、agraph(?529,60,font1);ppars230= new Paragraph(Payment Made,font2); ppars231=new Paragraph(-) 529,60,font2); ppars240= new Paragraph(Balance Due,font1); ppars2*4+*1+=new Paragraph(?0,00,font1);名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - -
29、for(int i=0;i5;i+) t2.addCell(); t2.addCell(); for(int j=0;j2;j+) ccs2ij=new PdfPCell(); if(j=0) ccs2ij.setColspan(2); / 合并单元格( 2 列)ccs2ij.setBorder(0); / 无边框ppars2ij.setAlignment(2); / 靠右显示ccs2ij.addElement(ppars2ij); t2.addCell(ccs2ij); Paragraph parag =new Paragraph(, FontFactory.getFont( FontFac
30、tory.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)
31、; 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(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -