java使用iText生成pdf表格
转载地址:http://www.open-open.com/code/view/1424011530749
首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< dependency > < groupId >com.lowagie</ groupId > < artifactId >iText</ artifactId > < version >2.1.5</ version > </ dependency > <!--itext生成word文档,需要下面dependency--> < dependency > < groupId >com.lowagie</ groupId > < artifactId >iText-rtf</ artifactId > < version >2.1.4</ version > </ dependency > < dependency > < groupId >com.lowagie</ groupId > < artifactId >iTextAsian</ artifactId > < version >2.1.4</ version > </ dependency > |
如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
package com.iText.create; import java.awt.Color; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; /** */ /** * 功能描述:使用Itext组件创建pdf文档<br> * 创建时间:2010-07-01 * @author sxyx2008 * */ public class CreatePdf { public CreatePdf() throws Exception{ //创建一个文档对象纸张大小为A4 Document doc= new Document(PageSize.A4, 50 , 50 , 50 , 50 ); //设置要输出到磁盘上的文件名称 PdfWriter writer=PdfWriter.getInstance(doc, new FileOutputStream( new File( "徐熙媛.pdf" ))); //设置作者信息 doc.addAuthor( "sxyx2008" ); //设置文档创建日期 doc.addCreationDate(); //设置标题 doc.addTitle( "iText测试" ); //设置值主题 doc.addSubject( "iText" ); //构建页脚 HeaderFooter footer= new HeaderFooter( new Phrase(), true ); //设置页脚是否有边框 //0表示无 //1上边框 //2下边框 //3上下边框都有 默认都有 //设置页脚是否有边框 footer.setBorder( 0 ); //footer.setBorder(1); //footer.setBorder(2); //footer.setBorder(3); //设置页脚的对齐方式 footer.setAlignment(Element.ALIGN_CENTER); //将页脚添加到文档中 doc.setFooter(footer); //打开文档开始写内容 doc.open(); //Paragraph par1=new Paragraph("Hello,Welcome You"); //Paragraph par2=new Paragraph("你好,中文测试",ChineseFont()); /**/ /*par1.setAlignment(Element.ALIGN_CENTER); doc.add(par1);*/ //par2.setAlignment(Element.ALIGN_CENTER); //doc.add(par2); //构建一段落 Paragraph par3= new Paragraph( "客户信息表" ,ChineseFont()); //设置局中对齐 par3.setAlignment(Element.ALIGN_CENTER); //添加到文档 doc.add(par3); //创建一个四列的表格 Table table= new Table( 4 ); //设置边框 table.setBorder( 1 ); //创建表头 Cell cell1= new Cell( new Phrase( "编号" ,ChineseFont())); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_CENTER); cell1.setHeader( true ); cell1.setBackgroundColor(Color.RED); Cell cell2= new Cell( new Phrase( "姓名" ,ChineseFont())); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_CENTER); cell2.setHeader( true ); cell2.setBackgroundColor(Color.RED); Cell cell3= new Cell( new Phrase( "性别" ,ChineseFont())); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_CENTER); cell3.setHeader( true ); cell3.setBackgroundColor(Color.RED); Cell cell4= new Cell( new Phrase( "备注" ,ChineseFont())); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(Element.ALIGN_CENTER); cell4.setHeader( true ); cell4.setBackgroundColor(Color.RED); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); //添加此代码后每页都会显示表头 table.endHeaders(); //循环向表格中添加100条记录 100行4列的表格 //以下代码的作用是创建100行数据,其中每行有四列,列依次为 编号 姓名 性别 备注 for ( int i = 1 ; i <= 100 ; i++) { //设置编号单元格 Cell cell11= new Cell(i+ "" ); //设置姓名单元格 Cell cell22= new Cell( new Phrase( "徐熙媛" ,ChineseFont())); //设置性别单元格 Cell cell33= new Cell( new Phrase( "女" ,ChineseFont())); //设置备注单元格 Cell cell44= new Cell( new Phrase( "好姑娘" ,ChineseFont())); //单元格水平对齐方式 cell11.setHorizontalAlignment(Element.ALIGN_CENTER); //单元格垂直对齐方式 cell11.setVerticalAlignment(Element.ALIGN_CENTER); cell22.setHorizontalAlignment(Element.ALIGN_CENTER); cell22.setVerticalAlignment(Element.ALIGN_CENTER); cell33.setHorizontalAlignment(Element.ALIGN_CENTER); cell33.setVerticalAlignment(Element.ALIGN_CENTER); cell44.setHorizontalAlignment(Element.ALIGN_CENTER); cell44.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell11); table.addCell(cell22); table.addCell(cell33); table.addCell(cell44); } //将表格添加到新的文档 doc.add(table); //创建新的一页 doc.newPage(); //添加图片 Image image=Image.getInstance( "D://Program Files//myeclipseworkspace//6.5//iText//src//5.jpg" ); //添加到文档 doc.add(image); //设置对象方式 image.setAlignment(Element.ALIGN_CENTER); doc.close(); writer.close(); } //pdf文档中文字符处理 public static Font ChineseFont() { BaseFont baseFont= null ; try { baseFont=BaseFont.createFont( "STSong-Light" , "UniGB-UCS2-H" , true ); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Font chineseFont= new Font(baseFont, 8 ,Font.NORMAL,Color.BLUE); return chineseFont; } public static void main(String[] args) { try { new CreatePdf(); } catch (Exception e) { e.printStackTrace(); } } } |
java使用iText生成pdf表格的更多相关文章
- 【PDF】java使用Itext生成pdf文档--详解
[API接口] 一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...
- Java 使用itext生成pdf以及下载
使用方法: 1.需要两个jar包: iText-5.0.6.jar //必须使用该版本,否则缺少相关的方法 TextAsian.jar //是为了文档中正常显示中文所必须引用的包 TextAsi ...
- 关于java poi itext生成pdf文件的例子以及方法
最近正在做导出pdf文件的功能,所以查了了一些相关资料,发现不是很完善,这里做一些小小的感想,欢迎各位“猿”童鞋批评指正. poi+itext,所需要的jar包有itext-2.1.7.jar,poi ...
- Java Itext 生成PDF文件
利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...
- Java使用iText7生成PDF
前言 我们之前使用js库html2canvas + jspdf实现html转PDF.图片,并下载(详情请戳:html页面转PDF.图片操作记录),大致原理是将页面塞到画布里,以图片的方式放到PDF中, ...
- iText生成PDF 格式报表
1.导包 <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artif ...
- MVC 生成PDf表格并插入图片
最近做的项目中有一个功能,将最终的个人信息生成PDF表格,并插入图片.对于没接触过的程序员来说回一片茫然,网上有多种生成PDf的方法,我给大家介绍一下我认为比较简单,好操作的一种. iTextShar ...
- java利用iTextWorker生成pdf
使用itext生成pdf,在linux环境下,中文全部失踪,因为itext要在linux下支持中文字体需要引入itext-asian, 并添加一个字体类. public static class Pd ...
- 在linux环境下使用itext生成pdf
转载请注明出处 https://www.cnblogs.com/majianming/p/9537173.html 项目中需要对订单生成pdf文件,在不断的尝试之后,终于生成了比较满意的pdf文档. ...
随机推荐
- android 调用.NET WebServices
下载Ksoap2.jar, import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.*;import org.ksoap2.tra ...
- 组合,关联,聚合的区别(转自http://jimmyleeee.blog.163.com/blog/static/9309618200932014422932/)
类间关系 在类图中,除了需要描述单独的类的名称.属性和操作外,我们还需要描述类之间的联系,因为没有类是单独存在的,它们通常需要和别的类协作,创造比单独工作更大的语义.在UML类图中,关系用类框之间的连 ...
- 【原创】对Java的synchronized关键字的学习
在Java中,每一个线程都有一个内部锁.当我们使用synchronized关键字时,就是利用这个内部锁来实现线程对某个对象的锁定控制. 那么,如果某个对象中有两个方法,方法一和方法二都使用了synch ...
- MySQL5.5安装出现CMake错误找不到CMakelists.txt原因
今天虚拟机上测试安装 CentOS6.3 + PHP5.4.8 + MySQL5.5.28,结果捣鼓了半天 MySQL都没装上,老是CMake目录下找不到那个 lists 文件,郁闷的不行,最后发现问 ...
- L2-001. 紧急救援
L2-001. 紧急救援 题目链接:https://www.patest.cn/contests/gplt/L2-001 Dijstra 本题是dijstra的拓展,在求最短路的同时,增加了不同的最短 ...
- js--学习方法之-转
既然你找到这篇文章来,说明你是真心想学好JavaScript的.你没有想错,当今如果要开发现代网站或web应用(包括互联网创业),都要学会JavaScript.而面对泛滥的JavaScript在线学习 ...
- UVa 1354 Mobile Computing | GOJ 1320 不加修饰的天平问题 (例题 7-7)
传送门1(UVa): https://uva.onlinejudge.org/external/13/1354.pdf 传送门2(GOJ): http://acm.gdufe.edu.cn/Probl ...
- 为什么使用 SLF4J 而不是 Log4J 来做 Java 日志
转自:为什么使用 SLF4J 而不是 Log4J 来做 Java 日志 英文原文:Why use SLF4J over Log4J for logging in Java 每个Java开发人员都知道日 ...
- 0. Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- tar的打包-压缩与解压缩,并解压到指定的目录
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:-x : 解压缩压缩档案的参数:-z : 是否需 ...