[java] itext 生成表格
itext的简单实例
package com.wa.poi.pdf; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Anchor; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; /** * 生成pdf文档 * @author Administrator * * 字符串中的中文被忽略问题待解决... */ public class DocumentPdf { public void genePDF() throws DocumentException, IOException{ //创建文档实例 Document document = new Document (PageSize.A4, 10,10,10, 10); //创建 PdfWriter 对象,生成的目标文件 PdfWriter pdfWriter = PdfWriter.getInstance (document,new FileOutputStream(new File("F:/test.pdf"))); //打开文档开始写入内容 document.open(); ; BaseFont baseFont =BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //创建段落对象 Anchor anchor = new Anchor("My First pdf 文档实例"); anchor.setName("pdf document"); Paragraph paragraph = new Paragraph(); Font font = new Font(baseFont, 20,Font.BOLD); paragraph.setFont(font); //设置段前间距 paragraph.setSpacingBefore(20); paragraph.add(anchor); //将段落放入文档对象中 document.add(paragraph); document.add(new Paragraph("itext is a poplular pdf document tools for web develope")); document.close(); System.out.println("pdf文档生成成功"); } public static void main (String[] args) throws DocumentException, IOException{ new DocumentPdf().genePDF(); } }
利用itext生成pdf文档。
package com.wa.poi.pdf; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; public class Report { private static List<String> list=new ArrayList<String>(); static{ list.add("阿里科技"); list.add("腾讯"); list.add("吕田集团"); list.add("SUN"); list.add("MIT"); list.add("NDDI"); list.add("稻盛和夫"); list.add("丰田一郎"); list.add(null); list.add("丰田一郎"); } public void gene() throws Exception{ //创建文档,设置页面格式为A4纸 Document document =new Document(PageSize.A4, 10, 10,10, 10); PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream (new File("F:/信用审查报告.pdf"))); //开启文档 document.open(); //设置字体 BaseFont baseFont =BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //新建段落 Paragraph paragraph = new Paragraph("信用审查报告",new Font(baseFont,20,Font.BOLD)); //设置段前间距 paragraph.setSpacingBefore(2); //设置段落居中 paragraph.setAlignment(Element.ALIGN_CENTER); //设置字体 Font font1 = new Font(baseFont,12,Font.NORMAL); document.add(paragraph); //创建表格 Table table = new Table(4); //设置边框 table.setBorder(1); //新建单元格 Cell cell1 = new Cell(new Paragraph("工商注册号",font1)); //设置文本居中 cell1.setVerticalAlignment(Element.ALIGN_CENTER); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); Cell cell2 = new Cell(new Paragraph("企业名称",font1)); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); Cell cell3 = new Cell(new Paragraph("组织机构代码",font1)); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); Cell cell4 = new Cell(new Paragraph("发证机关",font1)); cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); //表格中添加单元格 table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); for (int i=0;i<list.size();i++){ Cell cell = new Cell(new Paragraph(list.get(i),font1)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); } //将表格添加到文档中 document.add(table); //关闭文档 document.close(); System.out.println("测试审核报告"); } public static void main(String[] args) throws Exception { new Report().gene(); } }

参考资料:
http://8176726.blog.163.com/blog/static/519664742010025105935995/
http://lucifer-li6.iteye.com/blog/1637687
http://www.cnblogs.com/crazyjava/p/3199936.html
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号