java生成pdf表格


import jxl.Cell;
import jxl.Range;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class Pdf {
	
	private PdfPTable table;
	
	private PdfWriter writer;
	
	private BaseFont bf;
	
	private final Integer title1Hight=38;
	private final Integer title2Hight=28;
	private final Integer propertisHight=17;
	private  final static Integer resultHight=20;
	private  final static Integer colNum=8;

	
	@Test
	public void genTable() throws Exception {
		// 每种尺寸的具体数值设置去com.itextpdf.text.PageSize中查看,横板就原数值顺序反过来
	     Document document=new Document(new RectangleReadOnly(708,498), 2, 2,10, 10);
	     String newFilename = "E:\\excel\\reportNew"+System.currentTimeMillis()+".pdf";
	     this.writer = PdfWriter.getInstance(document, new FileOutputStream(newFilename));  
	        
	  
	      //创建BaseFont对象,指明字体,编码方式,是否嵌入      
	      bf=  BaseFont.createFont("E:\\simsun.ttc,0", BaseFont.IDENTITY_H,false);
	      //创建Font对象,将基础字体对象,字体大小,字体风格    
	      final Font title1Font=new Font(bf,15,Font.BOLD);
	      final Font title2Font=new Font(bf,14,Font.BOLD);
	      final Font propertisFont=new Font(bf,9,Font.NORMAL); 
	      final Font headerFont=new Font(bf,10,Font.BOLD);
	      final Font resultFont=new Font(bf,10,Font.NORMAL);
	     
	      try {  
	          table=new PdfPTable(colNum);     
	            
	          PdfPCell cellPdf = new PdfPCell();
	          cellPdf.disableBorderSide(15);

	          
	          cellPdf = mergeCol("大标题", title1Font, colNum,title1Hight); 
	          table.addCell(cellPdf);
	          cellPdf = mergeCol("副标题", title2Font, colNum,title2Hight);
	          table.addCell(cellPdf);
	          
	          List properties=new ArrayList();

	          for(int i=0;i<16;i++) {
	         	 cellPdf = mergeCol("样本号:H1234567", propertisFont, 2,propertisHight);
	             table.addCell(cellPdf);
	          }
	        
	          createDividingLine();
	    
	          createTableHeaderRow(headerFont,"项目名称","AST","1568.98","mol/L","964-180","正常");
	          for(int i=0;i<10;i++) {
	        	  createResultRow(resultFont,"血清电视服务和任天堂给微软个大","AST","1568.98","mol/L","964-180","正常");
	          }
	          
	          createDividingLine();
	        
	          
	          for(int i=0;i<12;i++) {
	         	 cellPdf = mergeCol("送检日志:2021-05-31", propertisFont, 2,propertisHight);
	             table.addCell(cellPdf);
	          }
	          document.open();        
	          document.add(table);    
	          document.close();    
	          writer.close();  
	      } catch (Exception e) {  
	          // TODO Auto-generated catch block  
	          e.printStackTrace();  
	      }  
	}
	
	//创建分割线
	public void createDividingLine() {
		  //创建单元格对象,将内容与字体放入段落中作为单元格内容    
        PdfPCell cell=new PdfPCell(new Paragraph("",null));    
      //  cell.setBorder(Rectangle.TOP);
            
        //设置最小单元格高度    
        cell.setMinimumHeight(0);    
        
        for(int i=0;i<colNum;i++) {
        	table.addCell(cell);
        }
	}
 	
	 public void createResultRow(Font font,String colv1,String colv2,String colv3,String colv4,String colv5,String colv6) {
		 //项目名称 
		 PdfPCell cell = getResultCell(colv2,font);
		 cell = mergeCol(colv1, font, 2);
          table.addCell(cell); 
          //项目简称 
          table.addCell(getResultCell(colv2,font));
          //检测结果 
          table.addCell(getResultCell(colv3,font));
          //单位
          table.addCell(getResultCell(colv4,font));
          //参考值
          cell = mergeCol(colv5, font, 2);
          table.addCell(cell);
          //标志
          table.addCell(getResultCell(colv6,font));
	 }
	 
	 public void createTableHeaderRow(Font font,String colv1,String colv2,String colv3,String colv4,String colv5,String colv6) {
		 PdfPCell cell = getTableHeaderCell(colv2,font);
		
		 //项目名称 
		  cell = mergeCol(colv1, font, 2);
          table.addCell(cell); 
          //项目简称 
          table.addCell(getTableHeaderCell(colv2,font));
          //检测结果 
          table.addCell(getTableHeaderCell(colv3,font));
          //单位
          table.addCell(getTableHeaderCell(colv4,font));
          //参考值
          cell = mergeCol(colv5, font, 2);
          table.addCell(cell);
          //标志
          table.addCell(getTableHeaderCell(colv6,font));
	 }
  
 
    
    //合并列的静态函数指定
    public static PdfPCell mergeCol(String str,Font font,int i,int minimumHeight) {       	 
        PdfPCell cell=new PdfPCell(new Paragraph(str,font));     cell.disableBorderSide(15);
        cell.setMinimumHeight(minimumHeight);    
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        //将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格    
        cell.setColspan(i);               
        return cell;    
    }  
    //合并列的静态函数    
    public static PdfPCell mergeCol(String str,Font font,int i) {            
        return mergeCol(str,font,i,25);    
    }  
    
    //获取结果列表标题 
   public static PdfPCell getTableHeaderCell(String string, Font font)     
   {    
       //创建单元格对象,将内容与字体放入段落中作为单元格内容    
       PdfPCell cell=new PdfPCell(new Paragraph(string,font));    
       cell.setBorder(1);
       cell.setHorizontalAlignment(Element.ALIGN_CENTER);    
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
           
       //设置最小单元格高度    
       cell.setMinimumHeight(resultHight);    
       return cell;    
   } 
     //获取结果列表单元格    
    public static PdfPCell getResultCell(String string, Font font)     
    {    
        //创建单元格对象,将内容与字体放入段落中作为单元格内容    
        PdfPCell cell=new PdfPCell(new Paragraph(string,font));    
        cell.disableBorderSide(15);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
            
        //设置最小单元格高度    
        cell.setMinimumHeight(resultHight);    
        return cell;    
    }   
    public static Paragraph convertParToChinese(String text, int fontsize, int fontStyle, Color color)
            throws Exception {
        BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle);
        Paragraph graph = new Paragraph(text, fontChinese);
        return graph;
    }
    //合并行的静态函数    
    public static PdfPCell mergeRow(String str,Font font,int i) {    
            
        //创建单元格对象,将内容及字体传入    
        PdfPCell cell=new PdfPCell(new Paragraph(str,font));    
        //设置单元格内容居中    
        cell.disableBorderSide(15);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
        //将该单元格所在列包括该单元格在内的i行单元格合并为一个单元格    
        cell.setRowspan(i);    
            
        return cell;    
    }   

}

 

 

 

 <dependency>
         <groupId>com.itextpdf</groupId>
         <artifactId>itextpdf</artifactId>
         <version>5.5.5</version>
   </dependency>

   <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
   </dependency>
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.6</version>
</dependency>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值