美文网首页
使用Itext生成pdf

使用Itext生成pdf

作者: 云师兄 | 来源:发表于2018-07-02 12:41 被阅读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!至于方法中涉及的几个类的意思,这里直接引用官方描述:

    • PdfWriter is an object that can write a PDF file. It doesn't know much about the actual content of the PDF document it is writing. The PdfWriter doesn't know what the document is about, it just writes different file parts and different objects that make up a valid document once the file structure is completed.
    • The PdfWriter knows what to write because it listens to a PdfDocument. The PdfDocument manages the content that is added, distributes that content over different pages, and keeps track of whatever information is relevant for that content.
    • Once we've created a PdfWriter and a PdfDocument, we're done with all the low-level, PDF-specific code. We create a Document that takes the PdfDocument as parameter. Now that we have the document object, we can forget that we're creating PDF.
    • We create a Paragraph containing the text "Hello World" and we add that paragraph to the document object.
    • We close the document. Our PDF has been created.

    添加字体和列表

    @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, ""));
        }
    

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

    相关文章

      网友评论

          本文标题:使用Itext生成pdf

          本文链接:https://www.haomeiwen.com/subject/xrakuftx.html