您的位置:首页 > 其它

itext实现pdf导出

2016-08-31 17:05 399 查看

jar

iText-2.1.4.jar

iTextAsian.jar 中文必备,否则不显示

程序员的一贯风格不多说,上代码:

public class OrderData2PDFServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private WebApplicationContext wac;
public OrderData2PDFServlet() {
super();
}

@Override
public void init() throws ServletException {
wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
}
/*
* <p>生成批PDF document 并支持下载</p>
*
* @author Z.J
*
* @date 2016-8-2
*
* @param request
*
* @param response
*
* @throws ServletException
*
* @throws IOException
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// No.1 处理分页查询参数
Paginator page = new Paginator();
CommonUtils.setParametersForPage(page, request);
int  columnCount=6;
float[] columnRatio=new float[] { 2, 2, 1, 1, 2, 2 };
//pdf表格导出的title
String[] titleArr={"Paypal交易单号","客户姓名","交易日期","总金额","国家","回访次数"};
String header="订单信息";
OrderService orderService=(OrderService)wac.getBean("orderService");
//获取订单信息
List<Order> resultList =orderService.getOrderListByPage(page);
//创建文档对象,并设置大小,本案例设为A4纸尺寸
Document document = new Document(PageSize.A4.rotate());
try{
// 设置响应输出数据的字符集
response.setContentType("application/x-msdownload;charset=UTF-8");
// 激活浏览器文件下载框,提示用户保存
response.setHeader("Content-Disposition", "attachment;filename=order_info.pdf");
// open output stream
PdfWriter.getInstance(document, response.getOutputStream());
// open PDF document
document.open();
//字体处理,中文需要用到
FontSelector selector = new FontSelector();
//设置英文显示字体TIMES_ROMAN及大小
selector.addFont(FontFactory.getFont(FontFactory.TIMES_ROMAN, 10));
//设置中文显示字体STSong-Light
Font cf1 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 10);
//设置标题显示字体STSong-Light及大小
Font headerFont = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 18);
selector.addFont(cf1);
//创建pdf表格并填充表格内容
if (resultList != null && resultList.size() > 0) {
PdfPTable table = new PdfPTable(columnRatio);
PdfPCell topHeaderCell = new PdfPCell(new Phrase(header,headerFont));
topHeaderCell.setColspan(columnCount);
topHeaderCell.setMinimumHeight(35);
topHeaderCell.setBackgroundColor(new Color(189, 215, 238));
topHeaderCell.setHorizontalAlignment(Element.ALIGN_CENTER);
topHeaderCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(topHeaderCell);

PdfPCell defaultCell = table.getDefaultCell();
defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
defaultCell.setMinimumHeight(30);
defaultCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPTableEvent event = new AlternatingBackground();
for (int i = 0; i < 2; i++) {
for(String s:titleArr){
table.addCell(selector.process(s));
}
}
table.setHeaderRows(3);
table.setFooterRows(1);
defaultCell.setMinimumHeight(25);
for (Order o : resultList) {
table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getPaypalNumber())));
CustomerModel cu=o.getCustomer();
if(cu!=null&&CommonUtils.isNotNullAndEmpty(cu.getCustomerName())){
table.addCell(selector.process(CommonUtils.processNullAndEmpty(cu.getCustomerName())));
}else{
table.addCell(selector.process(""));

}

table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getTradeDate())));
table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getOrderTotal())));
if(cu!=null&&CommonUtils.isNotNullAndEmpty(cu.getCustomerName())){
Country ct=cu.getCountry();
if(ct!=null&&CommonUtils.isNotNullAndEmpty(ct.getCountryName())){
table.addCell(selector.process(ct.getCountryName()));
}else{
table.addCell(selector.process(""));
}
}else{
table.addCell(selector.process(""));
}

table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getCallbackTimes())));
}
table.setTableEvent(event);
document.add(table);
}else{
Font f1 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 28);
f1.setColor(new GrayColor(0.75f));
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("暂无数据",f1));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(0);
cell.setMinimumHeight(30);
cell.setColspan(columnCount);
table.addCell(cell);
document.add(table);
}

}catch(Exception e){
e.printStackTrace();
}finally{
document.close();
}
}
/*
* <p></p>
*
* @author Z.J
*
* @date 2016-8-2
*
* @param request
*
* @param response
*
* @throws ServletException
*
* @throws IOException
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}

}


图简单的朋友们,可以直接把本代码给一下就能用,要是想深究的话,请仔细阅读itext的使用API。本案例使用FontSelector实现了中英文字体混合显示,程序会自动根据当前要显示的文字自行切换字体。

jar包和itext使用API的下载地址如下:http://download.csdn.net/detail/chrisjingu/9618083
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: