利用itext生成复杂pdf.docx_第1页
利用itext生成复杂pdf.docx_第2页
利用itext生成复杂pdf.docx_第3页
利用itext生成复杂pdf.docx_第4页
利用itext生成复杂pdf.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

利用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 Paragraph16;parr0 = new Paragraph(Zycoo EU,font1);parr1 = new Paragraph(LS 21 Armstrong House,First Avenue,font2);parr2 = new Paragraph(Doncaster south 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);/表格元素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);/表格元素3parr5 = new Paragraph(Bill To,font2);parr6= new Paragraph(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);/元素4PdfPCell c4 = new PdfPCell();c4.setBorder(0);t.addCell(c4);/元素5PdfPCell 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.setBorder(0);t.addCell(c5);/元素6PdfPCell c6 = new PdfPCell();c6.setBorder(0);t.addCell(c6);/元素7PdfPCell 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(Edificio H,font2);c7.addElement(new Paragraph( );for(int i=12;i16;i+)c7.addElement(parri);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 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 Paragraph(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.setAlignment(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); /向下合并单元格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 PdfPCell55;Paragraph ppars1 = new Paragraph55;ppars100=new 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);ppars113= 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 Paragraph(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=new Paragraph(38,60,font2);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);/无边框elseccs1ij.setBorderColor(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);elseppars1ij.setAlignment(2);/靠右显示ccs1ij.addElement(ppars1ij);t2.addCell(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);ppars221=new Paragraph(529,60,font1);ppars230= new Paragraph(Payment Made,font2);ppars231=new Paragraph(-) 529,60,font2);ppars240= new Paragraph(Balance Due,font1);ppars241=new Paragraph(0,00,font1);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.setBor

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论