ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot输出PDF文件

2022-07-01 15:34:46  阅读:179  来源: 互联网

标签:输出 springboot BaseFont over new itextpdf pdf PDF document


工作中经常有需求要求输出成.doc文件或者.pdf文件,输出word文档比较简单,但是输出成pdf就比较麻烦,后来在网上看了看大家都怎么生成pdf的。发现iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。在这里记录一下,以备后期查看。

引入maven包,如下使用了最新的包。

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author zd
 * @version 1.0
 * @description: TODO
 * @date 2022/7/1 10:12
 */
public class TestPdf {

    public static void strToPdfFile(String content,String filePath) {

        Document document = new Document(PageSize.A4);

        try {
            PdfWriter.getInstance(document, new FileOutputStream(filePath));
            //document.addTitle("example of PDF");
            document.open();
            //4.向文档中添加内容
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            Font font = new Font(bf, 12, Font.NORMAL);
            document.add(new Paragraph(content,font));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            document.close();
        }

    }

    public static void addWaterPrint(String sourcePdfPath,String destPdfPath) throws IOException, DocumentException {
        PdfReader reader = null;
        PdfStamper stamp=null;
            reader = new PdfReader("D:\\test\\testpdf.pdf");
            stamp = new PdfStamper(reader, new FileOutputStream("D:\\test\\testpdf2.pdf"));
            //文字水印
            PdfContentByte over = stamp.getOverContent(2);
            over.beginText();
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
                    BaseFont.EMBEDDED);
            over.setFontAndSize(bf, 18);
            over.setTextMatrix(30, 30);
            over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
            over.endText();
            stamp.close();
            reader.close();


    }

    public static void main(String[]args){
        // 不添加水印的
        StringBuffer sb=new StringBuffer("天天开心");
        TestPdf.strToPdfFile(sb.toString(),"D:\\test\\testpdf.pdf");
        // 给pdf添加水印
        try {
            TestPdf.addWaterPrint("D:\\test\\testpdf.pdf","D:\\test\\testpdf2.pdf");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }
}

添加完maven依赖之后编写上面的代码,亲试可以使用。

标签:输出,springboot,BaseFont,over,new,itextpdf,pdf,PDF,document
来源: https://www.cnblogs.com/zhangdanyang95/p/16434809.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有