<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

本文涉及的产品
转发路由器TR,750小时连接 100GB跨地域
简介: 原文链接http://hintcnuie.iteye.com/blog/183690 转载内容iText中输出中文,有三种方式:1、使用iTextAsian.

原文链接

http://hintcnuie.iteye.com/blog/183690

转载内容

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 包文件)

相关文章
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
PipeMapRed.waitOutputThreads(): subprocess failed with code X ,这里code X对应的信息如下:error code 1: Operation not perm...
1063 0
|
Web App开发 前端开发 算法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
import java.util.LinkedHashMap;import java.util.Map; /** * LRU (Least Recently Used)  */public class LRUCache e...
685 0
|
Web App开发 Java Apache
|
新零售 大数据
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
电商数据分析基础指标体系 傅志华 傅志华 信息流、物流和资金流三大平台是电子商务的三个最为重要的平台。而电子商务信息系统最核心的能力是大数据能力,包括大数据处理、数据分析和数据挖掘能力。
1581 0
|
Web App开发 前端开发 API
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
     比如RDD里的计算调用了别的组件类里的方法(比如hbase里的put方法),那么序列化时,会将该方法所属的对象的所有变量都序列化的,可能有些根本没有实现序列化导致直接报错。
798 0
|
关系型数据库 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
mysql数据库切分 前言 通过MySQLReplication功能所实现的扩展总是会受到数据库大小的限制,一旦数据库过于庞大,尤其是当写入过于频繁,很难由一台主机支撑的时候,我们还是会面临到扩展瓶颈。
2137 0
|
Web App开发 机器学习/深度学习 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
  Caffe Torch Theano TensorFlow Language C++, Python Lua Python Python Pretrained Yes ++ Yes ++ Ye...
748 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Hive性能优化(全面) 2018-02-02 Hadoop大数据应用 1.介绍 首先,我们来看看Hadoop的计算框架特性,在此特性下会衍生哪些问题? 数据量大不是问题,数据倾斜是个问题。
1085 0
|
Python
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
1110 0
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Jboss7中ejb3使用@Schedule调度器总是每分钟执行 1 问题:当我尝试着去开发一个ejb3的@Schedule调度器来执行我预定每5秒钟执行一次的任务时。
832 0

热门文章

最新文章

下一篇
oss云网关配置