利用iText導出pdf文件代碼
根據給定的pdf模板,導出pdf文件,并根據給定內容替換pdf模板中的輸入域。至于如何制作pdf模板,請參考Adobe公司的軟件Acrobat,該軟件對pdf模板的制作提供了很好的支持。 p.s.需要導入jiar包:itextpdf-5.4.1.jar。
一、導出pdf工具類:
package pdf; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.Map; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.AcroFields; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; /**
- 導出pdf工具類<br>
- 根據給定的pdf模板,導出pdf文件,并根據給定內容替換pdf中的輸入域
@author wangtl / public class ExportPdfUtil { /**
- 導出pdf工具類 <br>
- 導出pdf工具類
- @param outpath 輸出路徑
- @param fontPath 字體路徑
- @param templateName pdf模板文件路徑名
- @param content 需要填充內容
@return */ public static String exportpdf(String outpath, String templateName, String fontPath, Map<String, String> content) {
// 得到當前時間 Date now = new Date(); SimpleDateFormat dataformat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); String t = dataformat.format(now); // 得到一個隨機數 String ran = Math.random() + ""; // 以當前時間加上一個隨機數獲取下載的文件以保證不重名 String filename = t + "-" + ran;
String savepath = outpath + File.separator + filename + ".pdf"; PdfReader reader = null; ByteArrayOutputStream bos = null; PdfStamper ps = null; FileOutputStream fos = null; try {
// 創建字體 BaseFont chineseSong = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); // 讀取pdf reader = new PdfReader(templateName); bos = new ByteArrayOutputStream(); ps = new PdfStamper(reader, bos); AcroFields s = ps.getAcroFields(); // 添加所創建的字體 s.addSubstitutionFont(chineseSong); //找到pdf中輸入域并替換為內容 Iterator<String> it = s.getFields().keySet().iterator(); while(it.hasNext()){ String name = (String) it.next(); s.setField("" + name.trim(), content.get(name.trim())); } //這兩步必須有,否則pdf生成失敗 ps.setFormFlattening(true); ps.close(); //輸出pdf fos = new FileOutputStream(savepath); fos.write(bos.toByteArray());
} catch (FileNotFoundException e) { System.out.println("FileNotFoundException"); e.printStackTrace(); } catch (Exception e) { System.out.println("Exception"); e.printStackTrace(); } finally {
if (null != reader) { reader.close(); } try { if (null != bos) { bos.close(); } } catch (IOException e) { System.out.println("failed to close ByteArrayOutputStream "); } try { if (null != ps) { ps.close(); } } catch (DocumentException e) { System.out.println("failed to close PdfStamper "); } catch (IOException e) { System.out.println("failed to close PdfStamper "); } try { if (null != fos) { fos.close(); } } catch (IOException e) { System.out.println("failed to close FileOutputStream "); }
} return filename; } }</pre>
二、測試類:
package test; import java.util.HashMap; import java.util.Map; import pdf.ExportPdfUtil; public class TestPdf { public static void main(String[] args) { Map<String,String> content=new HashMap<String,String>(); content.put("name", "你好");//根據模板定義的輸入域的名字(如:name),填充值 ExportPdfUtil.exportpdf("C:\\", "C:\\template.pdf", "c://windows//fonts//simsun.ttc,1", content); } }