java生成pdf

1导入jar包

        <!--设备生成pdf使用-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.12</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>

这里需要注意版本,不然使用字体时会报错

打印PDF报错:STSong-Light' with 'UniGB-UCS2-H' is not recognized

2 代码

    public static void main(String[] args) {
        String tmpPath = "C:\\Users\\Desktop\\PDFDemo.pdf";
        PdfPCell cell;
        PdfPCell iCell;
        PdfPTable iTable;
        float lineHeight1 = (float) 40.0;
        float lineHeight2 = (float) 40.0;
        float lineHeight3 = (float) 40.0;
        float lineHeight4 = (float) 40.0;
        float lineHeight5 = (float) 40.0;

        try {
            // 不同字体(这里定义为同一种字体:包含不同字号、不同style)
            //注意 此处不定义字体,生成出来的pdf内容汉字部分会不显示
            BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            keyfont = new Font(bfChinese, 16, Font.BOLD);
            textfont = new Font(bfChinese, 10, Font.NORMAL);
            Document pdfDoc = new Document(PageSize.A4.rotate());

            PdfWriter.getInstance(pdfDoc, new FileOutputStream(tmpPath));

            pdfDoc.open();
			//自上往下1列
            PdfPTable headerTable = new PdfPTable(1);
            //设置表格具体宽度
            headerTable.setTotalWidth(160);
            //设置每一列宽度40
            headerTable.setWidthPercentage(40);

            //在第一列下分两列
            iTable = new PdfPTable(2);
            //设置每一列所占的长度
            iTable.setWidths(new float[]{40f, 120f});

            iCell = new PdfPCell(new Paragraph("检测公司", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight1);
            //merge column
            iCell.setColspan(2);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph("设备名称", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight2);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph("", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight2);
            iTable.addCell(iCell);

            cell = new PdfPCell(iTable);
            cell.setPadding(0);
            headerTable.addCell(cell);

            //在上面一行下再新建一行分4列
            iTable = new PdfPTable(4);
            //设置每一列所占的长度
            iTable.setWidths(new float[]{40f, 40f, 40f, 40f});

            iCell = new PdfPCell(new Paragraph("设备编号", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight3);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph(""));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight3);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph("规格型号", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight3);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph(""));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight3);

            iCell.setColspan(4);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph("出厂编号", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight4);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph(""));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight4);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph("生产厂家", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight4);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph(""));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight4);

            iCell.setColspan(4);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph("购置日期", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight5);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph(""));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight5);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph("管理人员", keyfont));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight5);
            iTable.addCell(iCell);
            iCell = new PdfPCell(new Paragraph(""));
            iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            iCell.setFixedHeight(lineHeight5);
            iTable.addCell(iCell);

            cell = new PdfPCell(iTable);
            cell.setPadding(0);
            headerTable.addCell(cell);

            pdfDoc.add(headerTable);

            pdfDoc.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3 生成内容展示

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值