1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > IText_根据模板导出PDF(文字 表格 图片)

IText_根据模板导出PDF(文字 表格 图片)

时间:2018-08-28 19:03:32

相关推荐

IText_根据模板导出PDF(文字 表格 图片)

1、引入jar包

<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.4.3</version></dependency>

2、新建word文档,创建模板,将文件另存为pdf,并用Adobe Acrobat DC打开

3、在需要插入数据的空白处,右击,点击【文本域】,将文本域拖放到你想要的位置,更改域名称为你传入的变量名。

4、模板(编辑好后的模板)

5、保存模板,将模板放到项目中

pdf生成,代码如下

public static void creatPdf(Map<String, Object> map, String filePath) {try {BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);FileOutputStream out = new FileOutputStream(filePath);// 输出流// 读取pdf模板// PdfReader reader = new PdfReader(TemplateToWord.class.getResource("/com/cn/business/templates/report.pdf"));PdfReader reader = new PdfReader(String.valueOf(map.get("tempPath")));ByteArrayOutputStream bos = new ByteArrayOutputStream();PdfStamper stamper = new PdfStamper(reader, bos);stamper.setFormFlattening(true);AcroFields form = stamper.getAcroFields();// 文字类的内容处理Map<String, String> datemap = (Map<String, String>) map.get("dataMap");form.addSubstitutionFont(bf);for (String key : datemap.keySet()) {String value = datemap.get(key);form.setField(key, value);}// 图片类的内容处理Map<String, String> imgmap = (Map<String, String>) map.get("imgMap");for (String key : imgmap.keySet()) {String value = imgmap.get(key);String imgpath = value;int pageNo = form.getFieldPositions(key).get(0).page;Rectangle signRect = form.getFieldPositions(key).get(0).position;float x = signRect.getLeft();float y = signRect.getBottom();// 根据路径读取图片Image image = Image.getInstance(imgpath);// 获取图片页面PdfContentByte under = stamper.getOverContent(pageNo);// 图片大小自适应image.scaleToFit(signRect.getWidth(), signRect.getHeight());// 添加图片image.setAbsolutePosition(x, y);under.addImage(image);}// 表格类Map<String, List<List<String>>> listMap = (Map<String, List<List<String>>>) map.get("tableList");for (String key : listMap.keySet()) {List<List<String>> lists = listMap.get(key);int pageNo = form.getFieldPositions(key).get(0).page;PdfContentByte pcb = stamper.getOverContent(pageNo);Rectangle signRect = form.getFieldPositions(key).get(0).position;//表格位置int column = lists.get(0).size();int row = lists.size();PdfPTable table = new PdfPTable(column);float tatalWidth = signRect.getRight() - signRect.getLeft() - 1;int size = lists.get(0).size();float width[] = new float[size];for (int i = 0; i < size; i++) {if (i == 0) {width[i] = 60f;} else {width[i] = (tatalWidth - 60) / (size - 1);}}table.setTotalWidth(width);table.setLockedWidth(true);table.setKeepTogether(true);table.setSplitLate(false);table.setSplitRows(true);Font FontProve = new Font(bf, 10, 0);//表格数据填写for (int i = 0; i < row; i++) {List<String> list = lists.get(i);for (int j = 0; j < column; j++) {Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);PdfPCell cell = new PdfPCell(paragraph);cell.setBorderWidth(1);cell.setVerticalAlignment(Element.ALIGN_CENTER);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setLeading(0, (float) 1.4);table.addCell(cell);}}table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), pcb);}stamper.setFormFlattening(true); // 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑stamper.close();Document doc = new Document();PdfCopy copy = new PdfCopy(doc, out);doc.open();int pageNum = reader.getNumberOfPages();for (int i = 1; i <= pageNum; i++) {PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);copy.addPage(importPage);}doc.close();} catch (IOException e) {System.out.println(e);} catch (DocumentException e) {System.out.println(e);}}

6、测试方法

public static void main(String[] args) {// 模板路径String tempPath = "D:\\admin\\test.pdf";//文字类Map<String, String> dataMap = new HashMap<String, String>();DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");String title = "测试文字内容" + "(" + format.format(new Date()) + ")";dataMap.put("title", title);// dataMap中的key要和模板中的域名对应//图片String imgPath = "C:\\Users\\Admin\\Pictures\\豚鼠图片.jpg";Map<String, String> imgMap = new HashMap<String, String>();imgMap.put("picture", imgPath);// imgMap中的key要和模板中的域名对应//表格 一行数据是一个listList<String> list = new ArrayList<String>();list.add("日期");list.add("金额");List<String> list2 = new ArrayList<String>();list2.add("-08-27");list2.add("100000");List<List<String>> List = new ArrayList<List<String>>();List.add(list);List.add(list2);Map<String, List<List<String>>> listMap = new HashMap<String, List<List<String>>>();listMap.put("table", List);// 这里的listMap中key要和模板中的域名对应Map<String, Object> o = new HashMap<String, Object>();o.put("tempPath", tempPath);o.put("dataMap", dataMap);o.put("imgMap", imgMap);o.put("tableList", listMap);String outPutPath = "D:\\admin\\test1.pdf";creatPdf(o, outPutPath);}

7、效果图

8、结语

itext的功能不单单只有这些,当然,这些也是比较常用的,另外还有合并PDF文件,以及生成带水印或背景的PDF,如果有需要,我也可以贴出来

原创不易,如果对你有帮助,一键三连吧

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。