1.正文。因为公司需要将数据以报表的形式把数据导出。在网上查了许久的资料最终决定以itext生成pdf然后再导出报表
因为写的时候借鉴了很多大神的东东,然后我也忘了记下他们的网址了。在次说一声抱歉哈。使用的jar包就只有itext-2.0.8.jar
2.直接上代码
package com.declare.utils.print;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import com.alibaba.fastjson.JSON;
import com.declare.pageModel.base.Json;
import com.declare.pageModel.base.SessionInfo;
import com.declare.pageModel.sys.PrintExcelEntity;
import com.declare.utils.DataUtils;
import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTable;
import com.lowagie.text.pdf.PdfWriter;
public class DataToPdf {
public static void main(String[] args) {
try {
//jsonToentity("","");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void jsonToentity(String feilpath,String printhrmljson) throws Exception{
//tabledata是我的json数据,然后是根据此数据来生成报表。printhrmljson=tabledata。feilpath是生成pdf文件的路径
// String tabledata="[{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 13:51:29','remarks':null,'status':'Offline','testResult':'Pass','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:53','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:51','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:49','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:46','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:43','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:41','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:38','remarks':null,'status':'Offline','testResult':'Pass','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:36','remarks':null,'status':'Offline','testResult':'Pass','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:34','remarks':null,'status':'Offline','testResult':'Pass','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:32','remarks':null,'status':'Offline','testResult':'Pass','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-23 16:45:29','remarks':null,'status':'Offline','testResult':'Pass','testtype':'Functional Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-15 15:20:14','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Duration Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-22 15:20:24','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Duration Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-19 15:20:37','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Duration Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-14 15:20:50','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Duration Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-26 15:21:01','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Duration Test'},{'battery':'80','groupname':'GROUP THREE','hostname':'020000E0','mains':'battery','name':'','num':'05000001','previousTest':'2018-11-19 15:21:11','remarks':null,'status':'Offline','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-24 17:56:30','remarks':null,'status':'Online','testResult':'Pass','testtype':'Functional Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-24 17:58:38','remarks':null,'status':'Online','testResult':'Pass','testtype':'Functional Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-24 18:00:46','remarks':null,'status':'Online','testResult':'Pass','testtype':'Functional Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-25 15:15:30','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-25 15:16:39','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-23 15:16:52','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:17:04','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-20 15:17:18','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-20 15:17:35','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-20 15:17:54','remarks':null,'status':'Online','testResult':'Fail','testtype':'Functional Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:06','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-18 15:19:20','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'},{'battery':'10','groupname':'GROUP THREE','hostname':'000000E0','mains':'AC','name':'','num':'050000E0','previousTest':'2018-11-21 15:19:32','remarks':null,'status':'Online','testResult':'Fail','testtype':'Duration Test'}]";
//拿到国际化信息
Locale local=ResourceUtil.getlocaleinfo();
List<PrintExcelEntity> rntu=new ArrayList<PrintExcelEntity>();
rntu = JSON.parseArray(printhrmljson,PrintExcelEntity.class);
Document document = new Document(PageSize.A4, 25, 25, 25, 25);
// File myPath = new File("D:/test.pdf");
File myPath = new File(feilpath);
if (!myPath.exists()){
myPath.createNewFile();
}
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(myPath));// throws FileNotFoundException,
//设置页面分页的页数
writer.setPageEvent(new NewPdfPagehe());
document.open();
BaseFont bfChinese = null;// FontFactory.getFont(FontFactory.COURIER,
// 14, Font.BOLD, new CMYKColor(0, 255, 0, 0);//大小,粗细,颜色
try {
//经过我使用,我发现黑体的效果最好
if(local.getLanguage().equals("hk")){
bfChinese = BaseFont.createFont("D:\\apache-tomcat-7.0.52\\webapps\\yidsys\\pdfFile\\msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
}else if(local.getLanguage().equals("zh")){
bfChinese = BaseFont.createFont("D:\\apache-tomcat-7.0.52\\webapps\\yidsys\\pdfFile\\msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
}else{
bfChinese = BaseFont.createFont("D:\\apache-tomcat-7.0.52\\webapps\\yidsys\\pdfFile\\msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
}
} catch (IOException e) {
e.printStackTrace();
}
//定义中文字体大小
Font f8 = new Font(bfChinese, 8, Font.NORMAL);
Font f10= new Font(bfChinese, 10, Font.NORMAL);
Font f12 = new Font(bfChinese, 10, Font.NORMAL);//其实是10号字体
Font f26 = new Font(bfChinese, 26, Font.NORMAL);//一号字体
Font f14 = new Font(bfChinese,14, Font.NORMAL);//一号字体
//这个是设置页面边距的。一次是左右上下
document.setMargins(20,20,100,50);
/*
* 创建标题,因为我每个页面都需要页眉和标题,所以这里的标题是空的
*/
String localtitle="";
Paragraph title1 = new Paragraph(localtitle, f8);
title1.setAlignment(Element.ALIGN_CENTER);
Chapter chapter1 = new Chapter(title1,0);
chapter1.setNumberDepth(0);
Section section1 = chapter1;
//创建表格
float[] cellswidth={0.2f,0.2f,0.2f,0.2f,0.2f,0.25f,0.3f,0.2f,0.2f,0.2f}; // 定义表格的宽度
PdfPTable table=new PdfPTable(10);
table.setSpacingBefore(20);//设置段落上空白
table.setSpacingAfter(20);//设置段落上下空白
table.setHorizontalAlignment(Element.ALIGN_CENTER);//居中
table.setWidths(cellswidth);//设置表格的宽
table.setTotalWidth(1000f);//设置表格的总宽度
table.setWidthPercentage(100);//设置表格宽度的百分比
// table.setHeaderRows(1);
table.setSplitLate(false);
table.setSplitRows(false);//这个是设置表格的行是否跨页显示,我选择的是不
//这些是设置表头的值。我是动态获取的
String num="";
String name="";
String groupname="";
String hostname="";
String mains="";
String battery="";
String struts="";
String testtesult="";
String Print_Date="";
String Checkedby="";
String Signature="";
ResourceBundle bundle=null;
if(local.getLanguage().equals("hk")){
bundle = java.util.ResourceBundle.getBundle("message_hk_CN");
}else if(local.getLanguage().equals("zh")){
bundle = java.util.ResourceBundle.getBundle("message_zh_CN");
}else{
bundle = java.util.ResourceBundle.getBundle("message_en_US");
}
num=bundle.getString("lamp_dynum");
name=bundle.getString("lamp_dyname");
hostname=bundle.getString("lamp_dyHost");
groupname=bundle.getString("lamp_dysugroup");
battery=bundle.getString("lamp_dybatterylevel");
struts=bundle.getString("lamp_dystruts");
testtesult=bundle.getString("lamp_dytestresult");
Checkedby=bundle.getString("lamp_checkp");
Print_Date=bundle.getString("lamp_PrintDate");
Signature=bundle.getString("lamp_Signature");
mains=bundle.getString("lamp_dymains");
//设置表头
PdfPCell C1=new PdfPCell(new Paragraph(num,f12));
C1.setFixedHeight(30);
C1.setHorizontalAlignment(Element.ALIGN_CENTER);
C1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(C1);
PdfPCell C7=new PdfPCell(new Paragraph(name,f12));
C7.setFixedHeight(30);
C7.setHorizontalAlignment(Element.ALIGN_CENTER);
C7.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(C7);
PdfPCell C2=new PdfPCell(new Paragraph(groupname,f12));
C2.setHorizontalAlignment(Element.ALIGN_CENTER);
C2.setVerticalAlignment(Element.ALIGN_MIDDLE);
C2.setFixedHeight(30);
table.addCell(C2);
PdfPCell C3=new PdfPCell(new Paragraph(hostname,f12));
C3.setHorizontalAlignment(Element.ALIGN_CENTER);
C3.setVerticalAlignment(Element.ALIGN_MIDDLE);
C3.setFixedHeight(30);
table.addCell(C3);
PdfPCell C4=new PdfPCell(new Paragraph(mains,f12));
C4.setHorizontalAlignment(Element.ALIGN_CENTER);
C4.setVerticalAlignment(Element.ALIGN_MIDDLE);
C4.setFixedHeight(30);
table.addCell(C4);
PdfPCell C6=new PdfPCell(new Paragraph(struts,f12));
C6.setHorizontalAlignment(Element.ALIGN_CENTER);
C6.setVerticalAlignment(Element.ALIGN_MIDDLE);
C6.setFixedHeight(30);
table.addCell(C6);
PdfPCell C5=new PdfPCell(new Paragraph(battery,f12));
C5.setHorizontalAlignment(Element.ALIGN_CENTER);
C5.setVerticalAlignment(Element.ALIGN_MIDDLE);
C5.setFixedHeight(30);
table.addCell(C5);
PdfPCell C8=new PdfPCell(new Paragraph(testtesult,f12));
C8.setHorizontalAlignment(Element.ALIGN_CENTER);
C8.setVerticalAlignment(Element.ALIGN_MIDDLE);
C8.setFixedHeight(30);
C8.setColspan(3);
table.addCell(C8);
String num_lamp="";
//给表格填充数据
for(PrintExcelEntity f:rntu)
{
if("".equalsIgnoreCase(num_lamp))
{
num_lamp=f.getNum();
addcelltou(table,f8,f,true);
}else {
if(num_lamp.equalsIgnoreCase(f.getNum()))
{
addcelltou(table,f8,f,false);
}else{
num_lamp=f.getNum();
addcelltou(table,f8,f,true);
}
}
PdfPCell PreviousTest=new PdfPCell(new Paragraph(f.getPreviousTest(),f8));
//这两个是设置居中
PreviousTest.setHorizontalAlignment(Element.ALIGN_CENTER);
PreviousTest.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(PreviousTest);
PdfPCell Testtype=new PdfPCell(new Paragraph(f.getTesttype(),f8));
Testtype.setHorizontalAlignment(Element.ALIGN_CENTER);
Testtype.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(Testtype);
PdfPCell TestResult=new PdfPCell(new Paragraph(f.getTestResult(),f8));
TestResult.setHorizontalAlignment(Element.ALIGN_CENTER);
TestResult.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(TestResult);
}
//生成上面的主表格后,在后天添加审查人的信息,也就是添加了一个表中表
float[] cellswidth1={0.05f,0.0235f}; // 定义表格的宽度
PdfPCell TestResult1=new PdfPCell();
PdfPTable iTable=new PdfPTable(2);
iTable.setHorizontalAlignment(Element.ALIGN_RIGHT);
iTable.setWidths(cellswidth1);//设置表格的宽
table.setTotalWidth(200f);//设置表格的总宽度
table.setWidthPercentage(100);//设置表格宽度的百分比
TestResult1.setBorder(0);
TestResult1.setHorizontalAlignment(Element.ALIGN_RIGHT);
TestResult1.addElement(iTable);
TestResult1.setBorder(0);
TestResult1.setColspan(10);
for(int i=0;i<3;i++)
{
if(i==0)
{
PdfPCell ltable1=new PdfPCell(new Paragraph(Print_Date,f10));
ltable1.setBorder(0);
ltable1.setFixedHeight(40);//这个是设置一行的高,我认为非常有用
ltable1.setHorizontalAlignment(Element.ALIGN_RIGHT);
ltable1.setVerticalAlignment(Element.ALIGN_BOTTOM);
iTable.addCell(ltable1);
String time="";
if(local.getLanguage().equals("hk")){
Calendar c =DataUtils.getCalendar();
int y=c.get(Calendar.YEAR);
int m=c.get(Calendar.MONTH)+1;
int d=c.get(Calendar.DATE);
time=d+"/"+m+"/"+y;
}else if(local.getLanguage().equals("zh")){
time=DataUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss");
}else{
Calendar c =DataUtils.getCalendar();
int y=c.get(Calendar.YEAR);
int m=c.get(Calendar.MONTH)+1;
int d=c.get(Calendar.DATE);
time=d+"/"+m+"/"+y;
}
PdfPCell ltable2=new PdfPCell(new Paragraph(" "+time,f8));
ltable2.setHorizontalAlignment(Element.ALIGN_LEFT);
ltable2.setVerticalAlignment(Element.ALIGN_BOTTOM);
ltable2.setBorder(0);
iTable.addCell(ltable2);
}else if(i==2){
PdfPCell ltable1=new PdfPCell(new Paragraph(Signature,f10));
ltable1.setBorder(0);
ltable1.setFixedHeight(40);
ltable1.setHorizontalAlignment(Element.ALIGN_RIGHT);
ltable1.setVerticalAlignment(Element.ALIGN_BOTTOM);
iTable.addCell(ltable1);
PdfPCell ltable2=new PdfPCell(new Paragraph("________________________________",f8));
ltable2.setHorizontalAlignment(Element.ALIGN_LEFT);
ltable2.setVerticalAlignment(Element.ALIGN_BOTTOM);
ltable2.setBorder(0);
iTable.addCell(ltable2);
}else{
PdfPCell ltable1=new PdfPCell(new Paragraph(Checkedby,f10));
ltable1.setBorder(0);
ltable1.setFixedHeight(40);
ltable1.setHorizontalAlignment(Element.ALIGN_RIGHT);
ltable1.setVerticalAlignment(Element.ALIGN_BOTTOM);
iTable.addCell(ltable1);
PdfPCell ltable2=new PdfPCell(new Paragraph("________________________________",f8));
ltable2.setHorizontalAlignment(Element.ALIGN_LEFT);
ltable2.setVerticalAlignment(Element.ALIGN_BOTTOM);
ltable2.setBorder(0);
iTable.addCell(ltable2);
}
}
table.addCell(TestResult1);
section1.add(table);
document.add(chapter1);
document.close();
}
public static void addcelltou(PdfPTable table,Font f8,PrintExcelEntity f,boolean flag){
if(flag)
{
PdfPCell num=new PdfPCell(new Paragraph(f.getNum(),f8));
num.setHorizontalAlignment(Element.ALIGN_CENTER);
num.setVerticalAlignment(Element.ALIGN_MIDDLE);
num.setFixedHeight(35);
table.addCell(num);
PdfPCell name_=new PdfPCell(new Paragraph(f.getName(),f8));
name_.setHorizontalAlignment(Element.ALIGN_CENTER);
name_.setVerticalAlignment(Element.ALIGN_MIDDLE);
name_.setFixedHeight(35);
table.addCell(name_);
PdfPCell groupname=new PdfPCell(new Paragraph(f.getGroupname(),f8));
groupname.setHorizontalAlignment(Element.ALIGN_CENTER);
groupname.setVerticalAlignment(Element.ALIGN_MIDDLE);
groupname.setFixedHeight(35);
table.addCell(groupname);
PdfPCell hostname=new PdfPCell(new Paragraph(f.getHostname(),f8));
hostname.setHorizontalAlignment(Element.ALIGN_CENTER);
hostname.setVerticalAlignment(Element.ALIGN_MIDDLE);
hostname.setFixedHeight(35);
table.addCell(hostname);
PdfPCell mains=new PdfPCell(new Paragraph(f.getMains(),f8));
mains.setHorizontalAlignment(Element.ALIGN_CENTER);
mains.setVerticalAlignment(Element.ALIGN_MIDDLE);
mains.setFixedHeight(35);
table.addCell(mains);
PdfPCell struts=new PdfPCell(new Paragraph(f.getStatus(),f8));
struts.setHorizontalAlignment(Element.ALIGN_CENTER);
struts.setVerticalAlignment(Element.ALIGN_MIDDLE);
struts.setFixedHeight(35);
table.addCell(struts);
PdfPCell battery=new PdfPCell(new Paragraph(f.getBattery()+"%",f8));
battery.setHorizontalAlignment(Element.ALIGN_CENTER);
battery.setVerticalAlignment(Element.ALIGN_MIDDLE);
battery.setFixedHeight(35);
table.addCell(battery);
}else{
PdfPCell num=new PdfPCell(new Paragraph("",f8));
num.setHorizontalAlignment(Element.ALIGN_CENTER);
num.setVerticalAlignment(Element.ALIGN_MIDDLE);
num.setFixedHeight(35);
table.addCell(num);
PdfPCell name_=new PdfPCell(new Paragraph("",f8));
name_.setHorizontalAlignment(Element.ALIGN_CENTER);
name_.setVerticalAlignment(Element.ALIGN_MIDDLE);
name_.setFixedHeight(35);
table.addCell(name_);
PdfPCell groupname=new PdfPCell(new Paragraph("",f8));
groupname.setHorizontalAlignment(Element.ALIGN_CENTER);
groupname.setVerticalAlignment(Element.ALIGN_MIDDLE);
groupname.setFixedHeight(35);
table.addCell(groupname);
PdfPCell hostname=new PdfPCell(new Paragraph("",f8));
hostname.setHorizontalAlignment(Element.ALIGN_CENTER);
hostname.setVerticalAlignment(Element.ALIGN_MIDDLE);
hostname.setFixedHeight(35);
table.addCell(hostname);
PdfPCell mains=new PdfPCell(new Paragraph("",f8));
mains.setHorizontalAlignment(Element.ALIGN_CENTER);
mains.setVerticalAlignment(Element.ALIGN_MIDDLE);
mains.setFixedHeight(35);
table.addCell(mains);
PdfPCell battery=new PdfPCell(new Paragraph("",f8));
battery.setHorizontalAlignment(Element.ALIGN_CENTER);
battery.setVerticalAlignment(Element.ALIGN_MIDDLE);
battery.setFixedHeight(35);
table.addCell(battery);
PdfPCell struts=new PdfPCell(new Paragraph("",f8));
struts.setHorizontalAlignment(Element.ALIGN_CENTER);
struts.setVerticalAlignment(Element.ALIGN_MIDDLE);
struts.setFixedHeight(35);
table.addCell(struts);
}
}
}
package com.declare.utils.print;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpSession;
import com.declare.pageModel.base.SessionInfo;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class NewPdfPagehe extends PdfPageEventHelper {
public static PdfTemplate tpl;
public static BaseFont bfChinese = null;
@Override
public void onEndPage(PdfWriter writer, Document document){
//拿到国际化信息
Locale local=ResourceUtil.getlocaleinfo();
SessionInfo session=ResourceUtil.getSessionInfo();
String customname=session.getCustomer().getName();
//设置页眉
PdfContentByte headAndFootPdfContent = writer.getDirectContent();
headAndFootPdfContent.saveState();
headAndFootPdfContent.beginText();
//设置中文
headAndFootPdfContent.setFontAndSize(bfChinese,11);
//文档页头信息设置
float x = document.top(-20);
float x1 = document.top(-60);
//页头信息左面
headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_LEFT,customname,document.left() + 20, x1, 0);
String localtitle="";
if(local.getLanguage().equals("hk")){
ResourceBundle bundle = java.util.ResourceBundle.getBundle("message_hk_CN");
localtitle=bundle.getString("lamp_dytitle");
}else if(local.getLanguage().equals("zh")){
ResourceBundle bundle = java.util.ResourceBundle.getBundle("message_zh_CN");
localtitle=bundle.getString("lamp_dytitle");
}else{
ResourceBundle bundle = java.util.ResourceBundle.getBundle("message_en_US");
localtitle=bundle.getString("lamp_dytitle");
}
float x2 = document.top(-30);
headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER,localtitle,(document.left() + document.right())/2, x2, 0);
PdfContentByte cb=writer.getDirectContent();
String text="Page "+writer.getPageNumber()+" of " ;
if(local.getLanguage().equals("hk")){
text="第 "+writer.getPageNumber()+" 頁 共 " ;
}else if(local.getLanguage().equals("zh")){
text="第 "+writer.getPageNumber()+" 页 共 " ;
}else{
text="Page "+writer.getPageNumber()+" of " ;
}
float textSizt=bfChinese.getWidthPoint(text,12);
float textbase=document.bottom()-10;
cb.beginText();
cb.setFontAndSize(bfChinese,12);
cb.setTextMatrix((document.left()+document.right())/2, textbase);
cb.showText(text);
cb.addTemplate(tpl,(document.left()+document.right())/2+textSizt, textbase);
}
@Override
public void onOpenDocument(PdfWriter writer, Document document){
tpl=writer.getDirectContent().createTemplate(100,100);
try {
try {
Locale local=ResourceUtil.getlocaleinfo();
if(local.getLanguage().equals("hk")){
bfChinese = BaseFont.createFont("D:\\apache-tomcat-7.0.52\\webapps\\yidsys\\pdfFile\\msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
}else if(local.getLanguage().equals("zh")){
bfChinese = BaseFont.createFont("D:\\apache-tomcat-7.0.52\\webapps\\yidsys\\pdfFile\\msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
}else{
bfChinese = BaseFont.createFont("D:\\apache-tomcat-7.0.52\\webapps\\yidsys\\pdfFile\\msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
}
} catch (DocumentException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onCloseDocument(PdfWriter writer, Document document){
//拿到国际化信息
Locale local=ResourceUtil.getlocaleinfo();
tpl.beginText();
tpl.setFontAndSize(bfChinese, 12);
tpl.setTextMatrix(0,0);
String text=""+(writer.getPageNumber()-1);
if(local.getLanguage().equals("hk")){
text=""+(writer.getPageNumber()-1)+"頁";
}else if(local.getLanguage().equals("zh")){
text=""+(writer.getPageNumber()-1)+"页";
}else{
text=""+(writer.getPageNumber()-1);
}
tpl.showText(text);
tpl.endText();
}
}
最后效果图: