报表生成工具 —— iText

十二月份一直在做生成报表的功能,中间踩了不少坑,刚好今天有时间,写篇博客记录一下。

一、iText 简介

iText 是著名开放源码站点 sourceforge 的一个项目,是一个用于生成 PDF 文档的 java 类库。通过 iText 不仅可以生成 PDF 或 rtf 的文档,而且可以将 XML、Html 文件转化为 PDF 文件。 —— 百度百科

二、添加 JAR 包

首先在项目中添加 iText 依赖包,公司项目是用 Maven 管理的,可以在网站 mvnrepository 中搜索 iText,查询相关依赖配置,然后在项目 pom.xml 文件中加入相关配置,如下:

<dependency>
  <groupId>com.lowagie</groupId>
  <artifactId>itext</artifactId>
  <version>2.1.7</version>
</dependency>

<dependency>
  <groupId>com.lowagie</groupId>
  <artifactId>itext-rtf</artifactId>
  <version>2.1.7</version>
</dependency>

三、常见问题

3.1 设置中文字体

Font arialFont = new RtfFont("黑 体"); 
Font songFont = new RtfFont("宋 体");
Font imitationSongFont = new RtfFont("仿宋");

注意,有的中文字体字符串中间要用空格隔开,有的不用,须自己测试。

3.2 不同等级标题

iText 默认只有三级标题。

RtfParagraphStyle oneLevelTitleStyle = RtfParagraphStyle.STYLE_HEADING_1;
RtfParagraphStyle twoLevelTitleStyle = RtfParagraphStyle.STYLE_HEADING_2;
RtfParagraphStyle threeLevelTitleStyle = RtfParagraphStyle.STYLE_HEADING_2;

如果要创建四级或五级标题怎么办?查看 iText 源码我们可以发现,iText 一二三级标题定义。

/**
 * The style for level 1 headings.
 */
public static final RtfParagraphStyle STYLE_HEADING_1 = new RtfParagraphStyle("heading 1", "Normal");
/**
 * The style for level 2 headings.
 */
public static final RtfParagraphStyle STYLE_HEADING_2 = new RtfParagraphStyle("heading 2", "Normal");
/**
 * The style for level 3 headings.
 */
public static final RtfParagraphStyle STYLE_HEADING_3 = new RtfParagraphStyle("heading 3", "Normal");

我们可以参照源码,创建一个四级标题。

public static final RtfParagraphStyle STYLE_HEADING_4 = new RtfParagraphStyle("heading 4", "Normal");

但光这样还不行,我们需要注册我们创建的 STYLE_HEADING_4

RtfWriter2 writer = RtfWriter2.getInstance(doc, outputStream);
RtfDocumentSettings settings = writer.getDocumentSettings();
settings.registerParagraphStyle(STYLE_HEADING_4);

3.3 排版问题

虽然通过 iText API 可以找到所有排版的设置方法,但找起来实在是麻烦,所以还是记录一下。

段落
Paragraph paragraph = new Paragraph(content, font);
paragraph.setFirstLineIndent(30f); // 设置首行缩进
paragraph.setLeading(24f); // 设置行间距
paragraph.setSpacingAfter(10f); // 设置段前距离
paragraph.setSpacingBefore(10f); // 设置段后距离
表格
Table table = new Table(columns);
table.setBorderWidth(1f); // 设置表格边线宽度
table.setWidth(100); // 设置表格宽度
table.setPadding(10); // 表格内边距
table.setOffset(1f); // 设置偏移量(该属性用来解决文字与表格中间有段落符号问题)
table.setAlignment(Element.ALIGN_CENTER); // 设置表格对齐方式

Cell cell = new Cell(content, font);
cell.setBackgroundColor(new Color(235, 235, 235)); // 设置单元格背景色
cell.setVerticalAlignment(Cell.ALIGN_CENTER); // 设置垂直对齐方式
cell.setHorizontalAlignment(Cell.ALIGN_CENTER); // 设置水平对齐方式
图片
Image image = Image.getInstance(imageBase64ToByteArray(rejectCompareImageStr));
image.setAlignment(Image.MIDDLE); // 设置对齐方式
image.scalePercent(getPercentByWidth(rejectCompareImage.getWidth())); // 设置缩放比例

因为我用的图片格式是 Base64 编码格式的,需要后台将其转成字节数组的格式。另外,如果图片太大,超出文档,需要设置图片缩放比例。

/**
 * 将 Base64 的图片转成字节数组
 * 
 * @param base64Str
 * @return
 * @throws Exception
 */
private byte[] imageBase64ToByteArray(String base64Str) throws Exception {
    byte[] buffer = {};
    base64Str = base64Str.replaceAll(" ", "+");
    String[] array = base64Str.split("base64,");
    if (array.length > 1) {
        base64Str = array[1];
        BASE64Decoder decoder = new BASE64Decoder();
        buffer = decoder.decodeBuffer(base64Str);
    }
    return buffer;
}

/**
 * 根据图片宽度设置图片缩放比例
 * 
 * @param width
 * @return
 */
private int getPercentByWidth(float width) {
    int p = 0;
    float p2 = 0.0f;
    p2 = 450 / width * 100;
    p = Math.round(p2);
    return p;
}

转载于:https://www.cnblogs.com/wenbingyin/p/8144555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值