环境需求:
                iText-2.1.7.jar
                iTextAsian.jar
示例代码:

 

 
  
  1. package com.iText.create;  
  2. import java.awt.Color;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import com.lowagie.text.Cell;  
  7. import com.lowagie.text.Document;  
  8. import com.lowagie.text.DocumentException;  
  9. import com.lowagie.text.Element;  
  10. import com.lowagie.text.Font;  
  11. import com.lowagie.text.HeaderFooter;  
  12. import com.lowagie.text.Image;  
  13. import com.lowagie.text.PageSize;  
  14. import com.lowagie.text.Paragraph;  
  15. import com.lowagie.text.Phrase;  
  16. import com.lowagie.text.Table;  
  17. import com.lowagie.text.pdf.BaseFont;  
  18. import com.lowagie.text.pdf.PdfWriter;  
  19. /** *//**  
  20.  * 功能描述:使用Itext组件创建pdf文档<br>  
  21.  * 创建时间:2010-07-01  
  22.  * @author sxyx2008  
  23.  *  
  24.  */ 
  25. public class CreatePdf {  
  26.     public CreatePdf() throws Exception{  
  27.           
  28.         //创建一个文档对象纸张大小为A4  
  29.         Document doc=new Document(PageSize.A4,50,50,50,50);  
  30.         //设置要输出到磁盘上的文件名称  
  31.         PdfWriter writer=PdfWriter.getInstance(doc,new FileOutputStream(new File("徐熙媛.pdf")));  
  32.         //设置作者信息  
  33.         doc.addAuthor("sxyx2008");  
  34.         //设置文档创建日期  
  35.         doc.addCreationDate();  
  36.         //设置标题  
  37.         doc.addTitle("iText测试");  
  38.         //设置值主题  
  39.         doc.addSubject("iText");  
  40.           
  41.         //构建页脚  
  42.         HeaderFooter footer=new HeaderFooter(new Phrase(), true);  
  43.         //设置页脚是否有边框  
  44.         //0表示无  
  45.         //1上边框  
  46.         //2下边框  
  47.         //3上下边框都有 默认都有  
  48.         //设置页脚是否有边框  
  49.         footer.setBorder(0);  
  50.         //footer.setBorder(1);  
  51.         //footer.setBorder(2);  
  52.         //footer.setBorder(3);  
  53.         //设置页脚的对齐方式  
  54.         footer.setAlignment(Element.ALIGN_CENTER);  
  55.         //将页脚添加到文档中  
  56.         doc.setFooter(footer);  
  57.           
  58.         //打开文档开始写内容  
  59.         doc.open();  
  60.         //Paragraph par1=new Paragraph("Hello,Welcome You");  
  61.         //Paragraph par2=new Paragraph("你好,中文测试",ChineseFont());  
  62.         /**//*par1.setAlignment(Element.ALIGN_CENTER);  
  63.         doc.add(par1);*/ 
  64.         //par2.setAlignment(Element.ALIGN_CENTER);  
  65.         //doc.add(par2);  
  66.           
  67.         //构建一段落  
  68.         Paragraph par3=new Paragraph("客户信息表",ChineseFont());  
  69.         //设置局中对齐  
  70.         par3.setAlignment(Element.ALIGN_CENTER);  
  71.         //添加到文档  
  72.         doc.add(par3);  
  73.           
  74.         //创建一个四列的表格  
  75.         Table table=new Table(4);  
  76.         //设置边框  
  77.         table.setBorder(1);  
  78.           
  79.         //创建表头  
  80.           
  81.         Cell cell1=new Cell(new Phrase("编号",ChineseFont()));  
  82.         cell1.setHorizontalAlignment(Element.ALIGN_CENTER);  
  83.         cell1.setVerticalAlignment(Element.ALIGN_CENTER);  
  84.         cell1.setHeader(true);  
  85.         cell1.setBackgroundColor(Color.RED);  
  86.           
  87.           
  88.         Cell cell2=new Cell(new Phrase("姓名",ChineseFont()));  
  89.         cell2.setHorizontalAlignment(Element.ALIGN_CENTER);  
  90.         cell2.setVerticalAlignment(Element.ALIGN_CENTER);  
  91.         cell2.setHeader(true);  
  92.         cell2.setBackgroundColor(Color.RED);  
  93.           
  94.         Cell cell3=new Cell(new Phrase("性别",ChineseFont()));  
  95.         cell3.setHorizontalAlignment(Element.ALIGN_CENTER);  
  96.         cell3.setVerticalAlignment(Element.ALIGN_CENTER);  
  97.         cell3.setHeader(true);  
  98.         cell3.setBackgroundColor(Color.RED);  
  99.           
  100.         Cell cell4=new Cell(new Phrase("备注",ChineseFont()));  
  101.         cell4.setHorizontalAlignment(Element.ALIGN_CENTER);  
  102.         cell4.setVerticalAlignment(Element.ALIGN_CENTER);  
  103.         cell4.setHeader(true);  
  104.         cell4.setBackgroundColor(Color.RED);  
  105.           
  106.           
  107.         table.addCell(cell1);  
  108.         table.addCell(cell2);  
  109.         table.addCell(cell3);  
  110.         table.addCell(cell4);  
  111.         //添加此代码后每页都会显示表头  
  112.         table.endHeaders();  
  113.           
  114.           
  115.         //循环向表格中添加100条记录 100行4列的表格  
  116.           
  117.         //以下代码的作用是创建100行数据,其中每行有四列,列依次为 编号 姓名 性别 备注  
  118.         for (int i = 1; i <=100; i++) {  
  119.               
  120.             //设置编号单元格  
  121.             Cell cell11=new Cell(i+"");  
  122.             //设置姓名单元格  
  123.             Cell cell22=new Cell(new Phrase("徐熙媛",ChineseFont()));  
  124.             //设置性别单元格  
  125.             Cell cell33=new Cell(new Phrase("女",ChineseFont()));  
  126.             //设置备注单元格  
  127.             Cell cell44=new Cell(new Phrase("好姑娘",ChineseFont()));  
  128.               
  129.             //单元格水平对齐方式  
  130.             cell11.setHorizontalAlignment(Element.ALIGN_CENTER);  
  131.             //单元格垂直对齐方式  
  132.             cell11.setVerticalAlignment(Element.ALIGN_CENTER);  
  133.               
  134.             cell22.setHorizontalAlignment(Element.ALIGN_CENTER);  
  135.             cell22.setVerticalAlignment(Element.ALIGN_CENTER);  
  136.               
  137.             cell33.setHorizontalAlignment(Element.ALIGN_CENTER);  
  138.             cell33.setVerticalAlignment(Element.ALIGN_CENTER);  
  139.               
  140.             cell44.setHorizontalAlignment(Element.ALIGN_CENTER);  
  141.             cell44.setVerticalAlignment(Element.ALIGN_CENTER);  
  142.               
  143.               
  144.             table.addCell(cell11);  
  145.             table.addCell(cell22);  
  146.             table.addCell(cell33);  
  147.             table.addCell(cell44);  
  148.               
  149.         }  
  150.           
  151.         //将表格添加到新的文档  
  152.         doc.add(table);  
  153.         //创建新的一页  
  154.         doc.newPage();  
  155.         //添加图片  
  156.         Image p_w_picpath=Image.getInstance("D://Program Files//myeclipseworkspace//6.5//iText//src//5.jpg");  
  157.         //添加到文档  
  158.         doc.add(p_w_picpath);  
  159.         //设置对象方式  
  160.         p_w_picpath.setAlignment(Element.ALIGN_CENTER);  
  161.           
  162.         doc.close();  
  163.         writer.close();  
  164.     }  
  165.       
  166.     //pdf文档中文字符处理  
  167.     public static Font ChineseFont()  
  168.     {  
  169.         BaseFont baseFont=null;  
  170.         try {  
  171.             baseFont=BaseFont.createFont("STSong-Light","UniGB-UCS2-H"true);  
  172.         } catch (DocumentException e) {  
  173.             e.printStackTrace();  
  174.         } catch (IOException e) {  
  175.             e.printStackTrace();  
  176.         }  
  177.         Font chineseFont=new Font(baseFont,8,Font.NORMAL,Color.BLUE);  
  178.         return chineseFont;  
  179.     }  
  180.       
  181.       
  182.     public static void main(String[] args) {  
  183.         try {  
  184.             new CreatePdf();  
  185.         } catch (Exception e) {  
  186.             e.printStackTrace();  
  187.         }  
  188.     }