pdfPCell中设置单元格 setRowspan()与setColspan(),setPadding(),setBorder()

 打印pdf格式:打印的表格如下:

pdfPCell中设置单元格 setRowspan()与setColspan(),setPadding(),setBorder()_第1张图片

代码如下:


	public Forward exportBasicInfo() throws Exception {
		try{
			String id = this.getRequest().getParameter("id";
			String personId = this.getRequest().getParameter("personId"));
			if(StringUtil.isNotEmpty(id)&&StringUtil.isNotEmpty(personId)){
			PersonGradeInfo gradeInfo = persInfoManager.getGradeInfoById(id,personId);
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			com.lowagie.text.Document pdfDoc = new com.lowagie.text.Document(com.lowagie.text.PageSize.A4, 20, 20, 20, 20);
			Font title = new Font(bfChinese, 20, Font.BOLD);
			Font cellTitle = new Font(bfChinese, 11, Font.BOLD);
			Font cellContent = new Font(bfChinese, 11, Font.NORMAL);
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			PdfWriter.getInstance(pdfDoc, baos);
			pdfDoc.open();

			Paragraph docTitle = new Paragraph("基 本 信 息 表\n", title);
			docTitle.setAlignment(Element.ALIGN_CENTER);
			docTitle.setSpacingBefore(20);
			pdfDoc.add(docTitle);
			
			int size = 35;
			int contentSize = 30;
			
			int widths[] = { 12,12, 25, 21, 25, 25, 25, 15, 15 };
			PdfPTable baseTable = new PdfPTable(9);
			baseTable.setWidthPercentage(100);
			baseTable.setSpacingBefore(10);
			baseTable.setWidths(widths);
	
			
			PdfPCell baseTableCell = new PdfPCell(new Paragraph("姓名", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(2);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getPersonName() + "", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("性别", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(sexMap.get(gradeInfo.getSex()) + "", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("出生年月", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			Date birthday = gradeInfo.getBirthday();
			if (null != birthday) {
				int year = DateUtil.getYear(birthday);
				int month = DateUtil.getMonth(birthday);
				baseTableCell = new PdfPCell(new Paragraph(year + "年" + month + "月", cellContent));
				baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
				baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
				baseTableCell.setColspan(1);
				baseTableCell.setFixedHeight(size);
				baseTable.addCell(baseTableCell);
			} else {
				baseTableCell = new PdfPCell(new Paragraph("", cellContent));
				baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
				baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
				baseTableCell.setColspan(1);
				baseTableCell.setFixedHeight(size);
				baseTable.addCell(baseTableCell);
			}
	        
			String url ="路径";
	        java.net.URL imgurl = new java.net.URL(url);
	        java.awt.Image awtImage = ImageIO.read(imgurl);

	        if(null!=awtImage){
				Image img = Image.getInstance(awtImage,null);
				img.scaleAbsolute((float) 210, (float) 300);
				baseTableCell = new PdfPCell(img,true);
				baseTableCell.setPadding(1);
				baseTableCell.setColspan(2);
				baseTableCell.setRowspan(3);
				baseTableCell.setFixedHeight(size*3);
				baseTable.addCell(baseTableCell);
			}else{
				baseTableCell = new PdfPCell(new Paragraph("相片", cellContent));
				baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
				baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
				baseTableCell.setColspan(2);
				baseTableCell.setRowspan(3);
				baseTableCell.setFixedHeight(size*3);
				baseTable.addCell(baseTableCell);
			}
			
			baseTableCell = new PdfPCell(new Paragraph("籍贯", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(2);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getNativePlace() + "", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);
		
			baseTableCell = new PdfPCell(new Paragraph("民族", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(nationMap.get(gradeInfo.getEthnic()) + "", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("政治面貌", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getPoliticalStatus() + "", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("标准工资", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(2);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph( "", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("身体状况", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getHealthCondition(), cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph("外语程度", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getForeignDegree(), cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(size);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("学习\n经历", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(contentSize*4);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getStudyExperience(), cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
			baseTableCell.setColspan(8);
			baseTableCell.setFixedHeight(contentSize*4);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("特长", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(contentSize*3);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getSpecialty(), cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
			baseTableCell.setColspan(8);
			baseTableCell.setFixedHeight(contentSize*3);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("奖罚", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setFixedHeight(contentSize*3);
			baseTable.addCell(baseTableCell);

			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getProbationReward(), cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
			baseTableCell.setColspan(8);
			baseTableCell.setFixedHeight(contentSize*3);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("见习\n工作", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(1);
			baseTableCell.setRowspan(3);
			baseTableCell.setFixedHeight(contentSize*10);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("时间", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(2);
			baseTableCell.setFixedHeight(contentSize);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("部门岗位", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(2);
			baseTableCell.setFixedHeight(contentSize);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph("主要工作内容", cellTitle));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(4);
			baseTableCell.setFixedHeight(contentSize);
			baseTable.addCell(baseTableCell);
			
			Date probationTime = gradeInfo.getProbationTime();
			if (null != probationTime) {
				int year = DateUtil.getYear(probationTime);
				int month = DateUtil.getMonth(probationTime);
				baseTableCell = new PdfPCell(new Paragraph(year + "年" + month + "月", cellContent));
				baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
				baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
				baseTableCell.setColspan(2);
				baseTableCell.setFixedHeight(size);
				baseTable.addCell(baseTableCell);
			} else {
				baseTableCell = new PdfPCell(new Paragraph("", cellContent));
				baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
				baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
				baseTableCell.setColspan(2);
				baseTableCell.setFixedHeight(size);
				baseTable.addCell(baseTableCell);
			}
			
			
			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getProbationDept()+"", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(2);
			baseTableCell.setFixedHeight(contentSize);
			baseTable.addCell(baseTableCell);
			
			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getProbationContent()+"", cellContent));
			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_MIDDLE);
			baseTableCell.setColspan(4);
			baseTableCell.setFixedHeight(contentSize);
			baseTable.addCell(baseTableCell);
			
//			baseTableCell = new PdfPCell(new Paragraph(gradeInfo.getProbationSumm()+"", cellContent));
//			baseTableCell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
//			baseTableCell.setVerticalAlignment(com.lowagie.text.Element.ALIGN_LEFT);
//			baseTableCell.setColspan(8);
//			baseTableCell.setFixedHeight(contentSize*8);
//			baseTable.addCell(baseTableCell);
			
			
			baseTableCell = new PdfPCell();
			Paragraph para = new Paragraph(gradeInfo.getProbationSumm()+"\n",cellContent);
			para.setAlignment(Element.ALIGN_LEFT);
			baseTableCell.addElement(para);
			
			para = new Paragraph("本人签名:                                                  年       月       日",cellContent);
			para.setAlignment(Element.ALIGN_RIGHT);
			baseTableCell.addElement(para);
			baseTableCell.setPaddingTop(5);
			baseTableCell.setPaddingBottom(5);
			
			baseTableCell.setFixedHeight(contentSize*8);
			baseTableCell.setColspan(8);
			baseTable.addCell(baseTableCell);
			
			pdfDoc.add(baseTable);
			pdfDoc.close();

			getResponse().setContentType("application/pdf");
			getResponse().setHeader("Content-Disposition", "attachment; filename=" + StringUtil.transFileName(gradeInfo.getPersonName() + "-基本信息表", getRequest()) + ".pdf");
			getResponse().setContentLength(baos.size());
			baos.writeTo(getResponse().getOutputStream());
			}
			}catch(IOException e){
				Log.doLog("EXCEPTION", e);
			}
		return null;
	}

在每一个字段和他对应的内容都会有setColspan()方法来设置它所占的列;

而行数默认是1行,所以在“相片”字段是需要3行的的时候,就必须用setRowspan(3)来设置;

setFixedHeight()是用来设置每一行的高度,可以自己设置;
注意:在显示照片的地方,设置setPadding(),是照片对内边框的设置;

           setBorder()是设置边框

在我写的这个打印pdf的代码中,遇到一个问题,当setPadding()和setBorder()一起使用的时候,相片的右边将会没有边框

原因有2个:1)是由于图片太大覆盖了边框;

             2)由于设置setBorder(1)与后面的baseTable.addCell(baseTableCell);重复,所以边框会消失

所以一般情况,setBorder()的值一般为0,或不要设置这个值。

你可能感兴趣的:(java)