itextPdf 中的一些细节问题

本文介绍如何在PDF文档中添加中文字体,并演示了不同字体样式的生成方法,包括正常、加粗及不同颜色的显示效果。此外,还介绍了如何创建带有特定样式的表格和单元格。

1 中文字体的添加

    BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",    BaseFont.NOT_EMBEDDED);

注意:一定要确定导入四个jar包(缺一个就可能导致中文字体添不上):itextpdf-5.5.10.jar itext-asian-5.2.0.jar 2.1.7\itext-2.1.7.jar itextasian-1.0.jar
下面是pom工程中导入包的maven依赖

<!--生成pdf -->
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itextasian</artifactId>
            <version>1.0</version>
        </dependency>
2 字体样式的生成

    Font textFont = new Font(bfChinese, 14, Font.NORMAL); // 正常
    Font redTextFont = new Font(bfChinese, 11, Font.NORMAL, Color.RED); // 正常,红色
    Font boldFont = new Font(bfChinese, 11, Font.BOLD); // 加粗
    Font redBoldFont = new Font(bfChinese, 11, Font.BOLD, Color.RED); // 加粗,红色
    Font firsetTitleFont = new Font(bfChinese, 22); // 一级标题
    Font secondTitleFont = new Font(bfChinese, 15, Font.BOLD); // 二级标题

3 表格的生成

    PdfPTable table = new PdfPTable(8);
    table.setTotalWidth(new float[] { 50, 50, 50, 50, 50, 50, 50, 130 }); // 设置列宽
    table.setLockedWidth(true); // 锁定列宽
    PdfPCell cell;  //生成单元格
    cell.disableBorderSide(1); // 隐藏单元格周边的上边框
    cell.disableBorderSide(2);// 隐藏单元格周边的下边框
    cell.disableBorderSide(4); // 隐藏单元格周边的左边框
    cell.disableBorderSide(8);// 隐藏单元格周边的右框
    cell.setBorderWidthLeft(3);//设置边框的宽度
    cell.setBorderWidthTop(3);//设置边框的宽度
    cell = new PdfPCell(new Phrase("报批工程建筑面积明细表", boldFont));//向单元格中插入数据
    cell.setColspan(8);//跨列合并单元格
    cell.setRowspan(2);//跨行合并单元格
    cell.setMinimumHeight(40); // 设置单元格高度
    cell.setUseAscender(true); // 设置可以居中
    cell.setHorizontalAlignment(Cell.ALIGN_CENTER); // 设置水平居中
    cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); // 设置垂直居中
    table.addCell(cell);

4 Phrase Paragraph Chrunk List 的细节方法
详见http://blog.youkuaiyun.com/asdfsadfasdfsa/article/details/75456965

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值