ItextPdf生成PDF二维码文件(支持多个打印)

生成二维码PDF文件


public class JitQrCodeUtil {
    private static final String FONT_URL = ServletHelper.BootPath() + "/resources/pdf/MSYH.TTF";
    /**
     * 二维码打印
     * @param entityList 打印的记录
     * @param contentMap 内容有序集合<key,Chinese>
     * @param pdfPath PDF保存路径
     */
    public static void createQrCodePdf(AList entityList, LinkedHashMap<String,String> contentMap,String pdfPath){

        BaseFont baseFont;
        try {
            //使用iTextAsian.jar中的字体
            //bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            //使用Windows系统字体(TrueType)
            baseFont = BaseFont.createFont(FONT_URL, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            Font font = new Font(baseFont, 8, Font.NORMAL);//设置字体

            int height = PdfUnitConversion.mm2points(60 - 3);
            int width = PdfUnitConversion.mm2points(80 - 4);

            //Rectangle(urx,ury)
            Rectangle pageSize = new Rectangle(width, height);

            //(pageSize,marginLeft,marginRight,marginTop,marginBottom)
            Document document = new Document(pageSize, 10, 10, 10, 0);

            // step 2
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            // step 3
            document.open();
            // step 4
            PdfContentByte cb = writer.getDirectContent();

            for (AData entity : entityList.getList()) {
                document.newPage();
                String keyName = entity.getString("key_name");

                PdfPTable table = getPdfPTable(2, new int[]{65, 35});//列
                table.setWidthPercentage(100);

                PdfPTable msgTable = new PdfPTable(1);
                Phrase phrase = new Phrase();
                int contentLen = 0;//设置最多有几条记录,多了就不显示
                for (Map.Entry<String, String> contentEntry : contentMap.entrySet()) {
                    String aValue = entity.getString(contentEntry.getKey());
                    if (aValue != null){
                        String value = contentEntry.getValue();
                        phrase.add(new Chunk(value + ":"  + aValue,font));
                        phrase.add(Chunk.NEWLINE);
                        if (++contentLen >= 9){
                            break;
                        }
                    }
                }
                PdfPCell msgPhrase = new PdfPCell();
                msgPhrase.addElement(phrase);
                msgPhrase.setBorder(0);
                msgPhrase.setHorizontalAlignment(Element.ALIGN_CENTER);
                msgPhrase.setVerticalAlignment(Element.ALIGN_CENTER);
                msgTable.addCell(msgPhrase);
                table.addCell(msgTable);

                PdfPTable qrCodeTable = new PdfPTable(1);
                Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
                hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 矫错级别
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

                //BarcodeQRCode(String content, int width, int height, Map<EncodeHintType, Object> hints)
                BarcodeQRCode qrcode = new BarcodeQRCode(keyName, 1, 1, hints);

                Image img = qrcode.getImage();
                qrCodeTable.addCell(img);
                Font keyNameFont = new Font(baseFont, 10, Font.NORMAL);//设置字体
                Phrase keyNameParse = new Phrase(keyName,keyNameFont);
                PdfPCell codeCell = new PdfPCell(keyNameParse);//
                codeCell.setBorder(0);
                codeCell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置单元格的水平对齐方式

                qrCodeTable.addCell(codeCell);
                table.addCell(qrCodeTable);
                document.add(table);
            }
            // step 5
            document.close();
        } catch (DocumentException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static PdfPTable getPdfPTable(int column, int[] tableWidth) throws DocumentException {
        PdfPTable table = new PdfPTable(column);
        table.setWidths(tableWidth);
        table.getDefaultCell().setBorder(0);
        table.setWidthPercentage(100);
        return table;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值