将数据以表格的形式保存到pdf中
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                将数据以表格的形式保存到pdf中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            private void ExpResPdf(Map<String, String> map) throws DocumentException {if (map == null) {map = new HashMap<String, String>();}// 生成文件的路徑String outPdf = Constant.MUBAN + "救護記錄單.pdf";// 獲取模板的路徑BaseFont bfChinese = null;try {// 對顯示的語言進行初始化,設置樣式// bfChinese = BaseFont.createFont("Helvetica", "Ansi",// BaseFont.NOT_EMBEDDED);// String path = FileUtils.getAssetFileContext(this,// "STFANGSO.TTF");String path = "file:///android_asset/Helvetica.Ttf";bfChinese = BaseFont.createFont(path, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);Font font8 = FontFactory.getFont(FontFactory.HELVETICA, 8);// 設置標題的語言、大小、字體樣式Font titleFont = new Font(bfChinese, 16, Font.BOLD);// 標題// 設置表格標題的語言、大小、字體樣式Font tableTitleFont = new Font(bfChinese, 12, Font.BOLD);// 表格標題// 設置內容的語言、大小、字體樣式Font conyentFont = new Font(bfChinese, 12, Font.NORMAL);// 內容// 創建document對象Document document = new Document(PageSize.A4, 50, 50, 50, 50);// 獲取操作pdf的實例(參數是document對象和文件輸出流)try {PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outPdf));} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 實例化// 單元格PdfPCell cell = null;// 段落Paragraph tableTitle = null;// 打開之前設置參數HeaderFooter footer = new HeaderFooter(new Phrase("接診人簽名:                       接診醫師簽名:                 ",conyentFont), false);document.setFooter(footer);document.open();// 添加標題Paragraph par = new Paragraph("云南省急救中心院前急救病例", tableTitleFont);par.setAlignment(Rectangle.ALIGN_CENTER);// par.setFirstLineIndent(15f);// 首行縮進// par.setSpacingBefore(15f);// 上留白par.setSpacingAfter(15f);// 上留白document.add(par);StringBuffer stringBuffer = new StringBuffer();String str = stringBuffer.append("站名: ").append(map.get("stationname")).append("   ").append("急救員救護性質: ").append(map.get("rescue_property")).append("   ").append("病案號: 暫無").toString();Paragraph zhanming = new Paragraph(str, conyentFont);zhanming.setSpacingAfter(15f);// 上留白document.add(zhanming);// 添加表格int myCol = 3;// 創建表格對象,設置其為3列PdfPTable myTable = new PdfPTable(myCol);myTable.setHorizontalAlignment(Element.ALIGN_LEFT);myTable.setTotalWidth(500f);myTable.setWidths(new float[] { 0.1f, 0.1f, 0.2f });myTable.setWidthPercentage(100);myTable.setLockedWidth(true);// 第一行StringBuffer stringBuffer2 = new StringBuffer();String strTableTitle = stringBuffer2.append("姓名: ").append(map.get("patient_name")).append("   ").append("性別: ").append(map.get("patient_sex")).append("   ").append("年齡: ").append(map.get("patient_age")).append("   ").append("單位: ").append(map.get("certificate_type")).append("   ").append("婚姻: ").append(map.get("patient_marry")).append("   ").toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列// 將創建的這一列放到表格中myTable.addCell(cell);// 第二行StringBuffer stringBuffer3 = new StringBuffer();strTableTitle = stringBuffer3.append("職業: ").append(map.get("patient_job")).append("   ").append("民族: ").append(map.get("patient_nation")).append("   ").append("國籍: ").append(map.get("patient_nationality")).append("   ").append("電話: ").append(map.get("patient_phone")).append("   ").append("住址或單位: ").append(map.get("patient_address")).append("   ").toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列// 將創建的這一列放到表格中myTable.addCell(cell);// 第三行StringBuffer stringBuffer4 = new StringBuffer();strTableTitle = stringBuffer4.append("出診時間: ").append(map.get("out_patient_time")).append("   ").append("到達現場時間: ").append(map.get("arrive_spot_time")).append("   ").append("現場地址: ").append(map.get("spot_address")).append("   ").toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列// 將創建的這一列放到表格中myTable.addCell(cell);// 第四行StringBuffer stringBuffer5 = new StringBuffer();strTableTitle = stringBuffer5.append("病史提供者: ").append(map.get("offer_history_person")).append("   ").append(map.get("patient_relation")).append("   ").append(map.get("patient_relation_other")).append("   ").toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列// 將創建的這一列放到表格中myTable.addCell(cell);// 第五行StringBuffer stringBuffer6 = new StringBuffer();strTableTitle = stringBuffer6.append("既往史: ").append(map.get("past_history")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(2);// 跨2列myTable.addCell(cell);// 第五行StringBuffer stringBuffer7 = new StringBuffer();strTableTitle = stringBuffer7.append("藥物過敏史: ").append(map.get("irritability_history")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中// cell.setColspan(2);// 跨2列myTable.addCell(cell);// 第六行StringBuffer stringBuffer8 = new StringBuffer();strTableTitle = stringBuffer8.append("主要體征: ").append("1.意識: ").append(map.get("main_awareness")).append("   ").append("2.T: ").append(map.get("physical_examination_temperatu")).append("℃").append("   ").append("P: ").append(map.get("physical_examination_pulse")).append("次/分").append("   ").append("R: ").append(map.get("physical_examination_heart")).append("次/分").append("   ").append("Bp: 收縮壓: ").append(map.get("physical_examination_bp_high")).append("mmHg").append("   ").append("舒張壓: ").append(map.get("physical_examination_bp_low")).append("mmHg").append("   ").append("3.其他: ").append(map.get("main_other_signs")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列myTable.addCell(cell);// 第七行StringBuffer stringBuffer9 = new StringBuffer();strTableTitle = stringBuffer9.append("患者主要情況: ").append(map.get("main_condition")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列myTable.addCell(cell);// 第七行 第一列strTableTitle = "輔助檢查";tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中myTable.addCell(cell);// 第七行 第二三列StringBuffer stringBuffer10 = new StringBuffer();strTableTitle = stringBuffer10.append("1.血氧飽和度: ").append(map.get("oxyhemoglobin_saturation")).append("%").append("   ").append("2.血糖: ").append(map.get("blood_glucose")).append("mmlo/h").append("   ").append("3.其他: ").append(map.get("main_other_signs")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(2);// 跨2列myTable.addCell(cell);// 第八行 第一列strTableTitle = "原診斷/初步印象";tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中myTable.addCell(cell);// 第八行 第二三列StringBuffer stringBuffer11 = new StringBuffer();strTableTitle = stringBuffer11.append(map.get("first_impression")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(2);// 跨2列myTable.addCell(cell);// 第九行 第一列strTableTitle = "病情判斷";tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中myTable.addCell(cell);// 第九行 第二三列StringBuffer stringBuffer12 = new StringBuffer();strTableTitle = stringBuffer12.append(map.get("illness_judge")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(2);// 跨2列myTable.addCell(cell);// 第十行StringBuffer stringBuffer13 = new StringBuffer();strTableTitle = stringBuffer13.append("救護措施: ").append(map.get("rescue_measure")).append("   ").append(map.get("nurse_usematerial")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列myTable.addCell(cell);// 第十一行StringBuffer stringBuffer14 = new StringBuffer();strTableTitle = stringBuffer14.append("已告知患者/家屬在現場救治、搬運和轉送途中患者隨時存在病情變化甚至惡化的可能,患方同意并要求送往 ").append(map.get("return_hospital")).append("醫院/家中。已知曉患者情況我同意轉送。").append("\n").append("患者或家屬簽名:              ").append("與患者關系: ").append(map.get("patient_relations")).append("      ").append("聯系電話: ").append(map.get("patient_tel")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列myTable.addCell(cell);// 第十二行StringBuffer stringBuffer15 = new StringBuffer();strTableTitle = stringBuffer15.append("送達地點: ").append(map.get("detaildelivery_place")).append(" ").append(map.get("send_hospital")).append(" ").append(map.get("office")).append("   ").append("送達時間: ").append(map.get("send_arrival_time")).toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列myTable.addCell(cell);// 第十三行StringBuffer stringBuffer16 = new StringBuffer();strTableTitle = stringBuffer16.append("記錄單完成時間: ").append(map.get("record_endtime")).append(" ").toString();tableTitle = new Paragraph(strTableTitle, conyentFont);cell = new PdfPCell(tableTitle);// cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中cell.setColspan(3);// 跨3列myTable.addCell(cell);document.add(myTable);document.close();}
                            
                        
                        
                        總結
以上是生活随笔為你收集整理的将数据以表格的形式保存到pdf中的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 模板类的声明和定义
 - 下一篇: 2.5 随从受伤溅血效果的实现———自制