PDF文件增加内容填充效果

1.导入pom

        <!--pdf-->
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.12</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-pdfa</artifactId>
			<version>5.5.12</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf.tool</groupId>
			<artifactId>xmlworker</artifactId>
			<version>5.5.12</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-xtra</artifactId>
			<version>5.5.12</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.commons</groupId>
					<artifactId>commons-imaging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

2.编写pdf填充实体类

public class PdfModelDTO {

    //x坐标
    private float xCoordinate;
    //y坐标
    private float yCoordinate;
    //填充页码
    private Integer pageNum;
    //填充内容
    private String content;

    public PdfModelDTO(float xCoordinate, float yCoordinate, Integer pageNum, String content) {
        this.xCoordinate = xCoordinate;
        this.yCoordinate = yCoordinate;
        this.pageNum = pageNum;
        this.content = content;
    }

    public float getxCoordinate() {
        return xCoordinate;
    }

    public void setxCoordinate(float xCoordinate) {
        this.xCoordinate = xCoordinate;
    }

    public float getyCoordinate() {
        return yCoordinate;
    }

    public void setyCoordinate(float yCoordinate) {
        this.yCoordinate = yCoordinate;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "PdfModelDTO{" +
                "xCoordinate=" + xCoordinate +
                ", yCoordinate=" + yCoordinate +
                ", pageNum=" + pageNum +
                ", content='" + content + '\'' +
                '}';
    }
}

3.编写pdf工具类

/** pdf内容填充
     *
     * @param oldFilePath 源文件地址
     * @param newFilePath 新文件地址
     * @param list 需要填充的信息
     * @return 0:成功  1:源文件不是pdf文件  -9:异常

     */
    public static int PdfTextFill(String oldFilePath, String newFilePath, List<PdfModelDTO> list){

        try {
            //判断源文件是否为pdf文件
            String suffix = oldFilePath.substring(oldFilePath.lastIndexOf('.') + 1);
            if (!"pdf".equalsIgnoreCase(suffix)) {
                logger.info(oldFilePath+"文件不是pdf文件,无法进行内容填充");
                return 1;
            }

            //文件解析
            PdfReader reader = new PdfReader(oldFilePath);
            FileOutputStream fOut = new FileOutputStream(newFilePath);
            PdfStamper stp = new PdfStamper(reader, fOut);

            // 获取文件总页数
            int numberOfPages = reader.getNumberOfPages();

            //内容填充
            for (PdfModelDTO model : list) {
                float xCoordinate = model.getxCoordinate();
                float yCoordinate = model.getyCoordinate();
                Integer pageNum = model.getPageNum();
                String content = model.getContent();

                //跳转到填充页并图层置顶
                PdfContentByte pdfContentByte = stp.getOverContent(pageNum);
                pdfContentByte.beginText();
                //设置字体及字号
                pdfContentByte.setFontAndSize(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false), 11);
                //设置字体颜色
                pdfContentByte.setColorFill(BaseColor.BLACK);
                //设置填充的对齐方式、内容、位置和旋转
                pdfContentByte.showTextAligned(Element.ALIGN_LEFT, content, xCoordinate, yCoordinate, 0);
                pdfContentByte.endText();
            }

            //关闭资源连接
            stp.close();
            fOut.close();
            reader.close();

            logger.info("源文件" + oldFilePath + "内容填充完毕,新文件地址:" + newFilePath);
            return 0;
        } catch (Exception e){
            logger.info("源文件" +oldFilePath+ "内容填充异常:" + e.getMessage());
            return -9;
        }
    }

4.编写测试代码

public static void main(String[] args) {
        //填充内容
        List<PdfModelDTO> list = new ArrayList<>();
        list.add(new PdfModelDTO(143f,675f,1,"1233211234567"));
        list.add(new PdfModelDTO(341f,675f,1,"西山根"));
        list.add(new PdfModelDTO(143f,653f,1,"某寡妇"));
        list.add(new PdfModelDTO(310f,653f,1,"女"));
        list.add(new PdfModelDTO(391f,491f,1,"2022年12月12日"));

        //文件路径
        String oleFilePath = "C:/Users/sky/Desktop/haha.pdf";
        String newFilePath = "C:/Users/sky/Desktop/haha_"+System.currentTimeMillis()+".pdf";

        //填充结果(0:成功  1:源文件不是pdf文件  -9:异常)
        int result = PdfTextFill(oleFilePath, newFilePath,list);
    }

注*:填充内容坐标位置需自行调试

5.运行测试

模板:

 

效果:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值