当前位置: 代码网 > it编程>编程语言>Java > SpringBoot集成iTextPDF的实例

SpringBoot集成iTextPDF的实例

2024年09月20日 Java 我要评论
springboot集成itextpdf依赖 <dependency> <groupid>com.lowagie</groupid&g

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();
    }

效果

  • 加上图片之后:

有兴趣的读者可自行美化

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com