com.itextpdf itextpdf 5.2.0 com.itextpdf itext-asian 5.2.0
java代码
/** * 导出PDF文件 java项目 www.fhadmin.org * @param orkQ * @param request * @param response * @return */ @ResponseBody @RequestMapping(value = "exportPdf") public AjaxJson exportPdf(orkQ orkQ, HttpServletRequest request, HttpServletResponse response) throws Exception { AjaxJson j = new AjaxJson(); Page page = orkQService.findPage(new Page(request, response, -1), orkQ); List list = page.getList(); // 第一步,实例化一个document对象 Document document = new Document(new RectangleReadOnly(842F, 595F)); // 第二步,设置要到出的路径 // 第二步,设置要到出的路径 //FileOutputStream out = new FileOutputStream("D:/workbook111.pdf"); //如果是浏览器通过request请求需要在浏览器中输出则使用下面方式 OutputStream out = response.getOutputStream(); // 第三步,设置字符 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); Font fontZH = new Font(bfChinese, 12.0F, 0); // 第四步,将pdf文件输出到磁盘 PdfWriter writer = PdfWriter.getInstance(document, out); // 第五步,打开生成的pdf文件 document.open(); // 第六步,设置内容 String title = "IT事"; try { Paragraph elements = new Paragraph(title, fontZH); elements.setAlignment(Paragraph.TITLE); document.add(elements);// document.add(new Paragraph(new Chunk(title,fontZH).setLocalDestination(title))); document.add(new Paragraph("\n")); } catch (DocumentException e) { e.printStackTrace(); } // 添加图片 /*Image image = Image.getInstance("图片路径"); image.setAlignment(Image.ALIGN_CENTER); image.scalePercent(40); //依照比例缩放 //image.setAbsolutePosition(40,60); document.add(image);*/ // 创建table,注意这里的3是三列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列 //创建表格对象 PdfPTable table = new PdfPTable(3); PdfPCell cell = new PdfPCell(); Paragraph zhi = new Paragraph("事件概要",fontZH); cell.setPhrase(zhi); //单元格水平居中对齐 cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph("联系人",fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph("事件类型",fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); for (int i = 0;i zhi = new Paragraph(list.get(i).getItSummary(),fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph(list.get(i).getContacts(),fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph(list.get(i).getItType(),fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); } document.add(table); document.add(new Paragraph("\n")); // 第七步,关闭document document.close(); return j;}