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中自带的字体为例说明如何输出中文:
- BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
- BaseFont.NOT_EMBEDDED);
- Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
- document.add(new Paragraph(" 产生的报告",FontChinese));
一个完整的例子:
- /*
- * $Id: RepeatingTable.java,v 1.5 2005/05/09 11:52:47 blowagie Exp $
- * $Name: $
- *
- * This code is part of the 'iText Tutorial'.
- * You can find the complete tutorial at the following address:
- * http://itextdocs.lowagie.com/tutorial/
- *
- * This code is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * itext-questions@lists.sourceforge.net
- */
- package com.lowagie.examples.objects.tables.alternatives;
- import java.awt.Color;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.util.Date;
- import com.lowagie.text.Cell;
- import com.lowagie.text.Document;
- import com.lowagie.text.Element;
- import com.lowagie.text.Font;
- import com.lowagie.text.FontFactory;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Phrase;
- import com.lowagie.text.Rectangle;
- import com.lowagie.text.Table;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- /**
- * Shows how a table is split if it doesn't fit the page.
- */
- public class RepeatingTable {
- /**
- * Shows how a table is split if it doesn't fit the page.
- *
- * @param args
- * no arguments needed
- */
- public static void main(String[] args) {
- System.out.println("table splitting");
- // creation of the document with a certain size and certain margins
- Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
- try {
- // creation of the different writers
- String filePath = "d:" + File.separator + "temp" + File.separator
- + "iText_Generated_pdf" + File.separator + "table"
- + File.separator;
- File file = new File(filePath);
- if (!file.exists()) {
- file.mkdirs();
- }
- PdfWriter.getInstance(document, new FileOutputStream(filePath
- + "repeatingtable.pdf"));
- // we add some meta information to the document
- document.addAuthor("chenzwei@cn.ibm.com,CTE WAC,GBSC,CDL,IBM");
- document.addSubject("This is a sample of iText in CTE.");
- document.open();
- Table datatable = new Table(10);
- int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };
- datatable.setWidths(headerwidths);
- datatable.setWidth(100);
- datatable.setPadding(3);
- // the first cell spans 10 columns
- Cell cell = new Cell(new Phrase(
- "Administration -System Users Report", FontFactory.getFont(
- FontFactory.HELVETICA, 24, Font.BOLD)));
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setLeading(30);
- cell.setColspan(10);
- cell.setBorder(Rectangle.NO_BORDER);
- cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
- datatable.addCell(cell);
- // These cells span 2 rows
- datatable.setBorderWidth(2);
- datatable.setAlignment(1);
- datatable.addCell("User Id");
- datatable.addCell("Name\nAddress");
- datatable.addCell("Company");
- datatable.addCell("Department");
- datatable.addCell("Admin");
- datatable.addCell("Data");
- datatable.addCell("Expl");
- datatable.addCell("Prod");
- datatable.addCell("Proj");
- datatable.addCell("Online");
- // this is the end of the table header
- datatable.endHeaders();
- datatable.setBorderWidth(1);
- for (int i = 1; i < 30; i++) {
- datatable.setAlignment(Element.ALIGN_LEFT);
- datatable.addCell("myUserId");
- datatable
- .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");
- datatable.addCell("No Name Company");
- datatable.addCell("D" + i);
- datatable.setAlignment(Element.ALIGN_CENTER);
- datatable.addCell("No");
- datatable.addCell("Yes");
- datatable.addCell("No");
- datatable.addCell("Yes");
- datatable.addCell("No");
- datatable.addCell("Yes");
- }
- BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
- document.add(new Paragraph(" 产生的报告",FontChinese));
- document.add(datatable);
- document.newPage();
- document.add(new Paragraph(
- "com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));
- datatable.setConvert2pdfptable(true);
- document.add(datatable);
- document.newPage();
- document.add(new Paragraph(
- "com.lowagie.text.Table - Cells kept together"));
- datatable.setConvert2pdfptable(false);
- datatable.setCellsFitPage(true);
- document.add(datatable);
- document.newPage();
- document
- .add(new Paragraph(
- "com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));
- datatable.setConvert2pdfptable(true);
- document.add(datatable);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // we close the document
- document.close();
- }
- }
附录:
http://www.lowagie.com/iText/tutorial/index.html (iText教程) http://www.lowagie.com/iText/download.html (iText核心包文件) http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948 (iTextArea 包文件)
相关推荐
Paragraph paragraph = new Paragraph("你的中文内容", chineseFont); document.add(paragraph); ``` 这里,`new Font(bfChinese, 12, Font.NORMAL)`创建了一个基于`bfChinese`的字体,设置字号为12,样式为正常。...
CmsLHFont chineseFont = CmsLHFont.loadFont("/path/to/chinese-font.ttf"); // 添加中文文本 Paragraph paragraph = new Paragraph("你好,世界!", chineseFont); document.add(paragraph); document....
Font font = FontFactory.getFont("ChineseFont", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); Document document = new Document(); ...
这个扩展包包含了对CJK(Chinese, Japanese, Korean)字符集的支持,使得开发者能够轻松地在PDF中插入中文文本,而无需担心乱码问题。在使用时,你需要确保正确加载了包含中文字体的资源,通常这些字体文件存储在...
由于东亚字符集如CJK(Chinese, Japanese, Korean)的复杂性,标准的IText库可能无法完美呈现这些语言的文本。iTextAsian包含了额外的字体和排版规则,确保在PDF文档中正确显示和处理这些语言的文字。这包括但不限于...
Font chineseFont = new Font(bfChinese, 12); Paragraph paragraph = new Paragraph("你好,世界!", chineseFont); document.add(paragraph); ``` 6. 处理表格和图像:` PdfPTable`类用于创建表格,`Image`类用于...
Font chineseFont = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.BLACK, true); chineseFont.setSubset(true); String chineseText = "你好,世界!"; Paragraph para = new Paragraph(chinese...
5. **字体支持**:iText支持多种字体,包括Unicode和CJK(Chinese, Japanese, Korean)字符集,如`iTextAsian.jar`所示,这确保了在PDF中正确显示非拉丁字符。 6. **国际化和PDF/A兼容**:`itext-pdfa-5.3.2-...
Git上下载的PDF开发工具(Java),目前是最新...font-asian-x.y.z.jar: use this is you need CJK functionality (Chinese / Japanese / Korean) sign-x.y.z.jar: use this if you need support for digital signatures
这个扩展库包含了对CJK(Chinese, Japanese, Korean)字符的支持,使得在PDF中正确显示和处理这些语言成为可能。在没有这个扩展的情况下,iText可能会遇到处理亚洲文字时的编码问题,导致输出不正确或者无法识别的...
Font chineseFont = new Font(bfChinese, 12, Font.NORMAL); ``` 3. **打开和关闭文档**:调用`document.open()`开始写入内容,`document.close()`则表示写入结束。 ```java document.open(); ``` 4. **添加中文...
Font chineseFont = FontFactory.getFont(FontFactory.registeredFontNames()[0], BaseFont.IDENTITY_H, BaseFont.EMBEDDED); ``` `BaseFont.IDENTITY_H`表示使用Unicode编码,`BaseFont.EMBEDDED`表示嵌入字体,...
Font chineseFont = FontFactory.getFont("myChineseFont", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Paragraph para = new Paragraph("你的中文内容", chineseFont); document.add(para); ``` 这里,`...
Font chineseFont = new Font(bfChinese, 12, Font.NORMAL); document.add(new Paragraph("你好,世界!", chineseFont)); document.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在...
Font chineseFont = new Font(bfChinese, 12, Font.NORMAL); // 添加中文内容 document.add(new Paragraph("你好,世界!", chineseFont)); document.close(); System.out.println("PDF created successfully...
iText 2.x版本默认不支持中文,而iText 5.x引入了对CJK(Chinese, Japanese, Korean)字符的支持。你需要提供支持中文的TrueType字体文件(.ttf),并使用`BaseFont`类加载这些字体,然后在创建`Font`对象时指定。...
它包含了对CJK(Chinese, Japanese, Korean)字符的支持,确保在生成的PDF文档中正确显示这些语言的文字。 最后,`itext.jar`可能是另一个版本的iText库,可能是更早的1.x版本或者是开发者误输入的文件名。在这个...
它包含了对CJK(Chinese, Japanese, Korean)字符集的支持,确保了PDF文档中的文字正确显示,避免了乱码问题。 2. **iTextpdf-5.0.6.jar**:这是iText的核心库,提供了一系列API用于创建、编辑和读取PDF文档。你...
document.add(new Paragraph("报表中的中文内容", chineseFont)); ``` 4. **表格操作**:` PdfPTable`类可以用来创建和填充表格,适用于展示结构化的数据。例如: ```java PdfPTable table = new PdfPTable(3);...