Java使用itext非模板方式生成PDF表格文件2

由于基于模板生成pdf文件的方式,无法灵活支持同类别不固定行数的信息展示,所以只能改为不依靠模板的方式。现将我开发实现过程,做一简单分享。算是一个总结,便于自己日后查阅,也希望能够帮助到其他有需要的同学。

需要参考的,看一下我下面截图的结果样式,再看一下对应的测试代码就可以使用了。

1. 生成结果样式
pdf表格文件示例
2. 生成PDF文件实现方式

具体实现:

2.1 引入jar包
<dependency>
     <groupId>com.itextpdf</groupId>
     <artifactId>itextpdf</artifactId>
     <version>5.5.13</version>
</dependency>
<dependency>
     <groupId>com.itextpdf</groupId>
     <artifactId>itext-asian</artifactId>
     <version>5.2.0</version>
</dependency>
2.2 具体代码如下:

不啰嗦直接上代码,具体含义如果不清楚的话,可以看注释,我已经把注释尽可能的写详细了。

package pdfgentest;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class PdfTableGenTest {
    private static Font headFont;// 设置字体大小
    private static Font sonHeadFont;// 设置字体大小
    private static Font normalTextFont;// 设置字体大小
    private static Font tableLineHeadFont;// 设置字体大小
    private static Font tableColumHeadFont;// 设置字体大小
    private static Font textFont;// 设置字体大小
    private static Font minTextFont;// 设置字体大小

    static {
        BaseFont bfChinese;
        try {
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            headFont = new Font(bfChinese, 16, Font.BOLD);// 设置字体大小
            sonHeadFont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
            normalTextFont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小
            tableLineHeadFont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
            tableColumHeadFont = new Font(bfChinese, 6, Font.BOLD);// 设置字体大小
            textFont = new Font(bfChinese, 6, Font.NORMAL);// 设置字体大小
            minTextFont = new Font(bfChinese, 5, Font.NORMAL);// 设置字体大小
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeExampaperPdf() throws Exception {
        // 1.新建document对象
        // 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
        Document document = new Document(PageSize.A4, 50, 50, 20, 40);
        // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
        // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://myPdfFile.pdf"));

        //3.2打开文档
        document.open();
        //创建一个包含多列的表格,以通过合并单元格的方式,控制表格的宽度大小
        PdfPTable table = createTable(12);
        table.addCell(createCell("\n", headFont, Element.ALIGN_CENTER, 4, false));
        table.addCell(createCell("客户基本信息表", headFont, Element.ALIGN_CENTER, 4, false));

        sonHeadFont.setColor(new BaseColor(205, 133, 63)); //设置字体颜色
        table.addCell(createCell("文件编号:FILEBH-001", sonHeadFont, Element.ALIGN_RIGHT, 4, false));

        PdfPCell jbxxCell = createCell("基本信息", tableLineHeadFont, Element.ALIGN_CENTER, 12, true);
        jbxxCell.setBackgroundColor(new BaseColor(205, 133, 63));
        table.addCell(jbxxCell);

        //创建单元格,指定字体、对齐方式、合并单元格的个数、是否有边框
        table.addCell(createCell("姓名", tableColumHeadFont, Element.ALIGN_CENTER, 2, true));
        table.addCell(createCell(null, textFont, Element.ALIGN_CENTER, 4, true));
        table.addCell(createCell("职业", tableColumHeadFont, Element.ALIGN_CENTER, 2, true));
        table.addCell(createCell("工程师", textFont, Element.ALIGN_CENTER, 4, true));

        table.addCell(createCell("出生日期", tableColumHeadFont, Element.ALIGN_CENTER, 2, true));
        table.addCell(createCell("2020-01-01", textFont, Element.ALIGN_CENTER, 4, true));
        table.addCell(createCell("国籍", tableColumHeadFont, Element.ALIGN_CENTER, 2, true));
        table.addCell(createCell("中国", textFont, Element.ALIGN_CENTER, 4, true));

        table.addCell(createCell("性别", tableColumHeadFont, Element.ALIGN_CENTER, 2, true));
        table.addCell(createCell("男", textFont, Element.ALIGN_CENTER, 4, true));
        table.addCell(createCell("联系电话", tableColumHeadFont, Element.ALIGN_CENTER, 2, true));
        table.addCell(createCell("18888888888", textFont, Element.ALIGN_CENTER, 4, true));

        table.addCell(createCell("个人税收居民身份", tableColumHeadFont, Element.ALIGN_CENTER, 2, true));

        Phrase phrase = new Phrase();
        PdfPCell wtrgrssjmxxCell1 = new PdfPCell();
        wtrgrssjmxxCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        wtrgrssjmxxCell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        phrase.add(new Chunk("□ 1.中国税收居民      □ 2.非中国税收居民      ■ 3.既是中国税收居民又是其他国家(地区)税收居民\n", minTextFont).setLineHeight(12f));
        phrase.add(new Chunk("★ 如以上选项中填选第2项或第3项,请填写下列信息:\n", textFont).setLineHeight(12f));
        phrase.add(new Chunk("   税收居民国(地区):", textFont).setLineHeight(12f));
        phrase.add(new Chunk(" HK00001", textFont).setLineHeight(12f).setUnderline(0.5f, -1f));
        phrase.add(new Chunk("     纳税人识别号(如有):", textFont).setLineHeight(12f));
        phrase.add(new Chunk("289000001", textFont).setLineHeight(12f).setUnderline(0.5f, -1f)); //设置下划线,并设置下划线的粗细
        wtrgrssjmxxCell1.setPhrase(phrase);
        wtrgrssjmxxCell1.setPaddingLeft(20f);
        wtrgrssjmxxCell1.setColspan(10);
        wtrgrssjmxxCell1.setPaddingTop(3.0f);
        wtrgrssjmxxCell1.setPaddingBottom(6.0f);
        table.addCell(wtrgrssjmxxCell1);

        PdfPCell cclyCell = createCell("保险清单列表", tableLineHeadFont, Element.ALIGN_CENTER, 12, true);
        cclyCell.setBackgroundColor(new BaseColor(205, 133, 63)); //设置单元格背景
        table.addCell(cclyCell);

        for (int i = 0; i < 2; i++) {
            table.addCell(createCell("保险清单" + (i + 1), tableColumHeadFont, Element.ALIGN_CENTER, 2, true));

            Phrase insurancePhrase1 = new Phrase();
            PdfPCell insuranceCell1 = new PdfPCell();
            insuranceCell1.setVerticalAlignment(Element.ALIGN_MIDDLE); //上下居中
            insuranceCell1.setHorizontalAlignment(Element.ALIGN_LEFT); //水平左对齐
            insurancePhrase1.add(new Chunk("1.保险产品名称:灵通万事家天下寿险" + i + "\n", textFont).setLineHeight(10f)); //setLineHeight 设置行高
            insurancePhrase1.add(new Chunk("3.保险单号:L000000002392997" + i + "\n", textFont).setLineHeight(10f));
            insurancePhrase1.add(new Chunk("5.保险险种:终身寿险" + "\n", textFont).setLineHeight(10f));
            insurancePhrase1.add(new Chunk("7.基本保额:250000" + i + "\n", textFont).setLineHeight(10f));
            insurancePhrase1.add(new Chunk("9.备注(其他情况说明):无", textFont).setLineHeight(10f));
            insuranceCell1.setPhrase(insurancePhrase1);
            insuranceCell1.setPaddingLeft(10f);//设置左侧空白填充的宽度
            insuranceCell1.setColspan(5); //合并单元格
            insuranceCell1.setPaddingTop(1.0f); //距离上边框距离
            insuranceCell1.setPaddingBottom(4.0f); //距离下边框距离
            insuranceCell1.disableBorderSide(8);//隐藏右边框 1-上, 2-下, 4-左, 8-右
            table.addCell(insuranceCell1);

            Phrase insurancePhrase2 = new Phrase();
            PdfPCell insuranceCell2 = new PdfPCell();
            insuranceCell2.setVerticalAlignment(Element.ALIGN_MIDDLE); //上下居中
            insuranceCell2.setHorizontalAlignment(Element.ALIGN_LEFT); //水平左对齐
            insurancePhrase2.add(new Chunk("2.保险公司:灵通万事" + i + "\n", textFont).setLineHeight(10f));
            insurancePhrase2.add(new Chunk("4.保单生效日:2020/12/0" + i + "\n", textFont).setLineHeight(10f));
            insurancePhrase2.add(new Chunk("6.应交总保费:1290500" + i + "\n", textFont).setLineHeight(10f));
            insurancePhrase2.add(new Chunk("8.受益人变更情况:生存受益人变更为保险公司;身故受益人变更为保险公司\n", textFont).setLineHeight(10f));
            insurancePhrase2.add(new Chunk("              \n", textFont).setLineHeight(10f));
            insuranceCell2.setPhrase(insurancePhrase2);
            insuranceCell2.setColspan(5);
            insuranceCell2.setPaddingTop(1.0f);
            insuranceCell2.setPaddingBottom(4.0f);
            insuranceCell2.disableBorderSide(4);//隐藏左边框: 1-上, 2-下, 4-左, 8-右
            table.addCell(insuranceCell2);
        }

        PdfPCell syrmxCell = createCell("受益人情况", tableLineHeadFont, Element.ALIGN_CENTER, 12, true);
        syrmxCell.setBackgroundColor(new BaseColor(205, 133, 63));
        table.addCell(syrmxCell);

        for (int i = 0; i < 1; i++) {
            table.addCell(createCell("受益人" + (i + 1), tableColumHeadFont, Element.ALIGN_CENTER, 1, true));

            Phrase syrPhrase1 = new Phrase();
            PdfPCell syrCell1 = new PdfPCell();
            syrCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            syrCell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            syrPhrase1.add(new Chunk("姓名:王小" + i + "\n", textFont).setLineHeight(10f));
            syrPhrase1.add(new Chunk("与委托人关系:儿子\n", textFont).setLineHeight(10f));
            syrPhrase1.add(new Chunk("婚姻状况:未婚\n", textFont).setLineHeight(10f));
            syrPhrase1.add(new Chunk("联系电话:1887878000" + i + "\n", textFont).setLineHeight(10f));
            syrPhrase1.add(new Chunk("电子邮箱:8888888@168.com\n", textFont).setLineHeight(10f));
            syrPhrase1.add(new Chunk("邮寄地址:北京市朝阳区光明路128号5栋102\n", textFont).setLineHeight(10f));
            syrPhrase1.add(new Chunk("终止分配比例:100%", textFont).setLineHeight(10f));

            syrCell1.setPhrase(syrPhrase1);
            syrCell1.setPaddingLeft(10f);
            syrCell1.setColspan(5);
            syrCell1.setPaddingTop(1.0f);
            syrCell1.setPaddingBottom(4.0f);
            table.addCell(syrCell1);

            table.addCell(createCell("个人税收居民身份", tableColumHeadFont, Element.ALIGN_CENTER, 1, true));

            Phrase syrgrssjmxxPhrase = new Phrase();
            PdfPCell syrgrssjmxxCell1 = new PdfPCell();
            syrgrssjmxxCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            syrgrssjmxxCell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            syrgrssjmxxPhrase.add(new Chunk("■ 1.中国税收居民  □ 2.非中国税收居民 □ 3.既是中国又是其他国家(地区)税收居民\n", minTextFont).setLineHeight(15f));
            syrgrssjmxxPhrase.add(new Chunk("★ 如以上选项中填选第2项或第3项,请填写下列信息:\n", textFont).setLineHeight(15f));
            syrgrssjmxxPhrase.add(new Chunk("   税收居民国(地区):▁▁▁▁     纳税人识别号(如有):▁▁▁▁", textFont).setLineHeight(15f));
            syrgrssjmxxCell1.setPhrase(syrgrssjmxxPhrase);
            syrgrssjmxxCell1.setPaddingLeft(6f);
            syrgrssjmxxCell1.setColspan(5);
            syrgrssjmxxCell1.setPaddingTop(3.0f);
            syrgrssjmxxCell1.setPaddingBottom(6.0f);
            table.addCell(syrgrssjmxxCell1);
        }

        document.add(table);

        document.add(new Paragraph("\n"));

        String paragraph5 = "客户(签字):▁▁▁▁▁▁▁▁";
        Paragraph elements5 = new Paragraph(paragraph5, normalTextFont);
        elements5.setAlignment(Element.ALIGN_RIGHT);
        document.add(elements5);

        // 5.关闭文档
        document.close();
    }

    public static PdfPTable createTable(int colNumber) {
        int maxWidth = 500;
        PdfPTable table = new PdfPTable(colNumber);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setWidths(new int[]{50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50});
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }


    public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3.0f);
        if (!boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        }
        return cell;
    }

    public static void main(String[] args) {
        try {
            writeExampaperPdf();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3. 写在最后的小心得

网上得来的代码,尤其是涉及流资源读取与关闭的,一定要仔细重点的经过反复测试验证之后,再借鉴使用。一定不要拿来主义,直接照搬。因为很多时候看似是捷径的方法,其实质很可能是拖你后腿的大坑。

4. 参考文章

https://www.bbsmax.com/A/gVdnK17XzW/
https://www.cnblogs.com/qlqwjy/p/8213989.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,607评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,239评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,960评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,750评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,764评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,604评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,347评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,253评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,702评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,893评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,015评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,734评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,352评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,934评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,052评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,216评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,969评论 2 355

推荐阅读更多精彩内容