itext 导出pdf 中文

 

	    response.setContentType("application/pdf");
	    response.setHeader("Expires", "0");
	    //response.setHeader("pragma","no-cache");
	    response.setContentType("application/x-msdownload");//指定文件为下载方式,是其不能在线打开
	    response.setHeader("Content-Disposition", "attachment; filename="+Day.getDay3()+b.getBcode()+".pdf");
	    response.setHeader("Cache-Control:no-cache", "must-revalidate, post-check=0, pre-check=0");
	    response.setHeader("Pragma", "public");
	    //response.setHeader("Content-disposition","inline; filename="+Day.getDay3()+b.getBcode()+".pdf" );
	    try {
	      // 新建一个文档,默认是A4纸的大小,4个边框为36
	      Document document = new Document();
	      // 将文档输出,我们写到输出流里面
	      PdfWriter.getInstance(document, response.getOutputStream());
	      // 以下的代码没有特殊的东西了。
	      	 
	      // 打开文档
	      document.open();	
	      BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
		          BaseFont.NOT_EMBEDDED);
		      Font FontChinese = new Font(bfChinese, 12, Font.BOLD);
		      Font contextFont = new Font(bfChinese,10,Font.NORMAL);   
	      Paragraph title = new Paragraph("文儀用品申請清單 ",FontChinese);   
	        //设置标题格式对齐方式   
	        title.setAlignment(Element.ALIGN_CENTER);  
	        //title.setFont(FontChinese);   
	        document.add(title);
	        Paragraph mydbranch = new Paragraph("申請部門:"+b.getBname(),new Font(bfChinese,8,Font.NORMAL));  
	        mydbranch.setAlignment(Element.ALIGN_RIGHT);		  
	        document.add(mydbranch);
	        Paragraph mydatep = new Paragraph("列印時間:"+Day.getDay2(),new Font(bfChinese,8,Font.NORMAL));  
	        mydatep.setAlignment(Element.ALIGN_RIGHT);		  
	        document.add(mydatep);
	     
	        Table table = new Table(6);   
	        int width[] = {10,16,15,20,10,10};//设置每列宽度比例   
	        table.setWidths(width); 
	        
	        table.setWidth(90);//占页面宽度比例   
	        
	        table.setPadding(2);
	        table.setSpacing(0);
	        table.setAlignment(Element.ALIGN_CENTER);//居中   
	        table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中   
	        table.setAutoFillEmptyCells(true);//自动填满   
	        table.setBorderWidth(1);//边框宽度   
	        Cell cell = new Cell();
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);   
	        cell.setVerticalAlignment(Element.ALIGN_CENTER);   
	        Cell cell1= new Cell(new Paragraph("項目 ",contextFont));
	        //Cell cell2= new Cell(new Paragraph("部門名稱 ",contextFont));
	        Cell cell3= new Cell(new Paragraph("貨品編號 ",contextFont));
	        Cell cell4= new Cell(new Paragraph("單價  ",contextFont));
	        Cell cell5= new Cell(new Paragraph("貨品名稱  ",contextFont));
	        Cell cell6= new Cell(new Paragraph("數量 ",contextFont));
	        Cell cell7= new Cell(new Paragraph("單位  ",contextFont));
	        table.addCell(cell1);
	        //table.addCell(cell2);
	        table.addCell(cell3);
	        table.addCell(cell4);
	        table.addCell(cell5);
	        table.addCell(cell6);
	        table.addCell(cell7);
	        int i=0;
			for (OrderList o : listO) {
				int num = o.getNum();
				if (num != 0) {
					i++;
					Food f = this.fservice.getFood(o.getFid());
					  table.addCell(new Cell(i+""));            
			            table.addCell(new Cell(f.getFnum()));
			            table.addCell(new Cell(f.getStandard()));
			            table.addCell(new Cell(new Paragraph(f.getFname(),contextFont)));
			            table.addCell(new Cell(num+"" ));
			            table.addCell(new Cell(new Paragraph(f.getUnit(),contextFont)));
				
				}
				
				
			}
	        document.add(table);
	        document.add(new Paragraph("\n"));
	        Paragraph bottomName1 = new Paragraph("部門主管簽字:_________________",contextFont);   
	        bottomName1.setAlignment(Element.ALIGN_RIGHT);   
	        document.add(bottomName1);
	        Paragraph bottomName2 = new Paragraph("日期:_________________",contextFont);   
	        bottomName2.setAlignment(Element.ALIGN_RIGHT);     
	        document.add(bottomName2);
	        document.close();
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }


iText中输出中文,有三种方式:

 


1、使用iTextAsian.jar中的字体
    BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
2、使用Windows系统字体(TrueType)
        BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
3、使用资源字体(ClassPath)
    BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

第2、三种方式使用的字体多一些,但是需要和实际资源绑定,在实际项目中可以将一些字体库和项目打包在一起。

 

 

以下个人写的一个示例,但导出pdf后,有的电脑能正常显示,有的不显示中文字体,可能是客户机没有相关字体库,建议使用第三种方式

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值