itext导出pdf 中文

最近一个项目需要导出pdf文档,选择使用了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后,有的电脑能正常显示,有的不显示中文字体,可能是客户机没有相关字体库,建议使用第三种方式

 /*public void xxxx(){

         
         
         
             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("xx項目 ",contextFont));
                 //Cell cell2= new Cell(new Paragraph("xx名稱 ",contextFont));
                 Cell cell3= new Cell(new Paragraph("xx編號 ",contextFont));
                 Cell cell4= new Cell(new Paragraph("xx單價  ",contextFont));
                 Cell cell5= new Cell(new Paragraph("xx名稱  ",contextFont));
                 Cell cell6= new Cell(new Paragraph("xx數量 ",contextFont));
                 Cell cell7= new Cell(new Paragraph("xx單位  ",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();
             }

}

猜你喜欢

转载自yuegedetiang.iteye.com/blog/2233303