利用itext生成复杂pdf

上传人:wt****50 文档编号:36345276 上传时间:2018-03-28 格式:DOCX 页数:7 大小:24.22KB
返回 下载 相关 举报
利用itext生成复杂pdf_第1页
第1页 / 共7页
利用itext生成复杂pdf_第2页
第2页 / 共7页
利用itext生成复杂pdf_第3页
第3页 / 共7页
利用itext生成复杂pdf_第4页
第4页 / 共7页
利用itext生成复杂pdf_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《利用itext生成复杂pdf》由会员分享,可在线阅读,更多相关《利用itext生成复杂pdf(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.it

2、extpdf.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.showTe

3、xtAligned(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,

4、 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/SIMYO

5、U.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.get

6、Font(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 中 Colum

7、nText.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

8、,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 Image image1 = Image.getInstance(“1.jpg“);image1.scaleAbsolute(170f, 100f);/宽、高 PdfPCell c1 = new PdfPCell()

9、; 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(

10、“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“);image2.setAlignment(Image.RIGHT); image2.scaleAbsolute(110f, 110f);/宽、高,值为像素 PdfPCell c2 = new PdfPCell();c2.addElement(image2); /设置图像对齐方式

11、 c2.setBorder(0); / t.addCell(c2);/表格元素 3 parr5 = 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(ne

12、w Paragraph(“ “); for(int i=5;i8;i+) c3.addElement(parri); c3.setBorder(0); t.addCell(c3);/元素 4 PdfPCell 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=

13、new Paragraph(“Portugal“,font2); c5.addElement(new Paragraph(“ “); for(int i=9;i12;i+) c5.addElement(parri); c5.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 Parag

14、raph(“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);/元素 8 PdfPCell c8 = new PdfPCell();PdfPTable tt = new PdfPTable

15、(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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号