iText导出pdf、word、图片
1、测试例子:
需要导入的外部jar包:

相关API
http://www.coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/package-summary.html
推荐博客相关参考资料:
http://www.cnblogs.com/linjiqin/p/3539283.html
1 import java.awt.Color;
2 import java.io.FileNotFoundException;
3 import java.io.FileOutputStream;
4 import java.net.URL;
5
6 import javax.imageio.ImageIO;
7
8 import sun.font.FontFamily;
9
10 import com.lowagie.text.Cell;
11 import com.lowagie.text.Document;
12 import com.lowagie.text.Font;
13 import com.lowagie.text.FontFactory;
14 import com.lowagie.text.Image;
15 import com.lowagie.text.Paragraph;
16 import com.lowagie.text.Row;
17 import com.lowagie.text.Table;
18 import com.lowagie.text.pdf.BaseFont;
19 import com.lowagie.text.pdf.PdfWriter;
20
21
22 /**
23 * 导出数据:
24 * 输出PDF格式文件:
25 * Created by Jason 2016-7-17 下午5:01:41
26 */
27 public class PrinterTest {
28
29 public static void main(String[] args) {
30
31 /**
32 * 第一步:创建PDF文档对象
33 */
34 Document doc=new Document();
35 try {
36 /**
37 * 第二步:流对象的准备
38 */
39 FileOutputStream fos= new FileOutputStream("测试pdf格式文件.pdf");
40 PdfWriter writer=PdfWriter.getInstance(doc,fos);
41
42 /**
43 * 第三步:打开文档
44 */
45 doc.open();
46 /**
47 * 第四步:设置文档信息输出属性 :下面设置的是中文字体
48 */
49 BaseFont bf=BaseFont.createFont( "STSong-Light", //被封装过的字体类型:字体
50 "UniGB-UCS2-H", //pdf编码格式和pdf数据显示方向:(xxx-H)水平、(xxx-V)垂直
51 BaseFont.NOT_EMBEDDED); //boolean值 意义不大
52
53 Font font=new Font(bf,18,Font.BOLD,Color.green);
54
55 /**
56 * 第五步:写入文档内容
57 */
58
59 /*************************【插入Table表格】******************************************/
60
61 Table table=new Table(3,10);//3列10行
62
63 for (int i = 1; i <=10; i++) {
64 if(i==1){//列标题
65 table.addCell(new Cell("Name"), 0,0);
66 table.addCell(new Cell("Sex"), 0,1);
67 table.addCell(new Cell("Hobby"),0,2);
68 }else {//行数据
69 for (int j = 1; j <=3; j++) {
70 table.addCell(new Cell("("+i+","+j+")"), i-1, j-1);
71 }
72 }
73
74 }
75
76 table.setBorder(6);
77 table.setBorderWidthBottom(2);table.setBorderWidthTop(2);
78 table.setBorderColor(Color.green);
79 table.setBorderColorBottom(Color.red);
80 table.setBorderColorTop(Color.red);
81 table.setAlignment(1);//表格:0:左,1:中,2:右
82 table.setBackgroundColor(Color.lightGray);
83
84 doc.add(table);
85
86
87 doc.add(new Paragraph("\n\n\n"));
88 /*************************【插入Image图片】******************************************/
89 Image png = Image.getInstance("aa.png");
90 png.setAbsolutePosition(140, 140);//定位原点:屏幕左下角为圆心
91 doc.add(png);
92
93 /*************************【插入文本信息】******************************************/
94 doc.add(new Paragraph("\n\n\n"));
95 //写入样式1
96 Paragraph p= new Paragraph("测试标题",font);
97 p.setAlignment(1);
98 doc.add(p);
99
100 //写入样式2 :
101 font.setColor(Color.red);
102 doc.add( new Paragraph("我是小风,我爱彦! \n 信息:123:I am Jason,I love Angle Yan!\n信息:123:I am Jason,I love Angle Yan!",
103 font));;//字体颜色
104
105
106 //提示导出成功!
107 System.out.println("导出成功!");
108 /**
109 * 第六步:关闭文档
110 */
111 doc.close();
112 } catch (FileNotFoundException e) {
113 System.err.println("没有找到文档对象!");
114 e.printStackTrace();
115 } catch (Exception e) {
116 e.printStackTrace();
117 }
118 }
119 }
2、测试输出:

输出样式:

研究技术需要静下心来,一点一点地深究.......




浙公网安备 33010602011771号