代码语言:javascript复制
public class PdfUtils {
// 利用模板生成pdf
public static void pdfout(Map<String,Object> o,String newPDFPath){
// 模板路径
String templatePath = "C:/Users/pc/Desktop/990696 list -1.pdf";
// 生成的新文件路径
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
List<PdfReader> Pdflist = new ArrayList();
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);
PdfReader pdfReaders = null;
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);
}*/
Map<String, List<List<String>>> listMap = (Map<String, List<List<String>>>) o.get("list");
for (String key : listMap.keySet()) {
List<List<String>> lists = listMap.get(key);
System.out.println("lists" lists);
int pageNo = form.getFieldPositions(key).get(0).page;
System.out.println(pageNo "page");
PdfContentByte pcb = stamper.getOverContent(pageNo);
Rectangle signRect = form.getFieldPositions(key).get(0).position;
//表格位置
int column = lists.get(0).size();
System.out.println(column "column");
int row = lists.size();
System.out.println("row" row);
PdfPTable table = new PdfPTable(column);
float tatalWidth = signRect.getRight() - signRect.getLeft() - 1;
System.out.println(tatalWidth);
int size = lists.get(0).size();
float width[] = new float[size];
for (int i = 0; i < size; i ) {
if (i == 0) {
width[i] = 60f;
} else {
width[i] = (tatalWidth - 60) / (size - 1);
}
}
System.out.println(width);
table.setTotalWidth(width);
table.setLockedWidth(true);
table.setKeepTogether(true);
table.setSplitLate(true);
table.setSplitRows(true);
Font FontProve = new Font(bf, 10, 0);
//表格数据填写
if(row>20) {
System.out.println("进来啦" row);
for (int i = 0; i < 23; i ) {
int k = i 1;
List<String> list = lists.get(i);
for (int j = 0; j < column; j ) {
Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);
PdfPCell cell = new PdfPCell(paragraph);
//cell.setBorderWidth(1);
// cell.disableBorderSide(15);//全没了
if (i == (row - 2)) {
System.out.println("i是:" (row - 2));
BaseFont bftemp = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontProve = new Font(bftemp, 10, Font.BOLD, BaseColor.BLACK);
//cell.setBorderWidth(1);
cell.disableBorderSide(14);//全没了
} else if (i == (row - 1)) {
System.out.println("i是:" (row - 1));
cell.setColspan(5);
cell.setBorderWidth(0);
//cell.disableBorderSide(13);
} else {
cell.disableBorderSide(15);
}
//cell.disableBorderSide(15);//全没
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.4);
table.addCell(cell);
}
}
}else if(row<=20) {
System.out.println("哈哈哈哈" row);
for (int i = 0; i < row; i ) {
List<String> list = lists.get(i);
for (int j = 0; j < column; j ) {
Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);
PdfPCell cell = new PdfPCell(paragraph);
//cell.setBorderWidth(1);
// cell.disableBorderSide(15);//全没了
if (i == (row - 2)) {
System.out.println("i是:" (row - 2));
BaseFont bftemp = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontProve = new Font(bftemp, 10, Font.BOLD, BaseColor.BLACK);
//cell.setBorderWidth(1);
cell.disableBorderSide(14);//全没了
} else if (i == (row - 1)) {
System.out.println("i是:" (row - 1));
cell.setColspan(5);
cell.setBorderWidth(0);
//cell.disableBorderSide(13);
} else {
cell.disableBorderSide(15);
}
//cell.disableBorderSide(15);//全没
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.4);
table.addCell(cell);
}
}
}
table.setSpacingAfter(40f);
table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), pcb);
}
Map<String, List<List<String>>> listMaps = (Map<String, List<List<String>>>) o.get("listss");
for (String key : listMaps.keySet()) {
List<List<String>> lists = listMaps.get(key);
System.out.println("lists" lists);
int pageNo = form.getFieldPositions(key).get(0).page;
PdfContentByte pcb = stamper.getOverContent(pageNo);
Rectangle signRect = form.getFieldPositions(key).get(0).position;
//表格位置
int column = lists.get(0).size();
int row = lists.size();
System.out.println(column "column" "row" row);
PdfPTable table = new PdfPTable(column);
float tatalWidth = signRect.getRight() - signRect.getLeft() - 1;
System.out.println(tatalWidth);
int size = lists.get(0).size();
float width[] = new float[size];
for(int i=0;i<size;i ){
if(i==0){
width[i]=100f;
}else{
width[i]=(tatalWidth-60)/(size-1);
}
}
System.out.println(width);
table.setTotalWidth(width);
table.setLockedWidth(true);
table.setKeepTogether(true);
table.setSplitLate(false);
table.setSplitRows(true);
Font FontProve = new Font(bf, 8, Font.BOLD,BaseColor.BLACK);
//表格数据填写
for(int i=0;i<row;i ){
List<String> list = lists.get(i);
for(int j=0;j<column;j ){
Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);
PdfPCell cell = new PdfPCell(paragraph);
cell.disableBorderSide(14);//全没了
if(i==0){
BaseFont bftemp = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontProve = new Font(bftemp, 5, Font.NORMAL, BaseColor.BLACK);
cell.disableBorderSide(14);//全没了
}else if(i==(row-1)){
cell.disableBorderSide(15);
}else{
cell.disableBorderSide(15);
}
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.4);
table.addCell(cell);
}
}
table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), pcb);
}
stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
stamper.close();
pdfReaders = new PdfReader(bos.toByteArray());
Pdflist.add(pdfReaders);
Document doc = new Document();
Font font = new Font(bf, 32);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
int pageNum = reader.getNumberOfPages();
System.out.println(pageNum "页数");
for(int k = 1;k <= pageNum;k ){
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), k);
System.out.println(importPage);
doc.newPage();
copy.addPage(importPage);
copy.addDocument(new PdfReader(bos.toByteArray()));
System.out.println("pdf" Pdflist.size());
// PdfReader pdfReader = Pdflist.get(k);
// PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), k);
// doc.newPage();
// copy.addDocument(pdfReader);
// copy.addPage(importPage);
}
doc.close();
} catch (IOException e) {
System.out.println(e);
} catch (DocumentException e) {
System.out.println(e);
}
}
public static void pdfoutEn(Map<String,Object> s,String newPDFPath){
// 模板路径
String templatePath = "C:/Users/pc/Desktop/990696 list en.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>)s.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);
}*/
Map<String, List<List<String>>> listMap = (Map<String, List<List<String>>>) s.get("lists");
for (String key : listMap.keySet()) {
List<List<String>> lists = listMap.get(key);
System.out.println("lists" lists);
int pageNo = form.getFieldPositions(key).get(0).page;
System.out.println(pageNo "page");
PdfContentByte pcb = stamper.getOverContent(pageNo);
Rectangle signRect = form.getFieldPositions(key).get(0).position;
//表格位置
int column = lists.get(0).size();
System.out.println(column "column");
int row = lists.size();
System.out.println("row" row);
PdfPTable table = new PdfPTable(column);
float tatalWidth = signRect.getRight() - signRect.getLeft() - 1;
System.out.println(tatalWidth);
int size = lists.get(0).size();
float width[] = new float[size];
for(int i=0;i<size;i ){
if(i==0){
width[i]=60f;
}else{
width[i]=(tatalWidth-60)/(size-1);
}
}
System.out.println(width);
table.setTotalWidth(width);
table.setLockedWidth(true);
table.setKeepTogether(true);
table.setSplitLate(true);
table.setSplitRows(true);
Font FontProve = new Font(bf, 10, 0);
//表格数据填写
if(row>20) {
System.out.println("进来啦" row);
for (int i = 0; i < 23; i ) {
int k = i 1;
List<String> list = lists.get(i);
for (int j = 0; j < column; j ) {
Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);
PdfPCell cell = new PdfPCell(paragraph);
//cell.setBorderWidth(1);
// cell.disableBorderSide(15);//全没了
if (i == (row - 2)) {
System.out.println("i是:" (row - 2));
BaseFont bftemp = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontProve = new Font(bftemp, 10, Font.BOLD, BaseColor.BLACK);
//cell.setBorderWidth(1);
cell.disableBorderSide(14);//全没了
} else if (i == (row - 1)) {
System.out.println("i是:" (row - 1));
cell.setColspan(5);
cell.setBorderWidth(0);
//cell.disableBorderSide(13);
} else {
cell.disableBorderSide(15);
}
//cell.disableBorderSide(15);//全没
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.4);
table.addCell(cell);
}
}
}else if(row<=20) {
System.out.println("哈哈哈哈" row);
for (int i = 0; i < row; i ) {
List<String> list = lists.get(i);
for (int j = 0; j < column; j ) {
Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);
PdfPCell cell = new PdfPCell(paragraph);
//cell.setBorderWidth(1);
// cell.disableBorderSide(15);//全没了
if (i == (row - 2)) {
System.out.println("i是:" (row - 2));
BaseFont bftemp = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontProve = new Font(bftemp, 10, Font.BOLD, BaseColor.BLACK);
//cell.setBorderWidth(1);
cell.disableBorderSide(14);//全没了
} else if (i == (row - 1)) {
System.out.println("i是:" (row - 1));
cell.setColspan(5);
cell.setBorderWidth(0);
//cell.disableBorderSide(13);
} else {
cell.disableBorderSide(15);
}
//cell.disableBorderSide(15);//全没
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.4);
table.addCell(cell);
}
}
}
table.setSpacingAfter(40f);
table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), pcb);
}
Map<String, List<List<String>>> listMaps = (Map<String, List<List<String>>>) s.get("listen");
for (String key : listMaps.keySet()) {
List<List<String>> lists = listMaps.get(key);
System.out.println("lists" lists);
int pageNo = form.getFieldPositions(key).get(0).page;
PdfContentByte pcb = stamper.getOverContent(pageNo);
Rectangle signRect = form.getFieldPositions(key).get(0).position;
//表格位置
int column = lists.get(0).size();
int row = lists.size();
System.out.println(column "column" "row" row);
PdfPTable table = new PdfPTable(column);
float tatalWidth = signRect.getRight() - signRect.getLeft() - 1;
System.out.println(tatalWidth);
int size = lists.get(0).size();
float width[] = new float[size];
for(int i=0;i<size;i ){
if(i==0){
width[i]=100f;
}else{
width[i]=(tatalWidth-60)/(size-1);
}
}
System.out.println(width);
table.setTotalWidth(width);
table.setLockedWidth(true);
table.setKeepTogether(true);
table.setSplitLate(false);
table.setSplitRows(true);
Font FontProve = new Font(bf, 8, Font.BOLD,BaseColor.BLACK);
//表格数据填写
for(int i=0;i<row;i ){
List<String> list = lists.get(i);
for(int j=0;j<column;j ){
Paragraph paragraph = new Paragraph(String.valueOf(list.get(j)), FontProve);
PdfPCell cell = new PdfPCell(paragraph);
cell.disableBorderSide(14);//全没了
if(i==0){
BaseFont bftemp = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontProve = new Font(bftemp, 5, Font.NORMAL, BaseColor.BLACK);
cell.disableBorderSide(14);//全没了
}else if(i==(row-1)){
cell.disableBorderSide(15);
}else{
cell.disableBorderSide(15);
}
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(0, (float) 1.4);
table.addCell(cell);
}
}
table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), pcb);
}
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();
int pageNum = reader.getNumberOfPages();
System.out.println(pageNum "页数");
for(int i = 1;i <= pageNum;i ){
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
System.out.println(importPage);
doc.newPage();
// copy.addDocument("j");
copy.addPage(importPage);
}
doc.close();
} catch (IOException e) {
System.out.println(e);
} catch (DocumentException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
Map<String,String> map = new HashMap();
Map<String,String> maps = new HashMap();
map.put("invoiceNo","990696");
map.put("deliverycompany","湖南省长沙市芙蓉区");
map.put("inviocecompany","湖南省长沙市雨花区");
map.put("paymethod","支付宝");
map.put("deliverymethod","邮寄");
map.put("deliverymethod","邮寄");
map.put("forwarder","申通快递");
map.put("deliverydate","2019-9-19");
map.put("orderReference","百度一下,你就知道");
map.put("referenceOrderNo","R1112112222");
map.put("invoice_no","990696");
map.put("invoicedate","2019-9-20");
map.put("dudate","2019-10-20");
map.put("companyNo","224542");
//map.put("product_no","22454222");
//map.put("description","该产品非常好");
//map.put("deliveryItemCount","100");
//map.put("orderPrice","23");
//map.put("orderPriceCount","230");
map.put("invoicetotal","230");
map.put("companyName","万特瑞有限公司");
map.put("tel","电话 12345678 " "—" "传真 0215");
maps.put("invoiceNo","990696");
maps.put("deliverycompany","Furong District, Changsha City, Hunan Province");
maps.put("inviocecompany","Furong District, Changsha City, Hunan Province");
maps.put("paymethod","zhifubao");
maps.put("deliverymethod","youji");
maps.put("deliverymethod","youji");
maps.put("forwarder","shentong");
maps.put("deliverydate","2019-9-19");
maps.put("orderReference","baiduyixia,nijiuzhidao");
maps.put("referenceOrderNo","R1112112222");
maps.put("invoice_no","990696");
maps.put("invoicedate","2019-9-20");
maps.put("dudate","2019-10-20");
maps.put("companyNo","224542");
//map.put("product_no","22454222");
//map.put("description","该产品非常好");
//map.put("deliveryItemCount","100");
//map.put("orderPrice","23");
//map.put("orderPriceCount","230");
maps.put("invoicetotal","230");
maps.put("companyName","wanruiteyouxiangongs");
maps.put("tel","tel 12345678 " "—" "fax 0215");
Map<String,String> map2 = new HashMap();
// map2.put("img","C:/Users/pc/Desktop/timg.jpg");
List<String> list11 = new ArrayList<String>();
list11.add(" ");
list11.add(" ");
list11.add(" ");
list11.add("总价");
list11.add("630");
List<String> list11en = new ArrayList<String>();
list11en.add(" ");
list11en.add(" ");
list11en.add(" ");
list11en.add("total");
list11en.add("630");
//表格 一行数据是一个list
List<List<String>> List = new ArrayList<List<String>>();
List<List<String>> Lists = new ArrayList<List<String>>();
for(int i=0;i<=40;i ){
List<String> list = new ArrayList<String>();
List<String> listen = new ArrayList<String>();
list.add("22454223");
list.add("该产品非常好");
list.add("200");
list.add("23");
list.add("460");
List.add(list);
listen.add("22454223");
listen.add("very good");
listen.add("200");
listen.add("23");
listen.add("460");
Lists.add(listen);
}
List<String> list12 = new ArrayList<String>();
list12.add(" ");
list12.add(" ");
list12.add(" ");
list12.add(" ");
list12.add("订单交付后,如有任何问题,请在7天内与我们联系,否则,我们将不受理任何索赔.");
List<String> list12en = new ArrayList<String>();
list12en.add(" ");
list12en.add(" ");
list12en.add(" ");
list12en.add(" ");
list12en.add("UPON ORDER DELIVERY, PLEASE CONTACT US IF ANY PROBLEM NOT LATER THAN 7 DAYS, OTHERWISE, NO CLAIM WILL BE RECOGNIZED.");
List.add(list11);
List.add(list12);
Lists.add(list11en);
Lists.add(list12en);
Map<String, List<List<String>>> listMap = new HashMap<String, List<List<String>>>();
listMap.put("eventList", List);
Map<String, List<List<String>>> listMapen = new HashMap<String, List<List<String>>>();
listMapen.put("eventList", Lists);
List<List<String>> ListAddress = new ArrayList<List<String>>();
List<String> listdd = new ArrayList<String>();
listdd.add("上海浦东新区");
List<String> listss = new ArrayList<String>();
listss.add("上海浦东新区华夏路");
ListAddress.add(listdd);
ListAddress.add(listss);
Map<String, List<List<String>>> listMaps = new HashMap<String, List<List<String>>>();
listMaps.put("companyAdress",ListAddress );
List<List<String>> ListAddressen = new ArrayList<List<String>>();
List<String> listdden = new ArrayList<String>();
listdden.add("Shanghai Pudong New Area");
List<String> listssen = new ArrayList<String>();
listssen.add("Shanghai Pudong New Area huaxiarod");
ListAddressen.add(listdden);
ListAddressen.add(listssen);
Map<String, List<List<String>>> listMapsen = new HashMap<String, List<List<String>>>();
listMapsen.put("companyAdress", ListAddressen);
Map<String,Object> o=new HashMap();
o.put("datemap",map);
// o.put("imgmap",map2);
o.put("list",listMap);
o.put("listss",listMaps);
Map<String,Object> s=new HashMap();
s.put("datemap",map);
s.put("lists",listMapen);
s.put("listen",listMapsen);
String uid=CommonUtils.uuid();
System.out.println(uid "uid");
String newPDFPath ="C:/Users/pc/Desktop/" uid ".pdf";
pdfout(o,newPDFPath);
String newPDFPaths ="C:/Users/pc/Desktop/" "I" uid ".pdf";
PdfUtils.pdfoutEn(s,newPDFPaths);
}
}
我的博客即将同步至腾讯云 社区,邀请大家一同入驻:
https://cloud.tencent.com/developer/support-plan?invite_code=2nviihzlpmgwg