公司OA项目即将上线,需要一系列请假、加班和财务等PDF报表的支持!
点我!实战实例
1.引入maven依赖
<!-- /artifact/com.itextpdf/itext-asian(字体) --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><!-- /artifact/com.itextpdf/itextpdf(核心) --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency><!-- /artifact/org.bouncycastle/bcprov-jdk15on(加密) --><dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.60</version></dependency><!-- /artifact/cn.hutool/hutool-core(三方工具类) --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-core</artifactId><version>${hutool}</version></dependency>
2.iText生成报表
2.1 生成基本报表
// 创建documentDocument document = new Document(); // 生成pdfWriter实例 PdfWriter.getInstance(document, new FileOutputStream("myPDF.pdf")); // 打开文档 document.open(); // 添加文档内容 document.add(new Paragraph("Hello World")); // 关闭文档document.close();
2.2 文档加密
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));// pdf文件加密pdfWriter.setEncryption("123456".getBytes(), "888888".getBytes(), PdfWriter.ALLOW_SCREENREADERS, PdfWriter.ENCRYPTION_AES_256);document.open();
加密后的文档不能进行其他的操作,例如加水印等。setEncryption方法参数: 用户密码,所有者密码,权限,加密类型
2.3 页面设置
// 设置页面大小(默认页边距为36磅),其中 retabe()指定横版Rectangle rectangle = new Rectangle(PageSize.A4.rotate()); // 设置页面背景颜色 rectangle .setBackgroundColor(BaseColor.ORANGE); // 生成document Document document = new Document(rectangle ); // 生成pdfWriter实例PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( "PDF.pdf")); document .open(); document .add(new Paragraph("Hello World")); // 关闭文档document.close();
① rotate();横向打印
② setRotation(int rotation);旋转角度
③ setBackgroundColor(BaseColor color);背景颜色
④ setGrayFill(float value);设置背景颜色灰度值
⑤ setBorder(int value);设置指定边边框
⑥ setBorderWidth(float width);设置边框宽度
⑦ setBorderColor(BaseColor color);边框颜色
2.4 新建一页
document.open(); document.add(new Paragraph("java 程序性能优化")); // 新建空白页 document.newPage(); writer.setPageEmpty(false); document.newPage(); document.add(new Paragraph("代码整洁之道"));
setPageEmpty方法:是否跳过空白页;true:忽略空白页;false:输出所有(含空白)
3.基本内容
3.1 字体设置
1)BaseFont - 字体类型的基类,可以支持中文
2)Font - 字体的设置,如颜色,字体,大小等
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);参数:1:字体类型名称或字体文件位置 2:编码格式 3:字体是否要嵌入到PDFFont textFont = new Font(baseFont, 8, Font.NORMAL);参数: 1:基础字体 2:字体大小 3:字体类型 4:字体颜色
3.2格式设置
1)Element接口:内容对象基本都实现这个接口。如Chunk、 Phrase
ALIGN_LEFT, ALIGN_CENTER、 ALIGN_RIGHT, ALIGN_JUSTIFIED 。
如:设置居中对齐:setAlignment(Element.ALIGN_CENTER)
3.3 Chunk Phrase Paragraph元素
1). Chunk 元素:这是可以添加到文档中的文本最小单位,大多数元素可以分成一个或多个Chunk
① setAnchor(String url);设置超链接
② setUnderline(…);设置下划线
③ setFont(Font font);设置字体
④ setLineHeight(float height);设置行高
⑤ setCharacterSpacing(float space);设置字符间距
⑥ setHorizontalScaling(float value);设置水平缩放
⑦ setBackground(BaseColor color);设置背景颜色
2). Phrase 元素:短语是一系列的块。短语有一个主字体,但是短语中的一些块可以有不同于主字体的字体。短语中的所有块有相同的行间距。(中文不显示解决方案:①使用new Phrase(String context,Font font);②使用add(Element element)方法
① add(String str);添加字符串(非中文)
② add(Element element);添加元素
③ setFont(Font font);设置字体
④ setLeading(float value);设置行间距
3). Paragraph 元素:段落是一系列的块和/或短语。段落具有短语的性质,但也有一些额外的布局参数:缩进、文本的对齐方式(中文不显示解决方案:①使用new Paragraph (String context,Font font);②使用add(Element element)方法)
① add(Element);添加内容
② setFont(Font font);设置字体
③ setLeading(float value);设置行间距(一个Paragraph只有一个行间距)
④ setIndentationLeft(float value);左缩进
⑤ setIndentationRight(float value);右缩进
⑥ setFirstLineIndent(float value);首行缩进
⑦ setSpacingBefore(float value);设置段落上空白
⑧ setSpacingAfter(float value);设置段落下空白
⑨ setAlignment(int alignment);设置居中对齐方式(可以使用Element)
3.4 Anchor(超链接) 元素
1).Anchor 元素:可以是引用的目标。
① setName(String name);设置锚的名称
② setReference(String reference) ;设置锚的引用
示例:
Anchor anchor = new Anchor("this is a link"); // 中文不显示,构造时需要指定Font字体anchor.setName("LINK");anchor.setReference("");
3.5 Image 元素
初始化:Image img = Image.getInstance(“watermark.jpg”);
① scalePercent(float percent);设置将图片比例压缩
② scaleToFit(float fitWidth, float fitHeight);缩放图像使其适合指定宽度和高度
③ setAlignment(int value);设置图片对齐方式
④ setRotationDegrees(float degrees);设置图像的旋转角度
⑤ setAbsolutePosition(float x,float y);设置图片的绝对位置
⑥ scaleAbsolute(float width, float height);将图像缩放到绝对宽度和绝对高度
3.6 PdfGState 元素
① setFillOpacity(float value);设置透明度
3.7 PdfPTable 元素
初始化表格:PdfPTable table = new PdfPTable(int numColumns);
① setTotalWidth(float totalWidth);设置表格总宽度
② setWidths(float widths[]);设置单元格宽度 (数组长度等于列数)
③ setLockedWidth(boolean lockedWidth);锁定表格宽度
④ setHorizontalAlignment(int value);设置对齐方式
⑤ setWidthPercentage(float widthPercent);设置表格宽度百分比
⑥ setBackgroundColor(BaseColor color);设置背景颜色
⑦ setSpacingBefore(float value);设置表格上面空白
⑧ setSpacingAfter(float value);设置表格下面空白
⑨ addCell(PdfPCell cell);添加单元格
3.8 PdfCell 元素
① setRowspan(int rowspan);设置跨行
② setColspan(int colspan);设置跨列
③ setVerticalAlignment(int align);设置垂直对齐方式
④ setHorizontalAlignment(int align);设置水平对齐方式
⑤ setBorder(int border);设置边框(0表示无边框)
⑥ setBorderWidth(float with);设置边框宽度(0表示无边框)
⑦ setBorderColor(BaseColor color);设置边框颜色
⑧ setBackgroundColor(BaseColor color);设置背景颜色
⑨ setMinimumHeight(float height);设置最小高度
⑩ setFixedHeight(float height);设置固定高度
4. PDF结构 (4层结构)
四层结构:一、四层可操作;二、三层Itext内部处理。层对象: PdfContentByte
1).PdfContentByte:包含用户定位的文本和页面的图形内容的对象。
① beginText();开始
② setGState(PdfGState gstate);设置图像状态(如:透明度)
③ addImage(Image image);添加图片
④ setFontAndSize(BaseFont font,float size);设置文本字体和大小
⑤ setTextMatrix(float x,float y);设置文本的矩阵
⑥ showTextAligned(int alignment, String text, float x, float y, float rotation);写入文档(对齐方式,文本,x坐标,y坐标,旋转角度)
⑦ endText();结束
2).PdfReader :读取PDF文档
PdfReader reader = new PdfReader(String filename);读取和解析PDF文档。
3).PdfStamper:将额外内容应用于PDF文档的页面。
PdfStamper stamper = new PdfStamper(PdfReader reader, OutputStream os); 开始向现有PDF文档添加额外内容的过程。
4).PdfWriter:写入PDF文档