本文主要內容是:用java在pdf模板中加入數據,圖片。
廢話不多說,舉個非常簡單的例子:
首先創建word文檔,導出PDF。
用 軟件adobe acrobat打開,操作步驟如圖:
在指定位置添加文本域, 保存退出。pdf模板創建完成,我們保存到 E:盤,起名叫 練習。
接下來是java內容。
在pom.xml文件加入,
<!-- itext 圖片轉pdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency>
在Controller層創建,節約時間直接附上代碼
package com.boot.controller; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.itextpdf.text.Image; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.AcroFields; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; @RestController public class PdfController { /** * 導出pdf * @author Changhai * @param response * @return * @throws UnsupportedEncodingException */ @RequestMapping(value={"/exportpdf"}) public String exportPdf(HttpServletResponse response) throws UnsupportedEncodingException { // 指定解析器 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); //String path = request.getSession().getServletContext().getRealPath("/upload/"); String filename="練習.pdf"; String path="e:/"; response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename, "UTF-8")); OutputStream os = null; PdfStamper ps = null; PdfReader reader = null; try { os = response.getOutputStream(); // 2 讀入pdf表單 reader = new PdfReader(path+ "/"+filename); // 3 根據表單生成一個新的pdf ps = new PdfStamper(reader, os); // 4 獲取pdf表單 AcroFields form = ps.getAcroFields(); // 5給表單添加中文字體 這里采用系統字體。不設置的話,中文可能無法顯示 BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); form.addSubstitutionFont(bf); // 6查詢數據================================================ Map<String, Object> data = new HashMap<String, Object>(); data.put("name", "小帥哥"); data.put("like", "大美女"); // 7遍歷data 給pdf表單表格賦值 for (String key : data.keySet()) { form.setField(key,data.get(key).toString()); } ps.setFormFlattening(true); //-----------------------------pdf 添加圖片---------------------------------- // 通過域名獲取所在頁和坐標,左下角為起點 System.out.println("pdf 添加圖片"); String imgpath="e:/美女.png"; int pageNo = form.getFieldPositions("img").get(0).page; Rectangle signRect = form.getFieldPositions("img").get(0).position; float x = signRect.getLeft(); float y = signRect.getBottom(); // 讀圖片 Image image = Image.getInstance(imgpath); // 獲取操作的頁面 PdfContentByte under = ps.getOverContent(pageNo); // 根據域的大小縮放圖片 image.scaleToFit(signRect.getWidth(), signRect.getHeight()); // 添加圖片 image.setAbsolutePosition(x, y); under.addImage(image); //------------------------------------------------------------- System.out.println("===============PDF導出成功============="); } catch (Exception e) { System.out.println("===============PDF導出失敗============="); e.printStackTrace(); } finally { try { ps.close(); reader.close(); os.close(); } catch (Exception e) { e.printStackTrace(); } } return null; } }
在瀏覽器上訪問
www.localhost:8080/exportpdf
好了 pdf下載成功
效果圖:
感謝您的閱讀