java 使用itext打印pdf 合并单元格

/**
     * 标签打印pdf
     * @param map
     * @param res
     */
    @Override
    public void labelPrinting(Map map,HttpServletResponse res) {
        List<Map> list = (List<Map>) map.get("detailList");
        PdfContentByte waterMar;
        try {
            //文档对象  实现A4纸页面
            Document document = new Document();
            //设置文档的页边距就是距离页面边上的距离,分别为:左边距,右边距,上边距,下边距
            document.setMargins(70, 70, 20, 20);
            //这个是生成破pdf的位置以及名称
            //String fileName = "C:\\Users\\DELL\\Desktop\\PDF\\" + System.currentTimeMillis() + ".pdf";
            //PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(fileName));
            File pdfFile = new File("物料标签.pdf");
            FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
            PdfWriter.getInstance(document,fileOutputStream);
            //打开文档
            document.open();
            //使用字体,正文字体
            Font font = new Font(BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED), 15,Font.BOLD);

            //循环多页
            for(Map m:list) {
                //设置每页大小
                document.setPageSize(new RectangleReadOnly(595.0F, 600.0F));
                //创建新的一页
                document.newPage();
                // -------------------二维码图片 ----------------
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageIO.write(QRCodeUtils.drawQRCodeWithContent(getMapValue(m, "materialsLabel")), "png", out);
                Image image = Image.getInstance(out.toByteArray());


                Paragraph title = new Paragraph("物料标签", new Font(BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 21, Font.BOLD));
                title.setAlignment(Element.ALIGN_CENTER);
                title.setPaddingTop(100f);
                title.setSpacingAfter(20f);
                document.add(title);

                PdfPTable table = new PdfPTable(4);
                table.setWidthPercentage(100); // Width 100%
                float[] columnWidths = {1f, 2f, 1f, 2f};
                table.setPaddingTop(205f);
                table.setWidths(columnWidths);
                table.addCell(getPdfPCell("名称", font));
                table.addCell(getPdfPCell(getMapValue(m, "materialsName"), font, 3, 1));
                document.add(table);

                table = new PdfPTable(4);
                table.setWidths(columnWidths);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("数量", font));
                table.addCell(getPdfPCell(getMapValue(m, "materialsNum"), font));
                table.addCell(getPdfPCell("日期", font));
                table.addCell(getPdfPCell(getMapValue(m, "materialsDate"), font));
                document.add(table);

                table = new PdfPTable(3);
                float[] columnWidths2 = {1f, 2f, 1f};
                table.setWidths(columnWidths2);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("料号", font));
                table.addCell(getPdfPCell(getMapValue(m, "materialsCode"), font, 2, 1));
                table.addCell(getPdfPCell("标签号", font));
                table.addCell(getPdfPCell(getMapValue(m, "materialsLabel"), font, 2, 1));
                table.addCell(getPdfPCell("SAP批次", font));
                table.addCell(getPdfPCell(getMapValue(m, "materialsSapBatch"), font, 2, 1));

                PdfPTable mergeTable = new PdfPTable(2);
                float[] columnWidths3 = {4f, 2f};
                mergeTable.setWidths(columnWidths3);
                mergeTable.setWidthPercentage(100);
                PdfPCell leftCell = new PdfPCell(table);
                mergeTable.addCell(leftCell);

                //合并3行
                PdfPCell rightCell = getPdfPCell("", font);
                rightCell.setBorderColor(BaseColor.LIGHT_GRAY);
                rightCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                rightCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                rightCell.setPadding(1);
                rightCell.setFixedHeight(120);
                rightCell.setImage(image);
                mergeTable.addCell(rightCell);
                document.add(mergeTable);


                table = new PdfPTable(4);
                table.setWidths(columnWidths);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("品保判定", font));
                table.addCell(getPdfPCell(getMapValue(m, "qualityAssurance"), font));
                table.addCell(getPdfPCell("校验", font));
                table.addCell(getPdfPCell(getMapValue(m, "check"), font));
                document.add(table);

                table = new PdfPTable(4);
                table.setWidths(columnWidths);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("有效日期", font));
                table.addCell(getPdfPCell(getMapValue(m, "effectiveDate"), font));
                table.addCell(getPdfPCell("配料员签字", font));
                table.addCell(getPdfPCell(getMapValue(m, "signatureOfBatcher"), font));
                document.add(table);

                table = new PdfPTable(4);
                table.setWidths(columnWidths);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("细度um", font));
                table.addCell(getPdfPCell(getMapValue(m, "finenessUm"), font));
                table.addCell(getPdfPCell("外观", font));
                table.addCell(getPdfPCell(getMapValue(m, "appearance"), font));
                document.add(table);

                table = new PdfPTable(4);
                table.setWidths(columnWidths);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("固含量%标准值", font));
                table.addCell(getPdfPCell(getMapValue(m, "solidsStandard"), font));
                table.addCell(getPdfPCell("检测值", font));
                table.addCell(getPdfPCell(getMapValue(m, "solidsDetection"), font));
                document.add(table);

                table = new PdfPTable(4);
                table.setWidths(columnWidths);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("粘度cps标准值", font));
                table.addCell(getPdfPCell(getMapValue(m, "viscosityStandard"), font));
                table.addCell(getPdfPCell("检测值", font));
                table.addCell(getPdfPCell(getMapValue(m, "viscosityDetection"), font));
                document.add(table);

                table = new PdfPTable(4);
                table.setWidths(columnWidths);
                table.setWidthPercentage(100);
                table.addCell(getPdfPCell("备注", font));
                table.addCell(getPdfPCell(getMapValue(m, "remark"), font, 3, 1));
                document.add(table);
            }

            document.close();
            //pdfWriter.close();

            FileInputStream in = new FileInputStream(pdfFile);
            ServletOutputStream outputStream = res.getOutputStream();
            byte[] buff = new byte[1024];
            int n;
            while ((n = in.read(buff)) != -1) {
                //将字节数组的数据全部写入到输出流中
                outputStream.write(buff, 0, n);
            }
            //强制将缓存区的数据进行输出
            outputStream.flush();
            //关流
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PdfPCell getPdfPCell (String value,Font font) {
        return getPdfPCell(value,font,0,0,null);
    }
    public PdfPCell getPdfPCell (String value,Font font,int colspan,int rowspan) {
        return getPdfPCell(value,font,colspan,rowspan,null);
    }
    public PdfPCell getPdfPCell (String value,Font font,int colspan,int rowspan,Image image) {
        PdfPCell cell = new PdfPCell(new Phrase(value,font));
        cell.setUseBorderPadding(true);
        cell.setBorderColor(BaseColor.LIGHT_GRAY);
        cell.setFixedHeight(40);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        if(isNotEmpty(image)){
            cell.setImage(image);
        }
        return cell;
    }
    public String getMapValue(Map map,String key){
        return isEmpty(map.get(key))?"":map.get(key).toString();
    }

打印后效果:

 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值