【java】 iText使用PDF模板生成输出PDF
项目需求涉及到操作pdf模板,根据生成好的模板向里面填充数据
用到的jar包是iText-5.0.6.jar 和iTextAsian.jar
pdf模板效果如下:
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class Test {
public static void main(String[] args) throws Exception {
test();
System.out.println("success");
}
public static void test() throws IOException, DocumentException {
String fileName = "F:/zxing/zs/zsTemp.pdf"; // pdf模板
PdfReader reader = new PdfReader(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
/* 将要生成的目标PDF文件名称 */
PdfStamper ps = new PdfStamper(reader, bos);
PdfContentByte under = ps.getUnderContent(1);
/* 使用中文字体 */
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
fontList.add(bf);
/* 取出报表模板中的所有字段 */
AcroFields fields = ps.getAcroFields();
fields.setSubstitutionFonts(fontList);
fillData(fields, data());
/* 必须要调用这个,否则文档不会生成的 */
ps.setFormFlattening(true);
ps.close();
OutputStream fos = new FileOutputStream("F:/zxing/zs/zsResult.pdf");
fos.write(bos.toByteArray());
fos.flush();
fos.close();
bos.close();
}
public static void fillData(AcroFields fields, Map<String, String> data)
throws IOException, DocumentException {
for (String key : data.keySet()) {
String value = data.get(key);
fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
}
}
public static Map<String, String> data() {
Map<String, String> data = new HashMap<String, String>();
data.put("name", "test:");
data.put("bianhao", "xx第10000001号");
data.put("amount", "1000");
data.put("date","2015年7月7日");
return data;
}
}
java 操作pdf模板(向指定域添加文本内容和图片)
public static void main(String[] args)
{
try
{
String TemplatePDF = "e:/F-3.pdf"; //魔板路径
String outFile = "e:/test.pdf"; //生成新的pdf的路径
PdfReader reader = new PdfReader(TemplatePDF);
PdfStamper ps = new PdfStamper(reader, new FileOutputStream(outFile)); // 生成的输出流
AcroFields s = ps.getAcroFields();
// 插入文字
insertText(ps, s);
// 插入图片
insertImage(ps, s);
ps.close();
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 插入图片
*
* @param ps
* @param s
* @author WangMeng
* @date 2016年6月16日
*/
public static void insertImage(PdfStamper ps, AcroFields s)
{
try
{
List<AcroFields.FieldPosition> list = s.getFieldPositions("QR_CODE");
Rectangle signRect = list.get(0).position;
Image p_w_picpath = Image.getInstance("e:/pdf.jpg");
PdfContentByte under = ps.getOverContent(2);
float x = signRect.getLeft();
float y = signRect.getBottom();
System.out.println(x);
System.out.println(y);
p_w_picpath.setAbsolutePosition(x, y);
p_w_picpath.scaleToFit(signRect.getWidth(), signRect.getHeight());
under.addImage(p_w_picpath);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建Chunk
*
* @return
* @author WangMeng
* @date 2016年6月16日
*/
public static Chunk CreateChunk()
{
BaseFont bfChinese;
Chunk chunk = null;
try
{
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
Font fontChinese = new Font(bfChinese, 10 * 4 / 3);
chunk = new Chunk("张三", fontChinese);
}
catch (Exception e)
{
e.printStackTrace();
}
return chunk;
}
/**
* 插入文本
*
* @return
* @author WangMeng
* @date 2016年6月16日
*/
public static void insertText(PdfStamper ps, AcroFields s)
{
List<AcroFields.FieldPosition> list = s.getFieldPositions("CONNECT_NAME");
Rectangle rect = list.get(0).position;
PdfContentByte cb = ps.getOverContent(1);
PdfPTable table = new PdfPTable(1);
float tatalWidth = rect.getRight() - rect.getLeft() - 1;
table.setTotalWidth(tatalWidth);
PdfPCell cell = new PdfPCell(new Phrase(CreateChunk()));
cell.setFixedHeight(rect.getTop() - rect.getBottom() - 1);
cell.setBorderWidth(0);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.1);
table.addCell(cell);
table.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), cb);
}
jar包下载地址:http://down.51cto.com/data/2300129