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中自带的字体为例说明如何输出中文:
- 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 包文件)
转载于:https://www.cnblogs.com/telwanggs/p/5357907.html
iText中输出 中文相关推荐
- vs2012编程中输出中文出现乱码
关于"vs2012编程中输出中文出现乱码"问题的探究 问题如下: //代码如下// #include<iostream> using namespace std; in ...
- 怎么样才能在CODELITE中输出中文呢!!!
前段时间做了个词法分析器,这两天想做语法分析中的算法优先符的程序的,结果不会了!悲剧,然后我就想着把我的词法分析器再整好看点,结果我用的 CODELITE 这IDE没法正确显示中文,郁闷!prin ...
- php 显示中文utf,php adodb 从mysql数据库中输出中文显示到utf编码网页乱码问题
首先html的编码格式是utf-8, 然后php通过adodb连接后也要设置utf-8的连接方式, 然后查询的时候也要以utf-8的方式查询, 这样就不会造成输出中文乱码的问题了. 代码更改如下 ad ...
- python乱码怎么办_python中输出中文乱码怎么解决
我们在使用python进行编程的时候,往往会面临输出中文的问题,这个时候往往会报错,小编这次与大家分享一下怎么解决. 工具/原料 Pycharm 方法/步骤 1 我们以pycharm为IDE工具,来举 ...
- python中如何输出中文_python3如何输出中文
Python3中输出中文的方法如下: 方法一:在环境变量中,设置PYTHONIOENCODING=utf-8 以centos为例执行:export PYTHONIOENCODING=utf-8 方法二 ...
- java下输出中文的一点研究
网上或者大部分书上都说Java中输出中文使用FileReader类就可以了,但是当你读取一个中文文档时,你会发现,除了乱码,还是乱码.究其原因,这其实是文件流读取时使用的编码方式和文件本身编码方式不同 ...
- python 如何输出中文_python3如何输出中文
Python3中输出中文的方法如下: 方法一:在环境变量中,设置PYTHONIOENCODING=utf-8 以centos为例执行:export PYTHONIOENCODING=utf-8 方法二 ...
- putText输出中文方法
最近有时候需要在图中输出中文,最先想到的是转换成GB2313的方法进行输出,但是不行,无奈只能想别的方法去实现. 先看看如果将utf-8转换成GB2313 std::string UTF8ToGBK( ...
- python加中文注释_Python使用中文注释和输出中文(原创)
刚开始学习python,需要在Python中注释中文和输出中文,现在开始尝试: 仅为初步学习参考,高手请绕行. -------------------------------------------- ...
最新文章
- python中递归函数的实例_Python 递归函数详解及实例
- Go语言垃圾回收(GC)
- 通过销售订单领用到成本中心,FI替代实现不同成本中心记账科目不同
- java各种数据库对应的jar包、驱动类名和URL格式
- 如何用命令行给mySQL添加用户
- 盘点80年前欧美最为匪夷所思的发明
- 关于Ajax工作原理
- Fast-SCNN 多分支结构共享低级特征的语义分割网络 (一)
- C语言之字符串探究(八):strchr、strstr、strtok
- kprobe原理解析(一)
- LINUX下载编译libxml2
- 智能车制作1——编码器
- PYTHON混淆器 pyobfuscate
- vue实现中英文切换
- tf15: 中文语音识别【转】
- lzg_ad:EWF原理详解
- C语言学习-- 计算机原理及二进制
- 好用的一些功能性网站
- c语言计算二次函数顶点坐标,C语言编写一个求一元二次方程的实根的程序。 编辑一个小程序去做一元二次方程的求解(b^24ac)...
- 旧金山大学数据结构和算法的可视化工具
热门文章
- 深入探讨一下如何打断点
- 【AD】PCB设计知识整理(持续更新)
- 【C语言】关键字volatile之有关__IO、__O、__I的故事
- L2TP协议笔记1---L2TP概念及协议流程分析
- linux网络代码结构
- 崩坏3服务器维护2月8号,《崩坏3》8月29日版本更新,全服维护通知
- ip和端口的本质与作用,网络协议栈
- linux共享内存 pmu,如何使用gator/streamline 收集PMU perf event计数
- 【LeetCode】剑指 Offer 07. 重建二叉树
- JavaScript-面试 表单验证