SpringBoot集成 iTextPDF示例

依赖:

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

注意:不加下面这个依赖无法设置中文

使用步骤:

  1. 先创建Document对象,该对象是PDF文档,可以进行一些属性设置
  2. PdfPTable是表格对象,用于表格对象的创建、管理及使用
  3. PdfPCell是具体的表格子项了,里面就是我们要操作写入的对象
  4. PdfPCell对象加入到PdfPTable对象中,PdfPTable对象再加入到Document对象中,便完成了文档的创建

基本属性:

文档大小:
由Document对象的多个重载构造器决定。
Document(); // 默认页面大小是A4
Document(PageSize.A4); // 指定页面大小为A4
Document(PageSize.A4,50,50,30,20); // 指定页面大小为A4,且自定义页边距(marginLeft、marginRight、marginTop、marginBottom)

段落的设置:
由Paragraph的对象属性决定。
Paragraph paragraph = new Paragraph(name,headfont);//设置字体样式
paragraph.setAlignment(1);//设置文字居中 0靠左 1,居中 2,靠右
paragraph.setIndentationLeft(12);// 左缩进
paragraph.setIndentationRight(12);// 右缩进
paragraph.setFirstLineIndent(24);// 首行缩进
paragraph.setLeading(20f); //行间距
paragraph.setSpacingBefore(5f); //设置段落上空白
paragraph.setSpacingAfter(10f); //设置段落下空白

表格:
由Table的对象属性决定。
table.setAlignment(Element.ALIGN_CENTER);//居中
table.setAutoFillEmptyCells(true);//自动填满
table.setBorderWidth((float)0.1);//表格边框线条宽度
table.setPadding(1);//边距:单元格的边线与单元格内容的边距
table.setSpacing(0);//间距:单元格与单元格之间的距离

cell:
由Cell的对象属性决定。
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中

代码:

    @RequestMapping(value = "ef/pdf")
    public void pdfController() throws IOException, DocumentException {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setHeader("content-Type", "application/pdf");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=test.pdf");

        Document document = new Document();
        try {
            PdfWriter.getInstance(document, response.getOutputStream());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        document.open();
        document.setPageCount(2);
        document.addTitle("Personal Grade Browser");// 标题
        document.addAuthor("Jack.Edward");// 作者
        document.addSubject("Personal Grade of Jack.Edward");// 主题
        document.addKeywords("Simulator");// 关键字
        document.addCreator("Kicinio");// 创建者

        Image image =Image.getInstance("/xp.png");

        List<String> titleList = new ArrayList<>();
        titleList.add("Literature");
        titleList.add("Math");
        titleList.add("English");

        for(int i = 0; i < 10; i++){
            PdfPTable tableContent = new PdfPTable(titleList.size());
            if(i == 0){
                PdfPTable tableTitle = new PdfPTable(titleList.size());
                PdfPCell cellOne = new PdfPCell();
                cellOne.setPhrase(new Paragraph(titleList.get(0)));
                cellOne.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cellOne.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTitle.addCell(cellOne);

                PdfPCell cellTwo = new PdfPCell();
                cellTwo.setPhrase(new Paragraph(titleList.get(1)));
                cellTwo.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTitle.addCell(cellTwo);

                PdfPCell cellThree = new PdfPCell();
                cellThree.setPhrase(new Paragraph(titleList.get(2)));
                cellThree.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTitle.addCell(cellThree);

                document.add(tableTitle);
            }
            Random randomGrade = new Random();
            PdfPCell cell = new PdfPCell();
            cell = new PdfPCell();
            cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
            tableContent.addCell(cell);
            document.add(tableContent);

            cell = new PdfPCell();
            cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
            tableContent.addCell(cell);
            cell.setBorderWidth(20);
            document.add(tableContent);

            cell = new PdfPCell();
            cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
    //        cell.setImage(image);
            tableContent.addCell(cell);
            document.add(tableContent);

        }
        document.close();
    }

效果:

在这里插入图片描述
加上图片之后:
在这里插入图片描述

有兴趣的读者可自行美化

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值