itextpdf 利用模板生成pdf 下载

阅读: 评论:0

itextpdf 利用模板生成pdf 下载

itextpdf 利用模板生成pdf 下载

1、编辑pdf 模板,编辑表单域,用来填充内容,也可填充图片。利用Adobe Acrobat DC 工具制作模板

2、编写代码

生成后效果图如下

1、制作pdf模板

Adobe Acrobat DC 打开pdf文件,工具-> 准备表单

或者 右键->添加文本域

编辑好后如下图

2、代码部分

2.1、pom 文件引入依赖
<!-- pdf start -->
<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>
<!-- pdf end -->
2.2、编写controller、页面请求下载
ller;import Image;
import Rectangle;
import pdf.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;@RestController
public class PDFController {/*** 模拟填充pdf的数据* key 对应模板中填充字段名*/public static Map<String,Map> mockData(){Map<String,Map> map = new HashMap<>();// 模板中需要填充的数据Map data = new HashMap<String, String>();data.put("name","Anna");data.put("age","23");data.put("phone","13888888888");data.put("email","123456@qq");data.put("address","上海市浦东新区");data.put("work","5年");data.put("projects","Here, fill in part of project experience" +"At least 2-3 years of NGO working experiences in gender program, with the specialty of promoting of women's rights and empowermen" +"at least 3 years working experience in automotive industry, oem project experiences will be a plus");//头像 图片 urlMap image = new HashMap<String, String>();image.put("image","D:\PDF-TEST\tx.jpg");map.put("data",data);map.put("image",image);return map;}@GetMapping("/pdf")public void downLoadPdf(HttpServletResponse response){try{//模板文件String sourceFile = "D:\PDF-TEST\blog.pdf";//下载后pdf名String fileName = "pdf-test";response.setHeader("content-disposition", "attachment;filename=" + fileName + ".pdf");response.setContentType("application/octet-stream");OutputStream out = OutputStream();//输出到本地
//            FileOutputStream out = new FileOutputStream("D:\test.pdf");//读取模板PdfReader  reader = new PdfReader(sourceFile);PdfStamper pdfStamper = new PdfStamper(reader, out);//设置字体格式BaseFont baseFont = ateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);ArrayList<BaseFont> fontList = new ArrayList<>();fontList.add(baseFont);//获取模板文件中的表单域AcroFields afs = AcroFields();afs.setSubstitutionFonts(fontList);//填充文本dataMap data = mockData().get("data");for (Map.Entry<String, AcroFields.Item> entry : Fields().entrySet()) {if (!CollectionUtils.isEmpty(data) && (Key()) != null) {//Field , 这个是自己在pdf上定义的变量名称afs.Key(), (Key()).toString(), true);}}// 处理图片Map imageMap = mockData().get("image");for (Map.Entry<String, AcroFields.Item> entry : Fields().entrySet()) {if (!CollectionUtils.isEmpty(imageMap) && (Key()) != null) {String key = Key();String value = (Key()).toString();int pageNo = FieldPositions(key).get(0).page;Rectangle signRect = FieldPositions(key).get(0).position;float x = Left();float y = Bottom();//根据路径读取图片Image image = Instance(value);//获取图片页面PdfContentByte under = OverContent(pageNo);//图片大小自适应image.Width(), Height());//添加图片image.setAbsolutePosition(x, y);under.addImage(image);}}pdfStamper.setFormFlattening(true);pdfStamper.close();out.close();}catch (Exception e){e.printStackTrace();}}
}

本文发布于:2024-01-28 12:54:46,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17064176907566.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模板   itextpdf   pdf
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23