1.创建pdf文件:
建maven项目,首先导jar
pom.xml文科配置
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.1.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.1.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.1.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.1.4</version>
</dependency>
<!-- Itext PDF 结束 -->
package com.senqi.pdf;
import java.awt.Color;
import java.awt.Font;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
public class Test1 {
public static void main(String[] args) throws Exception {
// 创建一个pdf输出流
PdfWriter writer = new PdfWriter("d:\\my.pdf");
// 创建一个pdf的文档
PdfDocument doc = new PdfDocument(writer);
// 添加pdf的文档属性
PdfDocumentInfo info = doc.getDocumentInfo();
info.setTitle("我是标题--成都");
info.setSubject("我是主题");
info.setAuthor("森淇");
info.setCreator("创建者");
Document result = new Document(doc);
// 创建字体对象,通过设置pdf对中文的解析编码
PdfFont font = PdfFontFactory.createFont("STSongStd-Light","UniGB-UCS2-H");
// 将字体对象 和 要输出的文档关联
result.setFont(font);
// 创建一个段落对象
Paragraph pa = new Paragraph("This is My pdf----你好,我也是pdf的内容");
// 居中
pa.setTextAlignment(TextAlignment.CENTER);
//pa.setFontColor(Color.BLACK);
// 文字的大小
pa.setFontSize(40);
// 往文档中添加 内容
result.add(pa);
// 将doc的文档关闭,关闭时流中的内容会自动写入文档
doc.close();
System.out.println("pdf 创建OK");
}
}
2.创建pdf的表格
package com.senqi.pdf;
import java.awt.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
public class Test2 {
public static void main(String[] args) throws Exception {
PdfWriter writer = new PdfWriter("d:\\my2.pdf");
PdfDocument doc = new PdfDocument(writer);
Document result = new Document(doc);
PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H");
result.setFont(font);
// 创建表格对象:各列的宽度
// 浮点数组:个数用来表示列数, 每一个值表示该列的宽
Table table = new Table(new float[] { 400, 200, 200, 400, 100 });
//table.setWidthPercent(100);
// 创建单元格对象,跨1行4列
Cell headcell = new Cell(1, 5);
// 设置单元格背景颜色
//headcell.setBackgroundColor(Color.RED);
// 向表格对象添加表头单元格,里面包含内容 Job title,设置居中、加粗
table.addHeaderCell(headcell.add(new Paragraph("我是标题行").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph("one").setFont(font)));
table.addCell(new Cell().add(new Paragraph("tow").setFont(font)));
table.addCell(new Cell().add(new Paragraph("three").setFont(font)));
table.addCell(new Cell().add(new Paragraph("four").setFont(font)));
for (int i = 1; i <= 36; i++) {
table.addCell(new Cell().add(new Paragraph(i + "列").setTextAlignment(TextAlignment.CENTER)));
}
result.add(table);
doc.close();
System.out.println("pdf 创建OK");
}
}
3.创建pdf表格的同时嵌入图片到pdf
package com.senqi.pdf;
import java.util.List;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import com.senqi.entity.Stu;
/**
- 将查询的学生列表, 存到pdf的表格中
- @author senqi
*/
public class Test3 {
public static void main(String[] args) throws Exception {
PdfWriter writer = new PdfWriter("d:\\my4.pdf");
PdfDocument doc = new PdfDocument(writer);
Document result = new Document(doc);
PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H");
result.setFont(font);
Table table = new Table(new float[] { 200, 200, 200, 200, 200, 300});
Cell headcell = new Cell(1, 6);
table.addHeaderCell(headcell.add(new Paragraph("学生列表").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph("学生编号").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph("姓名").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph("身高").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph("手机号").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph("生日").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph("头像").setTextAlignment(TextAlignment.CENTER)));
List<Stu> list = Stu.getAll();
for (Stu stu : list) {
table.addCell(new Cell().add(new Paragraph(stu.getId() + "").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph(stu.getStuName() + "").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph(stu.getHeight() + "").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph(stu.getPhone() + "").setTextAlignment(TextAlignment.CENTER)));
table.addCell(new Cell().add(new Paragraph(stu.getBirthday() + "").setTextAlignment(TextAlignment.CENTER)));
Image image = new Image(ImageDataFactory.create(stu.getIcon()));
image.setWidth(30);
table.addCell(new Cell().add(new Paragraph().add(image)).setTextAlignment(TextAlignment.CENTER));
}
result.add(table);
doc.close();
System.out.println("pdf 创建OK");
}
}
Stu的类
package com.offcn.entity;
import java.util.ArrayList;
import java.util.List;
public class Stu {
private Integer id;
private String stuName;
private Double height;
private String phone;
private String birthday;
private String icon;
public Stu(Integer id, String stuName, Double height, String phone, String birthday, String icon) {
this.id = id;
this.stuName = stuName;
this.height = height;
this.phone = phone;
this.birthday = birthday;
this.icon = icon;
}
public static List<Stu> getAll() {
Stu s1 = new Stu(1, "名字1", 123.4, "15623456789", "2014-12-12", "d:\\tttt.jpg");
Stu s2 = new Stu(2, "名字2", 123.4, "15623456789", "2014-12-12", "d:\\tttt.jpg");
Stu s3 = new Stu(3, "名字3", 123.4, "15623456789", "2014-12-12", "d:\\tttt.jpg");
Stu s4 = new Stu(4, "名字4", 123.4, "15623456789", "2014-12-12", "d:\\tttt.jpg");
Stu s5 = new Stu(5, "名字5", 123.4, "15623456789", "2014-12-12", "d:\\tttt.jpg");
Stu s6 = new Stu(6, "名字6", 123.4, "15623456789", "2014-12-12", "d:\\tttt.jpg");
Stu s7 = new Stu(7, "名字7", 123.4, "15623456789", "2014-12-12", "d:\\tttt.jpg");
List<Stu> stu = new ArrayList<Stu>();
stu.add(s1);
stu.add(s2);
stu.add(s3);
stu.add(s4);
stu.add(s5);
stu.add(s6);
stu.add(s7);
return stu;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}