iText5使用绝对定位制作pdf打印针式发票,支持跳页

1、定义纸张大小,通过对比A4的大小计算相应发票大小的值

A4纸的大小为210mm×297mm,对应的Rectangle为new Rectangle(595,842)

2、加载中文字体,中文字体采用的是simsun.ttc,Windows下有对应的字体文件
        Resource resource = new ClassPathResource("simsun.ttc");
        byte[] ttfAfm = StreamUtil.inputStreamToArray(resource.getInputStream());
        //注意,字体后边带有,0
        BaseFont baseFont = BaseFont.createFont("simsun.ttc,0", BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED, true, ttfAfm, null, false);
3、使用绝对定位输出发票信息

用的类为PdfContentByte,创建方式为PdfContentByte cb = writer.getDirectContent();
开始输出:cb.beginText();
设置字体:cb.setFontAndSize(baseFont, 11);
输出定位信息:cb.showTextAligned(PdfContentByte.ALIGN_LEFT, DateUtil.format(new Date(), “yyyy/MM/dd”), 100, 225, 0);
x、y的坐标对应的原点为左下角
停止输出: cb.endText();
换页:document.newPage();

5、完整代码
 Document document = new Document(new Rectangle(603.54f, 289.01f));
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Resource resource = new ClassPathResource("simsun.ttc");
        byte[] ttfAfm = StreamUtil.inputStreamToArray(resource.getInputStream());
        BaseFont baseFont = BaseFont.createFont("simsun.ttc,0", BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED, true, ttfAfm, null, false);
        List<TbTown> townList = townMapper.selectTbTownList(new TbTown());
        Map<Integer, String> townMap = townList.stream().collect(Collectors.toMap(TbTown::getId,
                TbTown::getMeterReader));
        for (TbWaterRecord waterRecord : list) {
            cb.beginText();
            cb.setFontAndSize(baseFont, 11);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, DateUtil.format(new Date(), "yyyy/MM/dd"), 100, 225, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, waterRecord.getWaterMonth().substring(0, 4), 415, 225, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, waterRecord.getWaterMonth().substring(4, 6), 465, 225, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, waterRecord.getCustomerName(), 105, 202, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getCustomerSeq()), 465, 202, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getLastEndNo()), 105, 186, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getCurrEndNo()), 260, 186, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getRealConsumption()), 400, 186,
                    0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "一阶梯", 100, 142, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getRealConsumption()), 205, 142,
                    0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getPrice()), 320, 142, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getAmount()), 390, 142, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "二阶梯", 100, 129, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "三阶梯", 100, 115, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "水资源税", 100, 102, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, Convert.digitToChinese(waterRecord.getAmount()), 150, 82, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(waterRecord.getAmount()), 415, 82, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, createName, 220, 53, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, townMap.get(waterRecord.getTownId()), 80, 53, 0);
            cb.endText();
            document.newPage();
        }
        document.close();

注意调整打印机的字体,以及定义好打印机发票的纸张大小,否则打印多了跳页会有问题

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值