java代码生成PDF文件-表格和图片

青旅半醒 2022-09-26 01:49 414阅读 0赞

jar:iText-2.1.4.jar,iTextAsian.jar

Java代码 复制代码 收藏代码spinner.gif

  1. package com.zjhcsoft.sim.util;
  2. import java.awt.Color;
  3. import java.io.ByteArrayOutputStream;
  4. import java.util.List;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.log4j.Logger;
  9. import com.lowagie.text.DocumentException;
  10. import com.lowagie.text.Element;
  11. import com.lowagie.text.Font;
  12. import com.lowagie.text.Image;
  13. import com.lowagie.text.PageSize;
  14. import com.lowagie.text.Paragraph;
  15. import com.lowagie.text.pdf.BaseFont;
  16. import com.lowagie.text.pdf.PdfPCell;
  17. import com.lowagie.text.pdf.PdfPTable;
  18. import com.lowagie.text.pdf.PdfWriter;
  19. import com.zjhcsoft.sim.device.bean.DeviceItems;
  20. import com.zjhcsoft.sim.si.bean.SiPollrpt;
  21. /***
  22. *

    Title:

  23. *
  24. *

    Description:

  25. *
  26. *

    Author:wangcheng

  27. *
  28. *

    Copyright: Copyright (c) 2013

  29. *
  30. *

    Company: 浙江鸿程

  31. *
  32. */
  33. public class PollRptPdf {
  34. Logger logger = Logger.getLogger(PollRptPdf.class);
  35. public void downPdf( HttpServletRequest request,
  36. HttpServletResponse response,SiPollrpt siPollrpt,String filePath){
  37. try {
  38. List deviceList = siPollrpt.getDeviceItemList();
  39. // 创建一个Document对象
  40. com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 36, 36, 36, 36);
  41. ByteArrayOutputStream ba = new ByteArrayOutputStream();
  42. //设置宋体
  43. BaseFont bfChinese = BaseFont.createFont(“STSongStd-Light”, “UniGB-UCS2-H”, true);
  44. Font fontChinese =new Font(bfChinese,15,Font.BOLD,Color.BLACK);
  45. Font normalfont =new Font(bfChinese,13,Font.NORMAL,Color.BLACK);
  46. BaseFont bfChinese2 = BaseFont.createFont(“STSong-Light”,”UniGB-UCS2-H”,BaseFont.NOT_EMBEDDED);//设置中文字体
  47. Font headFont2 = new Font(bfChinese2, 13, Font.NORMAL);//设置字体大小
  48. try {
  49. //添加一些信息
  50. document.addTitle(“平台巡检报告”);
  51. PdfWriter writer = PdfWriter.getInstance(document, ba);
  52. document.open();
  53. Paragraph paragraphTitle1 = new Paragraph(“平台巡检报告—(“+siPollrpt.getAPPNAME()+”)\n”,fontChinese);
  54. paragraphTitle1.setAlignment(1);//1.中间,2.右边
  55. Paragraph paragraphTitle2 = new Paragraph(“操作员:”+siPollrpt.getUSERNAME()+” (“+siPollrpt.getYEAR()+”年”+siPollrpt.getQUARTERNAME()+”)\n”,normalfont);
  56. paragraphTitle2.setAlignment(2);//1.中间,2.右边
  57. StringBuffer content1 = new StringBuffer(“\n 1.设备运行情况检查总结”);
  58. content1.append(“\n\t”).append(siPollrpt.getRPTDESC());
  59. StringBuffer content2 = new StringBuffer(“\n 2.系统网络结构\n\t”);
  60. Image image = Image.getInstance(request.getRealPath(“\\“) + siPollrpt.getFILEPATH());
  61. StringBuffer content3 = new StringBuffer(“\n 3.设备具体检查情况”);
  62. Paragraph paragraphContent1 = new Paragraph(content1.toString(),normalfont);
  63. Paragraph paragraphContent2 = new Paragraph(content2.toString(),normalfont);
  64. Paragraph paragraphContent3 = new Paragraph(content3.toString(),normalfont);
  65. document.add(paragraphTitle1);
  66. document.add(paragraphTitle2);
  67. document.add(paragraphContent1);
  68. document.add(paragraphContent2);
  69. document.add(image);
  70. document.add(paragraphContent3);
  71. DeviceItems items = null;
  72. for(int i=0;i<deviceList.size();i++){
  73. float[] widths = {70f, 180f, 500f};//设置表格的列宽
  74. PdfPTable table = new PdfPTable(widths);
  75. table.setTotalWidth(750);//设置表格的宽度
  76. table.setSpacingBefore(40f);//设置表格上面空白宽度
  77. items = (DeviceItems)deviceList.get(i);
  78. PdfPCell cellHType = new PdfPCell(new Paragraph(items.getSERVERTYPENAME(),headFont2));
  79. cellHType.setColspan(3);
  80. table.addCell(cellHType);
  81. PdfPCell cellH = new PdfPCell(new Paragraph(“编号”,headFont2));
  82. cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
  83. table.addCell(cellH);
  84. cellH= new PdfPCell(new Paragraph(“检查项目”,headFont2));
  85. cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
  86. table.addCell(cellH);
  87. cellH = new PdfPCell(new Paragraph(“检查结果”,headFont2));
  88. cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
  89. table.addCell(cellH);
  90. PdfPCell cellContent = null;
  91. for(int j=1;j<5;j++){
  92. if(j==1){
  93. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  94. table.addCell(cellContent);
  95. cellContent = new PdfPCell(new Paragraph(“CPU占用率检查”,headFont2));
  96. table.addCell(cellContent);
  97. cellContent = new PdfPCell(new Paragraph(items.getCPU(),headFont2));
  98. table.addCell(cellContent);
  99. }else if(j==2){
  100. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  101. table.addCell(cellContent);
  102. cellContent = new PdfPCell(new Paragraph(“内存检查”,headFont2));
  103. table.addCell(cellContent);
  104. cellContent = new PdfPCell(new Paragraph(items.getMEM(),headFont2));
  105. table.addCell(cellContent);
  106. }else if(j==3){
  107. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  108. table.addCell(cellContent);
  109. cellContent = new PdfPCell(new Paragraph(“本地硬盘容量检查”,headFont2));
  110. table.addCell(cellContent);
  111. cellContent = new PdfPCell(new Paragraph(items.getDISK(),headFont2));
  112. table.addCell(cellContent);
  113. }else if(j==4){
  114. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  115. table.addCell(cellContent);
  116. cellContent = new PdfPCell(new Paragraph(“进程检查”,headFont2));
  117. table.addCell(cellContent);
  118. cellContent = new PdfPCell(new Paragraph(items.getPROCESS(),headFont2));
  119. table.addCell(cellContent);
  120. }
  121. }
  122. document.add(table);
  123. }
  124. } catch (DocumentException de) {
  125. de.printStackTrace();
  126. }
  127. document.close();
  128. response.setHeader(“Content-disposition”, “attachment; filename=” + new String(filePath.getBytes(“GBK”), “iso8859-1”));
  129. response.setContentType(“application/pdf”);
  130. response.setContentLength(ba.size());
  131. ServletOutputStream out = response.getOutputStream();
  132. ba.writeTo(out);
  133. out.flush();
  134. out.close();
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. }

[java] view plain copy

  1. package com.zjhcsoft.sim.util;
  2. import java.awt.Color;
  3. import java.io.ByteArrayOutputStream;
  4. import java.util.List;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.log4j.Logger;
  9. import com.lowagie.text.DocumentException;
  10. import com.lowagie.text.Element;
  11. import com.lowagie.text.Font;
  12. import com.lowagie.text.Image;
  13. import com.lowagie.text.PageSize;
  14. import com.lowagie.text.Paragraph;
  15. import com.lowagie.text.pdf.BaseFont;
  16. import com.lowagie.text.pdf.PdfPCell;
  17. import com.lowagie.text.pdf.PdfPTable;
  18. import com.lowagie.text.pdf.PdfWriter;
  19. import com.zjhcsoft.sim.device.bean.DeviceItems;
  20. import com.zjhcsoft.sim.si.bean.SiPollrpt;
  21. /***
  22. *

    Title:

  23. *
  24. *

    Description:

  25. *
  26. *

    Author:wangcheng

  27. *
  28. *

    Copyright: Copyright (c) 2013

  29. *
  30. *

    Company: 浙江鸿程

  31. *
  32. */
  33. public class PollRptPdf {
  34. Logger logger = Logger.getLogger(PollRptPdf.class);
  35. public void downPdf( HttpServletRequest request,
  36. HttpServletResponse response,SiPollrpt siPollrpt,String filePath){
  37. try {
  38. List deviceList = siPollrpt.getDeviceItemList();
  39. // 创建一个Document对象
  40. com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 36, 36, 36, 36);
  41. ByteArrayOutputStream ba = new ByteArrayOutputStream();
  42. //设置宋体
  43. BaseFont bfChinese = BaseFont.createFont(“STSongStd-Light”, “UniGB-UCS2-H”, true);
  44. Font fontChinese =new Font(bfChinese,15,Font.BOLD,Color.BLACK);
  45. Font normalfont =new Font(bfChinese,13,Font.NORMAL,Color.BLACK);
  46. BaseFont bfChinese2 = BaseFont.createFont(“STSong-Light”,”UniGB-UCS2-H”,BaseFont.NOT_EMBEDDED);//设置中文字体
  47. Font headFont2 = new Font(bfChinese2, 13, Font.NORMAL);//设置字体大小
  48. try {
  49. //添加一些信息
  50. document.addTitle(“平台巡检报告”);
  51. PdfWriter writer = PdfWriter.getInstance(document, ba);
  52. document.open();
  53. Paragraph paragraphTitle1 = new Paragraph(“平台巡检报告—(“+siPollrpt.getAPPNAME()+”)\n”,fontChinese);
  54. paragraphTitle1.setAlignment(1);//1.中间,2.右边
  55. Paragraph paragraphTitle2 = new Paragraph(“操作员:”+siPollrpt.getUSERNAME()+” (“+siPollrpt.getYEAR()+”年”+siPollrpt.getQUARTERNAME()+”)\n”,normalfont);
  56. paragraphTitle2.setAlignment(2);//1.中间,2.右边
  57. StringBuffer content1 = new StringBuffer(“\n 1.设备运行情况检查总结”);
  58. content1.append(“\n\t”).append(siPollrpt.getRPTDESC());
  59. StringBuffer content2 = new StringBuffer(“\n 2.系统网络结构\n\t”);
  60. Image image = Image.getInstance(request.getRealPath(“\\“) + siPollrpt.getFILEPATH());
  61. StringBuffer content3 = new StringBuffer(“\n 3.设备具体检查情况”);
  62. Paragraph paragraphContent1 = new Paragraph(content1.toString(),normalfont);
  63. Paragraph paragraphContent2 = new Paragraph(content2.toString(),normalfont);
  64. Paragraph paragraphContent3 = new Paragraph(content3.toString(),normalfont);
  65. document.add(paragraphTitle1);
  66. document.add(paragraphTitle2);
  67. document.add(paragraphContent1);
  68. document.add(paragraphContent2);
  69. document.add(image);
  70. document.add(paragraphContent3);
  71. DeviceItems items = null;
  72. for(int i=0;i<deviceList.size();i++){
  73. float[] widths = {70f, 180f, 500f};//设置表格的列宽
  74. PdfPTable table = new PdfPTable(widths);
  75. table.setTotalWidth(750);//设置表格的宽度
  76. table.setSpacingBefore(40f);//设置表格上面空白宽度
  77. items = (DeviceItems)deviceList.get(i);
  78. PdfPCell cellHType = new PdfPCell(new Paragraph(items.getSERVERTYPENAME(),headFont2));
  79. cellHType.setColspan(3);
  80. table.addCell(cellHType);
  81. PdfPCell cellH = new PdfPCell(new Paragraph(“编号”,headFont2));
  82. cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
  83. table.addCell(cellH);
  84. cellH= new PdfPCell(new Paragraph(“检查项目”,headFont2));
  85. cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
  86. table.addCell(cellH);
  87. cellH = new PdfPCell(new Paragraph(“检查结果”,headFont2));
  88. cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
  89. table.addCell(cellH);
  90. PdfPCell cellContent = null;
  91. for(int j=1;j<5;j++){
  92. if(j==1){
  93. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  94. table.addCell(cellContent);
  95. cellContent = new PdfPCell(new Paragraph(“CPU占用率检查”,headFont2));
  96. table.addCell(cellContent);
  97. cellContent = new PdfPCell(new Paragraph(items.getCPU(),headFont2));
  98. table.addCell(cellContent);
  99. }else if(j==2){
  100. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  101. table.addCell(cellContent);
  102. cellContent = new PdfPCell(new Paragraph(“内存检查”,headFont2));
  103. table.addCell(cellContent);
  104. cellContent = new PdfPCell(new Paragraph(items.getMEM(),headFont2));
  105. table.addCell(cellContent);
  106. }else if(j==3){
  107. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  108. table.addCell(cellContent);
  109. cellContent = new PdfPCell(new Paragraph(“本地硬盘容量检查”,headFont2));
  110. table.addCell(cellContent);
  111. cellContent = new PdfPCell(new Paragraph(items.getDISK(),headFont2));
  112. table.addCell(cellContent);
  113. }else if(j==4){
  114. cellContent = new PdfPCell(new Paragraph((i+1)+”.”+j,headFont2));
  115. table.addCell(cellContent);
  116. cellContent = new PdfPCell(new Paragraph(“进程检查”,headFont2));
  117. table.addCell(cellContent);
  118. cellContent = new PdfPCell(new Paragraph(items.getPROCESS(),headFont2));
  119. table.addCell(cellContent);
  120. }
  121. }
  122. document.add(table);
  123. }
  124. } catch (DocumentException de) {
  125. de.printStackTrace();
  126. }
  127. document.close();
  128. response.setHeader(“Content-disposition”, “attachment; filename=” + new String(filePath.getBytes(“GBK”), “iso8859-1”));
  129. response.setContentType(“application/pdf”);
  130. response.setContentLength(ba.size());
  131. ServletOutputStream out = response.getOutputStream();
  132. ba.writeTo(out);
  133. out.flush();
  134. out.close();
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. }

发表评论

表情:
评论列表 (有 0 条评论,414人围观)

还没有评论,来说两句吧...

相关阅读