從:c/system/fonts/DENG.TTF拷到resources/fonts/目錄下。先找到等線,點擊進去,複製 《 等線 常規 》 這個字體 。 等線常規 就是DENG.TTF
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("applyNo","NEW201906172519");
map.put("loanTerm","3");
map.put("loanDate","2020-05-20");
map.put("finishDate","2020-08-20");
map.put("loanamt","25833.25");
map.put("amountRepaid","25833.25");
map.put("loanStatus", "歮靐斆龘我");
map.put("cardNo", "贛A DN983");
map.put("vinNo", "BY23424324");
map.put("modelName", "2020款 運動版 1.5T 自動兩驅精英型");
map.put("mortgagePerson","中國人壽財產保險股份有限公司");
map.put("carEquityNo", "New4234324324432432");
Map<String,Object> o=new HashMap<String,Object>();
o.put("dataMap",map);
pdfout(o);
}
public static void pdfout(Map<String,Object> o){
// 模板路徑
String templatePath = getTemplatePath();
System.out.println("模板路徑:"+templatePath);
// 生成的新文件路徑
String newPDFPath = "d:/test3.pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
String fontPath = getFontPath();
System.out.println("fontPath:"+fontPath);
BaseFont bf = BaseFont.createFont(fontPath , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//Font FontChinese = new Font(bf, 5, Font.NORMAL);
out = new FileOutputStream(newPDFPath);// 輸出流
reader = new PdfReader(templatePath);// 讀取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//文字類的內容處理
Map<String,String> datemap = (Map<String,String>)o.get("dataMap");
form.addSubstitutionFont(bf);
for(String key : datemap.keySet()){
String value = datemap.get(key);
form.setField(key,value);
}
stamper.setFormFlattening(true);// 若是爲false,生成的PDF文件能夠編輯,若是爲true,生成的PDF文件不能夠編輯
stamper.close();
Document doc = new Document();
//Font font = new Font(bf, 32);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (IOException e) {
System.out.println(e);
} catch (DocumentException e) {
System.out.println(e);
}
}
private static String getFontPath(){
String path="";
// 1. 生產環境路徑
ClassLoader classLoader= Thread.currentThread().getContextClassLoader();
java.net.URL url = (classLoader==null)?null:classLoader.getResource("/");
String threadCurrentPath = (url==null)?"":url.getPath();
// 2. 若是線程獲取爲null,則使用當前PdfUtils.class加載路徑
if(threadCurrentPath == null || "".equals(threadCurrentPath.trim())){
path = PdfUtils2.class.getClass().getResource("/").getPath();
}
// 3.拼接字體路徑
StringBuffer stringBuffer = new StringBuffer(path);
stringBuffer.append("/fonts/DENG.TTF");
path = stringBuffer.toString();
//logger.info("getFontPath threadCurrentPath: {} path: {}",threadCurrentPath,path);
return path;
}
private static String getTemplatePath(){
String path="";
// 1. 生產環境路徑
ClassLoader classLoader= Thread.currentThread().getContextClassLoader();
java.net.URL url = (classLoader==null)?null:classLoader.getResource("/");
String threadCurrentPath = (url==null)?"":url.getPath();
// 2. 若是線程獲取爲null,則使用當前PdfUtils.class加載路徑
if(threadCurrentPath == null || "".equals(threadCurrentPath.trim())){
path = PdfUtils2.class.getClass().getResource("/").getPath();
}
// 3.拼接字體路徑
StringBuffer stringBuffer = new StringBuffer(path);
stringBuffer.append("/dai_adobe.pdf");
path = stringBuffer.toString();
//logger.info("getFontPath threadCurrentPath: {} path: {}",threadCurrentPath,path);
return path;
}