itext导出pdf

1、引入jar包

         <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

2、公共方法

 /**
     *
     * @param datas 数据源
     * @param headers   列头
     * @param crosswise rotate设置横向打印
     * @param fileName 文件名
     * @param title 标题
     * @param response
     */
    public static void createPdf(String[][] datas, String[] headers, boolean crosswise, String fileName, String title, HttpServletResponse response) {
        Rectangle rectPageSize;

        if (crosswise) {
            rectPageSize = new Rectangle(PageSize.A4.rotate());// 定义A4页面大小, rotate设置横向打印
        } else {
            rectPageSize = new Rectangle(PageSize.A4);
        }
        Document document = new Document(rectPageSize, 50, 50, 50, 50);// 其余4个参数,设置了页面的4个边距,左右上下
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
            Font fontMoney = new Font(bfChinese, 13, Font.BOLD, BaseColor.RED);
            Font BlodFont = new Font(bfChinese, 12, Font.BOLD, BaseColor.BLACK);
            String downloadName = new String(fileName.getBytes(), "ISO8859-1");

            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadName);
            OutputStream output = response.getOutputStream();
            PdfWriter.getInstance(document, output);
            document.open();
            Paragraph pph1 = new Paragraph(title, new Font(bfChinese, 20, Font.BOLD, BaseColor.BLACK));
            pph1.setAlignment(Element.ALIGN_CENTER);
            pph1.setSpacingAfter(10);
            pph1.setSpacingBefore(5);
            document.add(pph1);


            PdfPTable table = new PdfPTable(headers.length);
            table.setTotalWidth(500);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);

            for (int i = 0; i < headers.length; i++) {
                PdfPCell cell = new PdfPCell(new Phrase(headers[i], fontChinese));

                table.addCell(cell);
            }

            if (datas != null && datas.length > 0) {
                for (int i = 0; i < datas.length; i++) {
                    if (i % 500 == 0) {
                        System.out.println("第" + i + "行,释放内存.............................................");
                        document.add(table);
                        table.deleteBodyRows();
                        table.setSkipFirstHeader(true);
                    }
                    for (int x = 0; x < datas[i].length; x++) {
                        PdfPCell cell = new PdfPCell(new Phrase(datas[i][x], fontChinese));
                        table.addCell(cell);
                    }
                }
            }
            document.add(table);
            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值