使用Itext生成pdf

2018-07-02  本文已影响35人  云师兄

参考文档

安装

首先参照官方文档,添加需要用到的依赖:

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>io</artifactId>
            <version>7.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>forms</artifactId>
            <version>7.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>pdfa</artifactId>
            <version>7.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>pdftest</artifactId>
            <version>7.0.4</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.18</version>
        </dependency>

实现hello world

@GetMapping("/itextpdf")
    public ResponseEntity<Response> testItextPdf() throws Exception{
        PdfWriter pdfWriter = new PdfWriter("/Users/yubuyun/Desktop/test.pdf");

        PdfDocument pdf = new PdfDocument(pdfWriter);

        Document document = new Document(pdf);

        document.add(new Paragraph("Hello world!"));

        document.close();

        return ResponseEntity.ok().body(new Response(true, stringBuilder.toString()));
    }

要注意的是上述代码中的Document类是itext中的类,另外Paragraph类不知道为啥,idea自动添加import不了,手动添加了一下:

import com.itextpdf.layout.element.Paragraph;

当我们发起请求调这个方法后,就会在/Users/yubuyun/Desktop/test.pdf路径下生成pdf文件,内容就是hello world!至于方法中涉及的几个类的意思,这里直接引用官方描述:

添加字体和列表

@GetMapping("/itextpdf")
    public ResponseEntity<Response> testItextPdf() throws Exception{

        PdfWriter writer = new PdfWriter("/Users/yubuyun/Desktop/test.pdf");
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);

        PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);

        document.add(new Paragraph("iText is:").setFont(font));

        com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List()
                .setSymbolIndent(12)
                .setListSymbol("\u2022")
                .setFont(font);

        list.add(new ListItem("Never gonna give you up"))
                .add(new ListItem("Never gonna let you down"))
                .add(new ListItem("Never gonna run around and desert you"))
                .add(new ListItem("Never gonna make you cry"))
                .add(new ListItem("Never gonna say goodbye"))
                .add(new ListItem("Never gonna tell a lie and hurt you"));

        document.add(list);
        document.close();

        return ResponseEntity.ok().body(new Response(true, ""));
    }

效果如下:


屏幕快照 2018-07-02 下午1.56.34.png

添加图片

@GetMapping("/itextpdf")
    public ResponseEntity<Response> testItextPdf() throws Exception{

        PdfWriter writer = new PdfWriter("/Users/yubuyun/Desktop/test.pdf");
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);

        PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);

        // Compose Paragraph
        Image fox = new Image(ImageDataFactory.create("/Users/yubuyun/Desktop/test.JPG"));
        Paragraph p = new Paragraph("The quick brown ")
                .add(fox)
                .add(" jumps over the lazy ");

        document.add(p);
        document.close();

        return ResponseEntity.ok().body(new Response(true, ""));
    }

上述实现了图片和文字混合添加的效果。

上一篇 下一篇

猜你喜欢

热点阅读