iText中输出 中文

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、三种方式使用的字体多一些,但是需要和实际资源绑定,在实际项目中可以将一些字体库和项目打包在一起,下面我们以iTextAsian中自带的字体为例说明如何输出中文:

Java代码   收藏代码
  1. BaseFont bfChinese = BaseFont.createFont("STSong-Light""UniGB-UCS2-H",   
  2.             BaseFont.NOT_EMBEDDED);   
  3.               
  4. Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);  
  5.         document.add(new Paragraph(" 产生的报告",FontChinese));  

一个完整的例子:

 

Java代码   收藏代码
  1. /* 
  2.  * $Id: RepeatingTable.java,v 1.5 2005/05/09 11:52:47 blowagie Exp $ 
  3.  * $Name:  $ 
  4.  * 
  5.  * This code is part of the 'iText Tutorial'. 
  6.  * You can find the complete tutorial at the following address: 
  7.  * http://itextdocs.lowagie.com/tutorial/ 
  8.  * 
  9.  * This code is distributed in the hope that it will be useful, 
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  12.  * 
  13.  * itext-questions@lists.sourceforge.net 
  14.  */  
  15. package com.lowagie.examples.objects.tables.alternatives;  
  16.   
  17. import java.awt.Color;  
  18. import java.io.File;  
  19. import java.io.FileOutputStream;  
  20. import java.util.Date;  
  21.   
  22. import com.lowagie.text.Cell;  
  23. import com.lowagie.text.Document;  
  24. import com.lowagie.text.Element;  
  25. import com.lowagie.text.Font;  
  26. import com.lowagie.text.FontFactory;  
  27. import com.lowagie.text.PageSize;  
  28. import com.lowagie.text.Paragraph;  
  29. import com.lowagie.text.Phrase;  
  30. import com.lowagie.text.Rectangle;  
  31. import com.lowagie.text.Table;  
  32. import com.lowagie.text.pdf.BaseFont;  
  33. import com.lowagie.text.pdf.PdfWriter;  
  34.   
  35. /** 
  36.  * Shows how a table is split if it doesn't fit the page. 
  37.  */  
  38. public class RepeatingTable {  
  39.   
  40.     /** 
  41.      * Shows how a table is split if it doesn't fit the page. 
  42.      *  
  43.      * @param args 
  44.      *            no arguments needed 
  45.      */  
  46.     public static void main(String[] args) {  
  47.         System.out.println("table splitting");  
  48.         // creation of the document with a certain size and certain margins  
  49.         Document document = new Document(PageSize.A4.rotate(), 50505050);  
  50.   
  51.         try {  
  52.             // creation of the different writers  
  53.             String filePath = "d:" + File.separator + "temp" + File.separator  
  54.                     + "iText_Generated_pdf" + File.separator + "table"  
  55.                     + File.separator;  
  56.             File file = new File(filePath);  
  57.             if (!file.exists()) {  
  58.                 file.mkdirs();  
  59.             }  
  60.             PdfWriter.getInstance(document, new FileOutputStream(filePath  
  61.                     + "repeatingtable.pdf"));  
  62.   
  63.             // we add some meta information to the document  
  64.             document.addAuthor("chenzwei@cn.ibm.com,CTE WAC,GBSC,CDL,IBM");  
  65.             document.addSubject("This is a sample of iText in CTE.");  
  66.   
  67.             document.open();  
  68.   
  69.             Table datatable = new Table(10);  
  70.   
  71.             int headerwidths[] = { 10241212777777 };  
  72.             datatable.setWidths(headerwidths);  
  73.             datatable.setWidth(100);  
  74.             datatable.setPadding(3);  
  75.   
  76.             // the first cell spans 10 columns  
  77.             Cell cell = new Cell(new Phrase(  
  78.                     "Administration -System Users Report", FontFactory.getFont(  
  79.                             FontFactory.HELVETICA, 24, Font.BOLD)));  
  80.             cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
  81.             cell.setLeading(30);  
  82.             cell.setColspan(10);  
  83.             cell.setBorder(Rectangle.NO_BORDER);  
  84.             cell.setBackgroundColor(new Color(0xC00xC00xC0));  
  85.             datatable.addCell(cell);  
  86.   
  87.             // These cells span 2 rows  
  88.             datatable.setBorderWidth(2);  
  89.             datatable.setAlignment(1);  
  90.             datatable.addCell("User Id");  
  91.             datatable.addCell("Name\nAddress");  
  92.             datatable.addCell("Company");  
  93.             datatable.addCell("Department");  
  94.             datatable.addCell("Admin");  
  95.             datatable.addCell("Data");  
  96.             datatable.addCell("Expl");  
  97.             datatable.addCell("Prod");  
  98.             datatable.addCell("Proj");  
  99.             datatable.addCell("Online");  
  100.   
  101.             // this is the end of the table header  
  102.             datatable.endHeaders();  
  103.   
  104.             datatable.setBorderWidth(1);  
  105.   
  106.             for (int i = 1; i < 30; i++) {  
  107.   
  108.                 datatable.setAlignment(Element.ALIGN_LEFT);  
  109.   
  110.                 datatable.addCell("myUserId");  
  111.                 datatable  
  112.                         .addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");  
  113.                 datatable.addCell("No Name Company");  
  114.                 datatable.addCell("D" + i);  
  115.   
  116.                 datatable.setAlignment(Element.ALIGN_CENTER);  
  117.                 datatable.addCell("No");  
  118.                 datatable.addCell("Yes");  
  119.                 datatable.addCell("No");  
  120.                 datatable.addCell("Yes");  
  121.                 datatable.addCell("No");  
  122.                 datatable.addCell("Yes");  
  123.   
  124.             }  
  125.             BaseFont bfChinese = BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);   
  126.             Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);  
  127.             document.add(new Paragraph(" 产生的报告",FontChinese));  
  128.             document.add(datatable);  
  129.             document.newPage();  
  130.             document.add(new Paragraph(  
  131.                     "com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));  
  132.             datatable.setConvert2pdfptable(true);  
  133.             document.add(datatable);  
  134.             document.newPage();  
  135.             document.add(new Paragraph(  
  136.                     "com.lowagie.text.Table - Cells kept together"));  
  137.             datatable.setConvert2pdfptable(false);  
  138.             datatable.setCellsFitPage(true);  
  139.             document.add(datatable);  
  140.             document.newPage();  
  141.             document  
  142.                     .add(new Paragraph(  
  143.                             "com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));  
  144.             datatable.setConvert2pdfptable(true);  
  145.             document.add(datatable);  
  146.         } catch (Exception e) {  
  147.             e.printStackTrace();  
  148.         }  
  149.   
  150.         // we close the document  
  151.         document.close();  
  152.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于使用iText7将HTML转换为PDF并解决中文乱码问题,我可以给你一些思路和代码示例。 首先,我们需要使用iText7的Core和pdfHTML模块,你可以在pom.xml添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core</artifactId> <version>7.1.16</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>html2pdf</artifactId> <version>3.0.2</version> </dependency> ``` 然后,我们需要使用pdfHTML模块提供的`HtmlConverter`类将HTML转换为PDF。在转换之前,我们需要设置一些PDF文档的属性,例如页边距和字体。 下面是一个简单的代码示例,可以将HTML字符串转换为PDF文件并解决中文乱码问题: ```java import com.itextpdf.html2pdf.ConverterProperties; import com.itextpdf.html2pdf.HtmlConverter; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class HtmlToPdfConverter { public static void main(String[] args) throws IOException { // HTML字符串 String html = "<html><head><meta charset='utf-8'></head><body><p>你好,世界!</p></body></html>"; // 输出文件 File outputFile = new File("output.pdf"); FileOutputStream outputStream = new FileOutputStream(outputFile); // 创建PDF文档 PdfWriter writer = new PdfWriter(outputStream); PdfDocument pdfDocument = new PdfDocument(writer); pdfDocument.setDefaultPageSize(PageSize.A4); // 设置字体 PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false); ConverterProperties converterProperties = new ConverterProperties(); converterProperties.setFontProvider((familyName, encoding, size, style, color) -> font); // 将HTML转换为PDF HtmlConverter.convertToPdf(new ByteArrayInputStream(html.getBytes()), pdfDocument, converterProperties); // 关闭流 outputStream.close(); } } ``` 在上面的代码,我们使用了`PdfFontFactory`来创建指定字体的PDF字体对象。这里使用了华文宋体字体和UniGB-UCS2-H编码,你可以根据自己的需求进行调整。 另外,需要注意的是,如果你的HTML包含中文字符,一定要在`<head>`标签设置`<meta charset='utf-8'>`,这样就可以正确处理中文字符了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值