知识点
IText中用文本块(Chunk)、短语(Phrase)和段落(paragraph)处理文本
- 文本块(Chunk)是处理文本的最小单位
- 短语(Phrase)由一个或多个文本块(Chunk)组成,短语(Phrase)也可以设定字体,但对于其中以设定过字体的文本块(Chunk)无效。通过短语(Phrase)成员函数add可以将一个文本块(Chunk)加到短语(Phrase)中,如:phrase6.add(chunk);
- 段落(paragraph)由一个或多个文本块(Chunk)或短语(Phrase)组成,相当于WORD文档中的段落概念,同样可以设定段落的字体大小、颜色等属性。另外也可以设定段落的首行缩进、对齐方式(左对齐、右对齐、居中对齐)。通过函数setAlignment可以设定段落的对齐方式,setAlignment的参数1为居中对齐、2为右对齐、3为左对齐,默认为左对齐。
生成PDF步骤
public void writePdf() {
//1.新建document对象
Document document = new Document(PageSize.A4, 50, 50, 50, 50); //第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
//2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter.getInstance(document, new FileOutputStream("E:\\pdf\\" + new Date().getTime() + ".pdf")); //创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
//3.打开文档
document.open();
//4.向文档中添加内容
document.add(new Chunk("content"));
//5.关闭文档
document.close();
}
代码
package com.qt.exercise;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.DottedLineSeparator;
import com.lowagie.text.pdf.draw.LineSeparator;
import java.awt.*;
import java.io.FileOutputStream;
import java.util.Date;
/**
* @author: XXX
* @date: 2019/7/10 11:32
*/
public class ExportPDF {
private static Font font;//通用
private static Font BOLD_24_FONT;// 设置粗体24字号
private static Font BOLD_12_FONT;
private static Font UNDERLINE_10_FONT;// 设置10号下划线字体
private static Font NORMAL_10_FONT;
private static BaseFont bfChinese; //字体库
static {
try {
//bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//字体库(默认)
bfChinese = BaseFont.createFont("D:\\SWFile\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体库(本地)
BOLD_24_FONT = new Font(bfChinese, 24, Font.BOLD);// 设置粗体24字号
BOLD_12_FONT = new Font(bfChinese, 12, Font.BOLD);
NORMAL_10_FONT = new Font(bfChinese, 10, Font.NORMAL);// 设置默认10字号
UNDERLINE_10_FONT = new Font(bfChinese, 10, Font.UNDERLINE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取字体大小
*
* @param size
* @param family
* @return
*/
public static Font getFont(float size, int family) {
return font = new Font(bfChinese, size, family);// 设置字体大小
}
public void writePdf() throws Exception {
//1.新建document对象
//第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
document.setPageSize(PageSize.A4);// 设置页面大小
//2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
//创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\pdf\\" + new Date().getTime() + ".pdf"));
/* // 设置密码为:"World"
writer.setEncryption("Hello".getBytes(), "World".getBytes(),
PdfWriter.ALLOW_SCREENREADERS,
PdfWriter.STANDARD_ENCRYPTION_128);*/
//添加页眉
HeaderFooter header = new HeaderFooter(new Phrase(""), false);
//设置是否有边框等
header.setBorder(Rectangle.BOTTOM);
header.setAlignment(0);
header.setBorderColor(Color.BLACK);
document.setHeader(header);
//添加页脚
HeaderFooter footer = new HeaderFooter(new Phrase(""), false);
//设置是否有边框等
footer.setBorder(Rectangle.BOTTOM);
footer.setAlignment(0);
footer.setBorderColor(Color.BLACK);
document.setFooter(footer);
document.setPageCount(1);
//3.打开文档
document.open();
//4.向文档中添加内容
//写入图片
Image image = Image.getInstance("D:\\PICC.jpg");
image.setAlignment(Image.ALIGN_LEFT);//设置图片居中 0靠左 1,居中 2,靠右
image.scaleAbsolute(200, 18);//图片大小 长 高
document.add(image);
document.add(new Chunk(" 01000010",NORMAL_10_FONT));
//添加标题
Paragraph title = new Paragraph("标题", BOLD_24_FONT);//设置字体样式
title.setAlignment(Element.ALIGN_CENTER);//设置文字居中 0靠左 1,居中 2,靠右
document.add(title);
//添加下划线
LineSeparator UNDERLINE = new LineSeparator(1, 100, Color.red, Element.ALIGN_CENTER, -2);
Paragraph Paragraph = new Paragraph("段落追加方式下划线",NORMAL_10_FONT);
Paragraph.add(UNDERLINE);
document.add(Paragraph);
document.add(new Paragraph("段落字体方式下划线",UNDERLINE_10_FONT));
document.add(new Chunk("文本块字体方式下划线\n",UNDERLINE_10_FONT));
document.add(new Chunk("文本块带颜色属性方式下划线\n",NORMAL_10_FONT).setUnderline(Color.RED,0.5f,0,-2f,0f,0));
document.add(new Chunk("文本块属性方式下划线\n",NORMAL_10_FONT).setUnderline(0.5f,-2f));
document.add(new Phrase("短语字体方式下划线\n",UNDERLINE_10_FONT));
document.add(new Chunk("文本块直线",NORMAL_10_FONT));
document.add(new Chunk(new LineSeparator()));
Paragraph lineSeparator = new Paragraph("段落点线",NORMAL_10_FONT);
lineSeparator.add(new Chunk(new DottedLineSeparator()));
document.add(lineSeparator);
document.add(new Chunk("\n"));
//添加表格1
PdfPTable table1 = new PdfPTable(2); //定义一个两列表格
PdfPCell cell1; //定义列
cell1 = new PdfPCell(new Phrase("隐藏框行1列1:", NORMAL_10_FONT)); //列添加内容
cell1.setBorder(Rectangle.NO_BORDER); //无边框
table1.addCell(cell1); //列添加到表
cell1 = new PdfPCell(new Phrase("隐藏框行1列2:", NORMAL_10_FONT));
cell1.setBorder(Rectangle.NO_BORDER);
table1.addCell(cell1);
cell1 = new PdfPCell(new Phrase("隐藏框行2列1:", NORMAL_10_FONT));
cell1.setBorder(Rectangle.NO_BORDER);
table1.addCell(cell1);
cell1 = new PdfPCell(new Phrase("隐藏框行2列2:", NORMAL_10_FONT));
cell1.setBorder(Rectangle.NO_BORDER);
table1.addCell(cell1);
table1.setWidthPercentage(100);
document.add(table1);
//插入表格2
PdfPTable table = new PdfPTable(3); //定义一个3列表格
PdfPCell cell;
cell = new PdfPCell(new Phrase("行1列1:", NORMAL_10_FONT));
cell.setColspan(2); //合并两列
cell.setMinimumHeight(20f); //最小列高20
table.addCell(cell);
table.addCell(new PdfPCell(new Phrase("行1列2:", NORMAL_10_FONT)));
cell = new PdfPCell(new Phrase("行2列1:", NORMAL_10_FONT));
cell.setColspan(2);
cell.setMinimumHeight(20f);
table.addCell(cell);
table.addCell(new PdfPCell(new Phrase("行2列2:", NORMAL_10_FONT)));
//遍历插入数据
for (int i = 0; i < 5; i++) {
cell = new PdfPCell(new Phrase("行3列1", NORMAL_10_FONT));
table.addCell(cell);
cell = new PdfPCell(new Phrase("行3列2值1: \n行3列2值3: ", NORMAL_10_FONT));
cell.setBorderWidthRight(0); //设置右边框为0
cell.setBorderWidthBottom(0); //设置下边框为0
table.addCell(cell);
cell = new PdfPCell(new Phrase("行3列2值2: \n行3列2值4:", NORMAL_10_FONT));
cell.setBorderWidthLeft(0);
cell.setBorderWidthBottom(0);
table.addCell(cell);
}
table.addCell(new PdfPCell(new Phrase("行4列1", NORMAL_10_FONT)));
Paragraph paragraph = new Paragraph(); //定义段落
paragraph.add(new Chunk(" ", UNDERLINE_10_FONT)); //段落添加内容且带下划线
paragraph.add(new Chunk("月,自", NORMAL_10_FONT));
paragraph.add(new Chunk(" ", UNDERLINE_10_FONT));
paragraph.add(new Chunk("年", NORMAL_10_FONT));
paragraph.add(new Chunk(" ", UNDERLINE_10_FONT));
paragraph.add(new Chunk("月", NORMAL_10_FONT));
paragraph.add(new Chunk(" ", NORMAL_10_FONT));
paragraph.add(new Chunk("日零时起,至", NORMAL_10_FONT));
paragraph.add(new Chunk(" ", UNDERLINE_10_FONT));
paragraph.add(new Chunk("年", NORMAL_10_FONT));
paragraph.add(new Chunk(" ", UNDERLINE_10_FONT));
paragraph.add(new Chunk("月", NORMAL_10_FONT));
paragraph.add(new Chunk(" ", UNDERLINE_10_FONT));
paragraph.add(new Chunk("日二十四时止。", NORMAL_10_FONT));
cell = new PdfPCell(paragraph);
cell.setColspan(2);
cell.setMinimumHeight(20f);
table.addCell(cell);
Paragraph paragraph2 = new Paragraph();
paragraph2.add(new Chunk(" 投保人声明:保险人已向本人提供并详细介绍了《展览会责任保险条款》,并对其中免除保险人责任的条款(包括但不限于责任免除、投保人被保险人义务、赔偿处理、其他事项等),以及本保险合同中付费约定和特别约定的内容向本人做了明确说明,本人已充分理解并接受上述内容,同意以此作为订立保险合同的依据,自愿投保本保险。\n" +
" 上述所填写的内容均属实。\n", BOLD_12_FONT));
paragraph2.add(new Chunk(" 投保人签名 / 签章: ", NORMAL_10_FONT));
paragraph2.add(new Chunk(" ", NORMAL_10_FONT).setUnderline(0.5f, -2f));
paragraph2.add(new Chunk("年", NORMAL_10_FONT));
paragraph2.add(new Chunk(" ", NORMAL_10_FONT).setUnderline(0.5f, -2f));
paragraph2.add(new Chunk("月 \n", NORMAL_10_FONT));
paragraph2.add(new Chunk("\n", NORMAL_10_FONT));
cell = new PdfPCell(paragraph2);
cell.setColspan(3);
table.addCell(cell);
//100%
table.setWidthPercentage(100); //设置表宽100%
document.add(table);//表格添加到文档;
document.add(new Paragraph("\n\n"));
宽度50% 居左
// table.setWidthPercentage(50);
// table.setHorizontalAlignment(Element.ALIGN_LEFT);
// document.add(table);
// document.add(new Paragraph("\n\n"));
//
宽度50% 居中
// table.setWidthPercentage(50);
// table.setHorizontalAlignment(Element.ALIGN_CENTER);
// document.add(table);
// document.add(new Paragraph("\n\n"));
//
宽度50% 居右
// table.setWidthPercentage(50);
// table.setHorizontalAlignment(Element.ALIGN_RIGHT);
// document.add(table);
// document.add(new Paragraph("\n\n"));
//
固定宽度
// table.setTotalWidth(500);
// table.setLockedWidth(true);
// document.add(table);
//5.关闭文档
document.close();
}
public static void main(String[] args) throws Exception {
System.out.println("begin");
ExportPDF pdf = new ExportPDF();
pdf.writePdf();
System.out.println("end");
}
}
需要的maven依赖
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
字体库
simkai.ttf字体下载