3 changed files with 251 additions and 35 deletions
@ -0,0 +1,101 @@ |
|||
package com.ccsens.util; |
|||
|
|||
import com.itextpdf.text.*; |
|||
import com.itextpdf.text.pdf.*; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.junit.Test; |
|||
import org.springframework.core.io.DefaultResourceLoader; |
|||
import org.springframework.core.io.ResourceLoader; |
|||
|
|||
import javax.script.ScriptException; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.FileOutputStream; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
public class PDFTest { |
|||
|
|||
@Test |
|||
public void test1() throws IOException { |
|||
Map<String,String> map = new HashMap(); |
|||
map.put("undefined","张三"); |
|||
|
|||
|
|||
Map<String,String> map2 = new HashMap(); |
|||
map2.put("img","c:/50336.jpg"); |
|||
|
|||
Map<String,Object> o=new HashMap(); |
|||
o.put("datemap",map); |
|||
o.put("imgmap",map2); |
|||
pdfout(o); |
|||
|
|||
} |
|||
|
|||
// 利用模板生成pdf
|
|||
@Test |
|||
public static void pdfout(Map<String,Object> o) { |
|||
// 模板路径
|
|||
String templatePath = "C:/Users/10626/Desktop/静脉溶栓、血管内介入治疗数据直报表(试行)Form.pdf"; |
|||
// 生成的新文件路径
|
|||
String newPDFPath = "C:/Users/10626/Desktop/testout1.pdf"; |
|||
|
|||
PdfReader reader; |
|||
FileOutputStream out; |
|||
ByteArrayOutputStream bos; |
|||
PdfStamper stamper; |
|||
try { |
|||
BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1" , 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("datemap"); |
|||
form.addSubstitutionFont(bf); |
|||
for(String key : datemap.keySet()){ |
|||
String value = datemap.get(key); |
|||
form.setField(key,value); |
|||
} |
|||
//图片类的内容处理
|
|||
Map<String,String> imgmap = (Map<String,String>)o.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); |
|||
} |
|||
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); |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue