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

jar:iText-2.1.4.jar,iTextAsian.jar 
Java代码  复制代码  收藏代码
  1. package com.zjhcsoft.sim.util;     
  2.   
  3. import java.awt.Color;   
  4. import java.io.ByteArrayOutputStream;   
  5. import java.util.List;   
  6.   
  7. import javax.servlet.ServletOutputStream;   
  8. import javax.servlet.http.HttpServletRequest;   
  9. import javax.servlet.http.HttpServletResponse;   
  10.   
  11. import org.apache.log4j.Logger;   
  12.   
  13. import com.lowagie.text.DocumentException;   
  14. import com.lowagie.text.Element;   
  15. import com.lowagie.text.Font;   
  16. import com.lowagie.text.Image;   
  17. import com.lowagie.text.PageSize;   
  18. import com.lowagie.text.Paragraph;   
  19. import com.lowagie.text.pdf.BaseFont;   
  20. import com.lowagie.text.pdf.PdfPCell;   
  21. import com.lowagie.text.pdf.PdfPTable;   
  22. import com.lowagie.text.pdf.PdfWriter;   
  23. import com.zjhcsoft.sim.device.bean.DeviceItems;   
  24. import com.zjhcsoft.sim.si.bean.SiPollrpt;   
  25.   
  26. /***  
  27.  * <p>Title: </p>   
  28.  *  
  29.  * <p>Description: </p>   
  30.  *  
  31.  * <p>Author:wangcheng </p>   
  32.  *  
  33.  * <p>Copyright: Copyright (c) 2013 </p>  
  34.  *   
  35.  * <p>Company: 浙江鸿程</p>  
  36.  *  
  37.  */  
  38. public class PollRptPdf {   
  39.     Logger logger = Logger.getLogger(PollRptPdf.class);   
  40.     public void downPdf( HttpServletRequest request,   
  41.             HttpServletResponse response,SiPollrpt siPollrpt,String filePath){   
  42.           try {   
  43.            List deviceList = siPollrpt.getDeviceItemList();   
  44.            // 创建一个Document对象   
  45.            com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 36363636);   
  46.            ByteArrayOutputStream ba = new ByteArrayOutputStream();   
  47.            //设置宋体   
  48.            BaseFont bfChinese = BaseFont.createFont("STSongStd-Light""UniGB-UCS2-H"true);    
  49.            Font fontChinese =new Font(bfChinese,15,Font.BOLD,Color.BLACK);   
  50.            Font normalfont =new Font(bfChinese,13,Font.NORMAL,Color.BLACK);   
  51.               
  52.            BaseFont bfChinese2 = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体   
  53.            Font headFont2 = new Font(bfChinese2, 13, Font.NORMAL);//设置字体大小    
  54.               
  55.            try {    
  56.             //添加一些信息   
  57.             document.addTitle("平台巡检报告");   
  58.             PdfWriter writer = PdfWriter.getInstance(document, ba);   
  59.             document.open();   
  60.             Paragraph paragraphTitle1 = new Paragraph("平台巡检报告--("+siPollrpt.getAPPNAME()+")\n",fontChinese);   
  61.             paragraphTitle1.setAlignment(1);//1.中间,2.右边   
  62.                
  63.             Paragraph paragraphTitle2 = new Paragraph("操作员:"+siPollrpt.getUSERNAME()+" ("+siPollrpt.getYEAR()+"年"+siPollrpt.getQUARTERNAME()+")\n",normalfont);   
  64.             paragraphTitle2.setAlignment(2);//1.中间,2.右边   
  65.                
  66.             StringBuffer content1 = new StringBuffer("\n 1.设备运行情况检查总结");   
  67.             content1.append("\n\t").append(siPollrpt.getRPTDESC());   
  68.                
  69.             StringBuffer content2 = new StringBuffer("\n 2.系统网络结构\n\t");   
  70.              Image image = Image.getInstance(request.getRealPath("\\") + siPollrpt.getFILEPATH());    
  71.             StringBuffer content3 = new StringBuffer("\n 3.设备具体检查情况");   
  72.                
  73.                
  74.             Paragraph paragraphContent1 = new Paragraph(content1.toString(),normalfont);   
  75.             Paragraph paragraphContent2 = new Paragraph(content2.toString(),normalfont);   
  76.             Paragraph paragraphContent3 = new Paragraph(content3.toString(),normalfont);   
  77.                
  78.             document.add(paragraphTitle1);   
  79.             document.add(paragraphTitle2);   
  80.                
  81.             document.add(paragraphContent1);   
  82.             document.add(paragraphContent2);   
  83.             document.add(image);   
  84.             document.add(paragraphContent3);   
  85.                
  86.                
  87.                
  88.             DeviceItems items  = null;   
  89.             for(int i=0;i<deviceList.size();i++){   
  90.                 float[] widths = {70f, 180f, 500f};//设置表格的列宽   
  91.                 PdfPTable table = new PdfPTable(widths);   
  92.                 table.setTotalWidth(750);//设置表格的宽度      
  93.                 table.setSpacingBefore(40f);//设置表格上面空白宽度   
  94.                    
  95.                 items = (DeviceItems)deviceList.get(i);   
  96.                 PdfPCell cellHType = new PdfPCell(new Paragraph(items.getSERVERTYPENAME(),headFont2));   
  97.                 cellHType.setColspan(3);   
  98.                 table.addCell(cellHType);   
  99.                    
  100.                 PdfPCell cellH = new PdfPCell(new Paragraph("编号",headFont2));   
  101.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);   
  102.                 table.addCell(cellH);   
  103.                 cellH= new PdfPCell(new Paragraph("检查项目",headFont2));   
  104.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);   
  105.                 table.addCell(cellH);   
  106.                 cellH = new PdfPCell(new Paragraph("检查结果",headFont2));   
  107.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);   
  108.                 table.addCell(cellH);   
  109.                    
  110.                 PdfPCell cellContent = null;   
  111.                 for(int j=1;j<5;j++){   
  112.                     if(j==1){   
  113.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  114.                         table.addCell(cellContent);   
  115.                         cellContent = new PdfPCell(new Paragraph("CPU占用率检查",headFont2));   
  116.                         table.addCell(cellContent);   
  117.                         cellContent = new PdfPCell(new Paragraph(items.getCPU(),headFont2));   
  118.                         table.addCell(cellContent);   
  119.                     }else if(j==2){   
  120.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  121.                         table.addCell(cellContent);   
  122.                         cellContent = new PdfPCell(new Paragraph("内存检查",headFont2));   
  123.                         table.addCell(cellContent);   
  124.                         cellContent = new PdfPCell(new Paragraph(items.getMEM(),headFont2));   
  125.                         table.addCell(cellContent);   
  126.                     }else if(j==3){   
  127.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  128.                         table.addCell(cellContent);   
  129.                         cellContent = new PdfPCell(new Paragraph("本地硬盘容量检查",headFont2));   
  130.                         table.addCell(cellContent);   
  131.                         cellContent = new PdfPCell(new Paragraph(items.getDISK(),headFont2));   
  132.                         table.addCell(cellContent);   
  133.                     }else if(j==4){   
  134.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  135.                         table.addCell(cellContent);   
  136.                         cellContent = new PdfPCell(new Paragraph("进程检查",headFont2));   
  137.                         table.addCell(cellContent);   
  138.                         cellContent = new PdfPCell(new Paragraph(items.getPROCESS(),headFont2));   
  139.                         table.addCell(cellContent);   
  140.                     }   
  141.                 }   
  142.                 document.add(table);   
  143.             }   
  144.                
  145.            } catch (DocumentException de) {   
  146.             de.printStackTrace();   
  147.            }   
  148.            document.close();   
  149.            response.setHeader("Content-disposition""attachment; filename=" + new String(filePath.getBytes("GBK"), "iso8859-1"));   
  150.            response.setContentType("application/pdf");   
  151.            response.setContentLength(ba.size());   
  152.            ServletOutputStream out = response.getOutputStream();   
  153.            ba.writeTo(out);   
  154.            out.flush();   
  155.            out.close();   
  156.           } catch (Exception e) {   
  157.            e.printStackTrace();   
  158.           }   
  159.     }   
  160.   
  161. }  
[java]  view plain  copy
  1. package com.zjhcsoft.sim.util;    
  2.   
  3. import java.awt.Color;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.util.List;  
  6.   
  7. import javax.servlet.ServletOutputStream;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import org.apache.log4j.Logger;  
  12.   
  13. import com.lowagie.text.DocumentException;  
  14. import com.lowagie.text.Element;  
  15. import com.lowagie.text.Font;  
  16. import com.lowagie.text.Image;  
  17. import com.lowagie.text.PageSize;  
  18. import com.lowagie.text.Paragraph;  
  19. import com.lowagie.text.pdf.BaseFont;  
  20. import com.lowagie.text.pdf.PdfPCell;  
  21. import com.lowagie.text.pdf.PdfPTable;  
  22. import com.lowagie.text.pdf.PdfWriter;  
  23. import com.zjhcsoft.sim.device.bean.DeviceItems;  
  24. import com.zjhcsoft.sim.si.bean.SiPollrpt;  
  25.   
  26. /*** 
  27.  * <p>Title: </p>  
  28.  * 
  29.  * <p>Description: </p>  
  30.  * 
  31.  * <p>Author:wangcheng </p>  
  32.  * 
  33.  * <p>Copyright: Copyright (c) 2013 </p> 
  34.  *  
  35.  * <p>Company: 浙江鸿程</p> 
  36.  * 
  37.  */  
  38. public class PollRptPdf {  
  39.     Logger logger = Logger.getLogger(PollRptPdf.class);  
  40.     public void downPdf( HttpServletRequest request,  
  41.             HttpServletResponse response,SiPollrpt siPollrpt,String filePath){  
  42.           try {  
  43.            List deviceList = siPollrpt.getDeviceItemList();  
  44.            // 创建一个Document对象  
  45.            com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 36363636);  
  46.            ByteArrayOutputStream ba = new ByteArrayOutputStream();  
  47.            //设置宋体  
  48.            BaseFont bfChinese = BaseFont.createFont("STSongStd-Light""UniGB-UCS2-H"true);   
  49.            Font fontChinese =new Font(bfChinese,15,Font.BOLD,Color.BLACK);  
  50.            Font normalfont =new Font(bfChinese,13,Font.NORMAL,Color.BLACK);  
  51.              
  52.            BaseFont bfChinese2 = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体  
  53.            Font headFont2 = new Font(bfChinese2, 13, Font.NORMAL);//设置字体大小   
  54.              
  55.            try {   
  56.             //添加一些信息  
  57.             document.addTitle("平台巡检报告");  
  58.             PdfWriter writer = PdfWriter.getInstance(document, ba);  
  59.             document.open();  
  60.             Paragraph paragraphTitle1 = new Paragraph("平台巡检报告--("+siPollrpt.getAPPNAME()+")\n",fontChinese);  
  61.             paragraphTitle1.setAlignment(1);//1.中间,2.右边  
  62.               
  63.             Paragraph paragraphTitle2 = new Paragraph("操作员:"+siPollrpt.getUSERNAME()+" ("+siPollrpt.getYEAR()+"年"+siPollrpt.getQUARTERNAME()+")\n",normalfont);  
  64.             paragraphTitle2.setAlignment(2);//1.中间,2.右边  
  65.               
  66.             StringBuffer content1 = new StringBuffer("\n 1.设备运行情况检查总结");  
  67.             content1.append("\n\t").append(siPollrpt.getRPTDESC());  
  68.               
  69.             StringBuffer content2 = new StringBuffer("\n 2.系统网络结构\n\t");  
  70.              Image image = Image.getInstance(request.getRealPath("\\") + siPollrpt.getFILEPATH());   
  71.             StringBuffer content3 = new StringBuffer("\n 3.设备具体检查情况");  
  72.               
  73.               
  74.             Paragraph paragraphContent1 = new Paragraph(content1.toString(),normalfont);  
  75.             Paragraph paragraphContent2 = new Paragraph(content2.toString(),normalfont);  
  76.             Paragraph paragraphContent3 = new Paragraph(content3.toString(),normalfont);  
  77.               
  78.             document.add(paragraphTitle1);  
  79.             document.add(paragraphTitle2);  
  80.               
  81.             document.add(paragraphContent1);  
  82.             document.add(paragraphContent2);  
  83.             document.add(image);  
  84.             document.add(paragraphContent3);  
  85.               
  86.               
  87.               
  88.             DeviceItems items  = null;  
  89.             for(int i=0;i<deviceList.size();i++){  
  90.                 float[] widths = {70f, 180f, 500f};//设置表格的列宽  
  91.                 PdfPTable table = new PdfPTable(widths);  
  92.                 table.setTotalWidth(750);//设置表格的宽度     
  93.                 table.setSpacingBefore(40f);//设置表格上面空白宽度  
  94.                   
  95.                 items = (DeviceItems)deviceList.get(i);  
  96.                 PdfPCell cellHType = new PdfPCell(new Paragraph(items.getSERVERTYPENAME(),headFont2));  
  97.                 cellHType.setColspan(3);  
  98.                 table.addCell(cellHType);  
  99.                   
  100.                 PdfPCell cellH = new PdfPCell(new Paragraph("编号",headFont2));  
  101.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);  
  102.                 table.addCell(cellH);  
  103.                 cellH= new PdfPCell(new Paragraph("检查项目",headFont2));  
  104.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);  
  105.                 table.addCell(cellH);  
  106.                 cellH = new PdfPCell(new Paragraph("检查结果",headFont2));  
  107.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);  
  108.                 table.addCell(cellH);  
  109.                   
  110.                 PdfPCell cellContent = null;  
  111.                 for(int j=1;j<5;j++){  
  112.                     if(j==1){  
  113.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));  
  114.                         table.addCell(cellContent);  
  115.                         cellContent = new PdfPCell(new Paragraph("CPU占用率检查",headFont2));  
  116.                         table.addCell(cellContent);  
  117.                         cellContent = new PdfPCell(new Paragraph(items.getCPU(),headFont2));  
  118.                         table.addCell(cellContent);  
  119.                     }else if(j==2){  
  120.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));  
  121.                         table.addCell(cellContent);  
  122.                         cellContent = new PdfPCell(new Paragraph("内存检查",headFont2));  
  123.                         table.addCell(cellContent);  
  124.                         cellContent = new PdfPCell(new Paragraph(items.getMEM(),headFont2));  
  125.                         table.addCell(cellContent);  
  126.                     }else if(j==3){  
  127.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));  
  128.                         table.addCell(cellContent);  
  129.                         cellContent = new PdfPCell(new Paragraph("本地硬盘容量检查",headFont2));  
  130.                         table.addCell(cellContent);  
  131.                         cellContent = new PdfPCell(new Paragraph(items.getDISK(),headFont2));  
  132.                         table.addCell(cellContent);  
  133.                     }else if(j==4){  
  134.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));  
  135.                         table.addCell(cellContent);  
  136.                         cellContent = new PdfPCell(new Paragraph("进程检查",headFont2));  
  137.                         table.addCell(cellContent);  
  138.                         cellContent = new PdfPCell(new Paragraph(items.getPROCESS(),headFont2));  
  139.                         table.addCell(cellContent);  
  140.                     }  
  141.                 }  
  142.                 document.add(table);  
  143.             }  
  144.               
  145.            } catch (DocumentException de) {  
  146.             de.printStackTrace();  
  147.            }  
  148.            document.close();  
  149.            response.setHeader("Content-disposition""attachment; filename=" + new String(filePath.getBytes("GBK"), "iso8859-1"));  
  150.            response.setContentType("application/pdf");  
  151.            response.setContentLength(ba.size());  
  152.            ServletOutputStream out = response.getOutputStream();  
  153.            ba.writeTo(out);  
  154.            out.flush();  
  155.            out.close();  
  156.           } catch (Exception e) {  
  157.            e.printStackTrace();  
  158.           }  
  159.     }  
  160.   
  161. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现将PDF表格内容生成Excel文档,需要使用一些Java库来实现。以下是可能的实现步骤: 1. 使用Apache PDFBox库来读取PDF文件并提取表格内容。PDFBox是一个开源Java库,可用于读取和操作PDF文件。您可以使用以下代码来提取表格内容: ``` PDDocument document = PDDocument.load(new File("example.pdf")); PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); PDFTextStripper tStripper = new PDFTextStripper(); String pdfFileInText = tStripper.getText(document); String[] lines = pdfFileInText.split("\\r?\\n"); for (String line : lines) { // process table content } document.close(); ``` 2. 解析表格内容并将其存储在Java对象中。这可以通过使用正则表达式来解析每行表格内容来实现。您可以使用以下代码来解析表格内容: ``` Pattern pattern = Pattern.compile("([^,]*),\\s*([^,]*),\\s*([^,]*)"); List<TableData> tableDataList = new ArrayList<>(); for (String line : lines) { Matcher matcher = pattern.matcher(line); if (matcher.find()) { TableData tableData = new TableData(); tableData.setColumn1(matcher.group(1)); tableData.setColumn2(matcher.group(2)); tableData.setColumn3(matcher.group(3)); tableDataList.add(tableData); } } ``` 3. 使用Apache POI库将表格内容写入Excel文档。POI是一个Java库,可用于创建、读取和操作Microsoft Office格式的文件。您可以使用以下代码将表格内容写入Excel文档: ``` Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Table Data"); int rownum = 0; for (TableData tableData : tableDataList) { Row row = sheet.createRow(rownum++); row.createCell(0).setCellValue(tableData.getColumn1()); row.createCell(1).setCellValue(tableData.getColumn2()); row.createCell(2).setCellValue(tableData.getColumn3()); } FileOutputStream out = new FileOutputStream(new File("table_data.xlsx")); workbook.write(out); out.close(); workbook.close(); ``` 最后,您需要将上述代码组合在一起,并处理任何可能的异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值