pdf在线转换

1.准备工作

1.使用itextpdf直接修改PDF

2.准备一份模板pdf

3.使用Adobe Acrobat DC打开

4.选择工具->准备表单

5.在PDF中添加文本域

 2.引入依赖

<!--引入itextpdf依赖-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>
<!--此依赖中包含中文字体-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

3.测试代码

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

//@SpringBootTest
class PdfDemoApplicationTests {

    @Test
    void contextLoads() throws Exception {

        // pdf文件
        String fileName = "src/main/resources/stu.pdf";

        // 读取pdf文件
        PdfReader pdfReader = new PdfReader(fileName);

        // 输出内存流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        // pdf签章
        PdfStamper pdfStamper = new PdfStamper(pdfReader, bos);

        // 获取表单域
        AcroFields fields = pdfStamper.getAcroFields();

        // 设置中文字体
        ArrayList<BaseFont> fonts = new ArrayList<>();
        fonts.add(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
        fields.setSubstitutionFonts(fonts);

        // 添加文本
        fillData(fields);

        // 添加图片
        List<AcroFields.FieldPosition> list = fields.getFieldPositions("stamp");
        Rectangle rectangle = list.get(0).position;
        Image image = Image.getInstance("src/main/resources/logo.png");
        image.scaleToFit(rectangle.getWidth(), rectangle.getHeight());
        image.setBorder(2);
        image.setAbsolutePosition(
                list.get(0).position.getLeft() + (rectangle.getWidth() - image.getScaledWidth() )
                , list.get(0).position.getTop() - (rectangle.getHeight()));
        PdfContentByte cb = pdfStamper.getOverContent((int)list.get(0).page);
        cb.addImage(image);

        // 关闭pdf签章
        pdfStamper.close();

        // 生成结果pdf
        FileOutputStream fileOutputStream = new FileOutputStream("res.pdf");
        fileOutputStream.write(bos.toByteArray());
        fileOutputStream.close();
        bos.close();

    }

    private void fillData(AcroFields fields) throws DocumentException, IOException {
        Map<String,String> data = new HashMap<>();
        data.put("name", "张三");
        data.put("begin", "01");
        data.put("end", "05");
        data.put("reason", "班长要结婚了");
        data.put("day", "4");

        Set<Map.Entry<String, String>> entries = data.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            fields.setFieldProperty(entry.getKey(), "textsize", 9.3f, null);
            fields.setField(entry.getKey(), entry.getValue());

        }
    }

}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值