人生若只如初见

WXY

Java使用itext7生成PDF文件(二)

2023-10-27

最终生成的PDF预览

2023-10-27-xennrcam.png

itext7的大部分都是和html的方法差不多,如果有前端基础使用起来会很简单。

参考官方Demo

项目整合itext7(7.1.13)

各个版本之间差异也有很多,注意自己依赖的版本

maven依赖

<!--pdf-->
<dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>itext7-core</artifactId>
   <version>7.1.13</version>
   <type>pom</type>
</dependency>

解决中文展示问题

/**
     * 获取中文字体
     *
     * @return
     */
    public static PdfFont getChineseFont() {
        try {
            return PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

绘制PDF方法

/**
     * @param document
     */
    public static void drawBaseInfo(Document document) throws MalformedURLException {
        DeviceRgb black = new DeviceRgb(0, 0, 0);
        DeviceRgb gray = new DeviceRgb(72, 127, 255);
        // 黑色虚线边框
        DashedBorder grayBorder = new DashedBorder(gray, 0.5F);
        SolidBorder solidBorder = new SolidBorder(black, 0.5F);

        Table table = new Table(8);
        table.useAllAvailableWidth();
        table.setTextAlignment(TextAlignment.CENTER);
        table.setFont(getChineseFont());
        table.setFontSize(16);
        table.setPadding(0);

        // 条码
        PdfDocument pdfDocument = document.getPdfDocument();
        PdfPage page = pdfDocument.addNewPage();
        Barcode128 barcode128 = new Barcode128(pdfDocument);
        barcode128.setCode("072201130001");
        barcode128.setCodeType(Barcode128.CODE128);
        barcode128.fitWidth(90);
        PdfFormXObject object =
            barcode128.createFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDocument);

        float width = object.getWidth();
        float height = object.getHeight();
        float x = 80;
        float y = 740;

        PdfCanvas canvas = new PdfCanvas(page);
        canvas.saveState();
        canvas.setFillColor(ColorConstants.WHITE);
        canvas.rectangle(x, y, width, height);
        canvas.fill();
        canvas.restoreState();
        canvas.addXObjectAt(object, x, y);

        // 顶部
        Cell cell1 =
            new Cell(1, 8).add(
                new Paragraph("XXX集团有限公司"));
        cell1.setBold();
        cell1.setFontSize(18);
        cell1.setBorder(Border.NO_BORDER);
        table.addCell(cell1);

        Cell cell2 =
            new Cell(1, 8).add(new Paragraph("xxxxx"));
        cell2.setBorder(Border.NO_BORDER);
        table.addCell(cell2);

        Cell cell3_1 =
            new Cell(1, 1).add(new Paragraph(
                "承运单位: ").add(new Paragraph("xxx公司").setFontColor(ColorConstants.RED)));
        cell3_1.setTextAlignment(TextAlignment.LEFT);
        cell3_1.setBorder(Border.NO_BORDER);
        table.addCell(cell3_1);

        Cell cell3_8 =
            new Cell(1, 7).add(new Paragraph("2023年8月15日"));
        cell3_8.setTextAlignment(TextAlignment.RIGHT);
        cell3_8.setBorder(Border.NO_BORDER);
        table.addCell(cell3_8);

        Cell cell4_1 =
            new Cell(1, 1).add(new Paragraph("始发站"));
        table.addCell(cell4_1);

        Cell cell4_2 =
            new Cell(1, 3).add(new Paragraph("xxxx站"));
        cell4_2.setBorder(solidBorder)
            .setFontColor(ColorConstants.RED);
        table.addCell(cell4_2);

        Cell cell4_3 =
            new Cell(1, 1).add(new Paragraph("运行线路"));
        cell4_3.setBorder(solidBorder);
        table.addCell(cell4_3);

        Cell cell4_4 =
            new Cell(1, 3).add(new Paragraph("xx-xx"));
        cell4_4.setBorder(solidBorder)
            .setFontColor(ColorConstants.RED);
        table.addCell(cell4_4);


        Cell cell5_1 =
            new Cell(1, 1).add(new Paragraph("车次"));
        table.addCell(cell5_1);

        Cell cell5_2 =
            new Cell(1, 3).add(new Paragraph("88888"));
        cell5_2.setBorder(solidBorder)
            .setFontColor(ColorConstants.RED);
        table.addCell(cell5_2);

        Cell cell5_3 =
            new Cell(1, 1).add(new Paragraph("发车时间"));
        cell5_3.setBorder(solidBorder);
        table.addCell(cell5_3);

        Cell cell5_4 =
            new Cell(1, 3).add(new Paragraph("18:00"));
        cell5_4.setBorder(solidBorder)
            .setFontColor(ColorConstants.RED);
        table.addCell(cell5_4);

        Cell cell6_1 =
            new Cell(1, 1).add(new Paragraph("车牌号"));
        table.addCell(cell6_1);

        Cell cell6_2 =
            new Cell(1, 3).add(new Paragraph("豫AA88888"));
        cell6_2.setBorder(solidBorder)
            .setFontColor(ColorConstants.RED);
        table.addCell(cell6_2);

        Cell cell6_3 =
            new Cell(1, 1).add(new Paragraph("电子单据号"));
        cell6_3.setBorder(solidBorder);
        table.addCell(cell6_3);

        Cell cell6_4 =
            new Cell(1, 3).add(new Paragraph("01-0000001"));
        cell6_4.setBorder(solidBorder)
            .setFontColor(ColorConstants.RED);
        table.addCell(cell6_4);

        // 第7行
        List<String> titleList =
            Arrays.asList("到站", "里程", "票价", "全票", "军童票", "优惠票", "人数", "金额");
        for (int i = 0; i < 8; i++) {
            Cell cell =
                new Cell(1, 1).add(new Paragraph(titleList.get(i)));
            cell.setBorder(solidBorder);
            table.addCell(cell);
        }


        // 模拟数据列表
        ArrayList<Map> dataList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("到站", "某某");
            map.put("里程", "33");
            map.put("票价", "13");
            map.put("全票", "1");
            map.put("军童票", "1");
            map.put("优惠票", "1");
            map.put("人数", "3");
            map.put("金额", "34");
            dataList.add(map);
        }

        //   渲染数据
        dataList.forEach(item -> {
            titleList.forEach(title -> {
                String vaule = (String) item.get(title);
                Cell cell =
                    new Cell(1, 1).add(new Paragraph(vaule));
                cell.setBorder(solidBorder).setFontColor(ColorConstants.RED);
                table.addCell(cell);
            });
        });

        Cell cell14_1 =
            new Cell(1, 3).add(new Paragraph("小计"));
        cell14_1.setBorder(solidBorder);
        table.addCell(cell14_1);

        Cell cell14_2 =
            new Cell(1, 1).add(new Paragraph("全票10"));
        cell14_2.setBorder(solidBorder);
        table.addCell(cell14_2);

        Cell cell14_3 =
            new Cell(1, 1).add(new Paragraph("军童10"));
        cell14_3.setBorder(solidBorder);
        table.addCell(cell14_3);

        Cell cell14_4 =
            new Cell(1, 1).add(new Paragraph("优惠10"));
        cell14_4.setBorder(solidBorder);
        table.addCell(cell14_4);


        Cell cell14_5 =
            new Cell(1, 1).add(new Paragraph("人数10"));
        cell14_5.setBorder(solidBorder);
        table.addCell(cell14_5);

        Cell cell14_6 =
            new Cell(1, 1).add(new Paragraph("金额10"));
        cell14_6.setBorder(solidBorder);
        table.addCell(cell14_6);


        Cell cell15_1 =
            new Cell(1, 2).add(new Paragraph("合计金额(大写)"));
        cell15_1.setBorder(solidBorder);
        table.addCell(cell15_1);

        Cell cell15_2 =
            new Cell(1, 6).add(new Paragraph("叁拾肆元"));
        cell15_2.setBorder(solidBorder);
        table.addCell(cell15_2);

        Cell cell16_1 =
            new Cell(1, 2).add(new Paragraph("驾驶员签字"));
        cell16_1.setBorder(solidBorder);
        table.addCell(cell16_1);

        Cell cell16_2 =
            new Cell(1, 6).add(new Paragraph("出站查验电子签字"));
        cell16_2.setBorder(solidBorder).setFontColor(ColorConstants.RED);
        table.addCell(cell16_2);

        Cell cell17_1 =
            new Cell(1, 2).add(new Paragraph("电子结算单状态"));
        cell17_1.setBorder(solidBorder);
        table.addCell(cell17_1);

        Cell cell17_2 =
            new Cell(1, 2).add(new Paragraph("已生效"));
        cell17_2.setBorder(solidBorder).setFontColor(ColorConstants.RED);
        table.addCell(cell17_2);

        Cell cell17_3 =
            new Cell(1, 2).add(new Paragraph("生成时间"));
        cell17_3.setBorder(solidBorder);
        table.addCell(cell17_3);

        Cell cell17_4 =
            new Cell(1, 2).add(new Paragraph("21:07:19"));
        cell17_4.setBorder(solidBorder);
        table.addCell(cell17_4);

        Cell cell18_1 =
            new Cell(1, 1).add(new Paragraph("说明"));
        cell18_1.setBorder(solidBorder);
        table.addCell(cell18_1);

        Cell cell18_2 =
            new Cell(1, 7).add(new Paragraph("1.驾驶员未签字将不会产生电子签章,结算单无效。\n" +
                "2.单据状态显示为“未生效”,结算单无效。"));
        cell18_2.setBorder(solidBorder).setFontSize(14).setTextAlignment(TextAlignment.LEFT);
        table.addCell(cell18_2);


        /********************** 盖章  标题 ******************************/
        Image maru = new Image(ImageDataFactory.create("/home/wxy/Desktop/nywy.png", true));
        maru.setFixedPosition(320, 728);  // 控制图片位置
        maru.scaleAbsolute(120, 80); // 控制图片大小
        document.add(maru);

        document.add(table);

    }

测试类调用生成PDF文件

public static void main(String[] args) throws FileNotFoundException {

        //首先你需要一个writer对象
        PdfWriter writer = new PdfWriter("/home/wxy/Desktop/test.pdf");
        //需要一个pdf的对象来操作pdf
        PdfDocument pdf = new PdfDocument(writer);

        //需要一个document的对象来操作数据 指定为A4大小
        try (Document document = new Document(pdf, PageSize.A4);) {
            drawBaseInfo(document);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

    }

new Cell(1, 2) 两个参数表示这个单元格纵向占1格,横向占2格

相关文章

Java使用itext7生成PDF文件(一)

Java使用itext7生成PDF文件(三)