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

115 阅读1分钟
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();

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