itext导出中文

最近一个项目需要导出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、三种方式使用的字体多一些,但是需要和实际资源绑定,在实际项目中可以将一些字体库和项目打包在一起。

</pre><p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;">以下个人写的一个示例,但导出pdf后,有的电脑能正常显示,有的不显示中文字体,可能是客户机没有相关字体库,建议使用第三种方式</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;"><pre name="code" class="java">   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();
             }


使用了 itextAsian.jar  ,itext-5.0.6.jar     ,注意itexAsian.jar对itext 的版本支持。

 

 

记录下代码,方便以后用到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iText 7是一款用于操作PDF的开源库,可以在多种不同的操作系统上使用,包括Linux。如果在使用iText 7在Linux上导出PDF时出现了文字字体空白的问题,可能是由于以下原因导致的: 1. 缺少中文字体: Linux系统默认情况下可能没有安装中文字体,这会导致在PDF中显示的文字出现空白。解决方法是在Linux系统上安装中文字体,例如通过执行命令 "apt-get install fonts-wqy-zenhei" 来安装文泉驿正黑字体。 2. 字体路径设置不正确:在使用iText 7时需要正确设置字体路径,确保能够找到所需的中文字体文件。可以通过使用setFont方法来设置字体路径和字体名称,例如: Font font = FontFactory.getFont("/path/to/font.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 或者 fontProvider.addFont("/path/to/font.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 这样可以确保在导出PDF时能够正确地使用中文字体。 3. 字体嵌入设置不正确:可能在导出PDF时没有将中文字体正确地嵌入到PDF文件中。可以通过设置字体的嵌入模式来解决此问题,例如: font.setSubset(false); // 设置不进行字体子集化,将整个字体嵌入到PDF中。 这些可能是导致iText 7在Linux上导出PDF中文字体空白的常见问题和解决方法。根据具体的情况,可能需要根据以上涉及到的步骤进行检查和调整,以确保在Linux上正确地显示和嵌入中文字体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值