java通過(guò)PDF模板填寫(xiě)PDF表單
本文實(shí)例為大家分享了java通過(guò)PDF模板填寫(xiě)PDF表單的具體代碼,包括圖片,供大家參考,具體內(nèi)容如下
需要用到的java包:
itext.jar、iTextAsian.jar的JAR包。這個(gè)包里面定義了與中文輸出相關(guān)的一些文件。
編寫(xiě)的表單如下:
import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.itextpdf.text.DocumentException; 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; /** * pdf工具類(lèi) * @author MOSHUNWEI * @since 2018-02-01 */ public class PDFUtil { /** * 根據(jù)模板生成pdf * @param data Map(String,Object) * @return */ public static boolean createPDF(String path,Map<String, Object> data) { PdfReader reader = null; AcroFields s = null; PdfStamper ps = null; ByteArrayOutputStream bos = null; try { reader = new PdfReader("D:\\test.pdf"); bos = new ByteArrayOutputStream(); ps = new PdfStamper(reader, bos); s = ps.getAcroFields(); /** * 使用中文字體 使用 AcroFields填充值的不需要在程序中設(shè)置字體,在模板文件中設(shè)置字體為中文字體 Adobe 宋體 std L */ BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); /** * 設(shè)置編碼格式 */ s.addSubstitutionFont(bfChinese); // 遍歷data 給pdf表單表格賦值 for (String key : data.keySet()) { s.setField(key,data.get(key).toString()); } // 如果為false那么生成的PDF文件還能編輯,一定要設(shè)為true ps.setFormFlattening(true); /** * 添加圖片 */ String imgpath="D:/n5.jpg"; int pageNo = s.getFieldPositions("img").get(0).page; Rectangle signRect = s.getFieldPositions("img").get(0).position; float x = signRect.getLeft(); float y = signRect.getBottom(); // 讀圖片 Image image = Image.getInstance(imgpath); // 獲取操作的頁(yè)面 PdfContentByte under = ps.getOverContent(pageNo); // 根據(jù)域的大小縮放圖片 image.scaleToFit(signRect.getWidth(), signRect.getHeight()); // 添加圖片 image.setAbsolutePosition(x, y); under.addImage(image); @SuppressWarnings("resource") FileOutputStream fos = new FileOutputStream("d:\\shouju_fb.pdf"); fos.write(bos.toByteArray()); return true; } catch (IOException | DocumentException e) { System.out.println("讀取文件異常"); e.printStackTrace(); return false; }finally { try { bos.close(); ps.close(); reader.close(); } catch (IOException | DocumentException e) { System.out.println("關(guān)閉流異常"); e.printStackTrace(); } } } public static void main(String[] args) { Map<String, Object> data = new HashMap<String, Object>(); data.put("id", "12312321"); data.put("name", "小帥哥"); data.put("sex", "男"); data.put("age", "21"); PDFUtil.createPDF("D:/n5.jpg",data); } }
還有相應(yīng)的編輯pdf表單的工具,默認(rèn)用Adobe Acrobat。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?@EventListener?異步中使用condition的問(wèn)題及處理
這篇文章主要介紹了Spring?@EventListener?異步中使用condition的問(wèn)題及處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12使用Shiro實(shí)現(xiàn)登錄成功后跳轉(zhuǎn)到之前的頁(yè)面
這篇文章主要介紹了如何使用Shiro實(shí)現(xiàn)不同用戶(hù)登錄成功后跳轉(zhuǎn)到不同主頁(yè),實(shí)現(xiàn)此功能目前比較好的方法是用ajax的方法登錄,第二種方法是把用戶(hù)未登錄前的url存在session中,需要的朋友可以參考下2015-07-07Scala可變參數(shù)列表,命名參數(shù)和參數(shù)缺省詳解
這篇文章主要介紹了Scala可變參數(shù)列表,命名參數(shù)和參數(shù)缺省詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06MySQL查詢(xún)字段實(shí)現(xiàn)字符串分割split功能的示例代碼
本文主要介紹了MySQL查詢(xún)字段實(shí)現(xiàn)字符串分割split功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Java貪心算法之Prime算法原理與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java貪心算法之Prime算法原理與實(shí)現(xiàn)方法,簡(jiǎn)單描述了Prime算法的概念、原理、實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2017-09-09Spring+SpringMVC+JDBC實(shí)現(xiàn)登錄的示例(附源碼)
這篇文章主要介紹了Spring+SpringMVC+JDBC實(shí)現(xiàn)登錄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08