itext实现pdf导出
2016-08-31 17:05
399 查看
jar
iText-2.1.4.jariTextAsian.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
相关文章推荐
- LISTVIEW
- CSS的选择器
- MultiValueDictKeyError at /add/27/34/
- JSTL标签常用标签
- POJ 1742 Coins(多重背包变型,楼教主男人八题)
- 《PCL点云库学习&VS2010(X64)》Part 18 PCL1.72(VTK6.2.0)编译CloudCompare注意点
- 链表逆序
- 深入浅出“冒泡排序”
- postman-Newman的安装及使用
- JAVA常见知识点总结
- Servlet 2.3过滤器编程
- 理解ThreadLocal(之二)
- 理解ThreadLocal(之一)
- CSS的三种样式
- App开发流程之增加预编译头文件
- openfire集群
- 数据库为什么要分库分表
- 这些Tips让你的App更容易维护
- There is no getter for property named 'containerId' in 'class java.lang.String'
- Word 2013中多级标题自动编号方法