/**

* AsianTest.java

*/

import java.io.FileOutputStream;
import java.io.IOException; import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.Font;
import java.awt.Color; public class AsianTest { public static void main(String[] args) { // 创建一个Document对象
Document document = new Document(); try { // 生成名为 AsianTest.pdf 的文档
PdfWriter.getInstance(document, new FileOutputStream(
"c://AsianTest.pdf")); /**
* 新建一个字体,iText的方法 STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀
* UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版, 相应的 V
* 代表竖版
*/
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", false); Font bold_fontChinese = new Font(bfChinese, 12, Font.BOLD,
Color.BLACK);
Font italic_fontChinese = new Font(bfChinese, 12, Font.ITALIC,
Color.BLACK);
Font impressFont = new Font(bfChinese, 16, Font.BOLDITALIC,
Color.BLACK);
// 打开文档,将要写入内容
document.open(); // 插入一个段落
// Paragraph par = new Paragraph("我们", fontChinese); // document.add(par);
//
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" ", bold_fontChinese));
String[] Trainspotting1 = { "选择生命,选择工作,选择职业,选择家庭,",
"选择可恶的大彩电,选择洗衣机、汽车、雷射碟机,", "选择健康、低胆固醇和牙医保险,选择楼宇按揭,",
"选择你的朋友,选择套装、便服和行李,选择分期付款和三件套西装,",
"选择收看无聊的游戏节目,边看边吃零食……选择你的未来,选择生命……", "太多选择,你选择什么,我选择不选择。" };
String[] Trainspotting2 = { "这是电影《猜火车》开头的旁白。", "这是一个关于“选择”的故事。" };
String[] Benjamin1 = { "有些人就在河边出生长大,", "有些人被闪电击中,",
"有些人对音乐有着非凡的天赋,", "有些人是艺术家,", "有人会游泳,", "有人懂得做纽扣,",
"有人会背莎士比亚,", "而有些人。。。是母亲,", "也有些人,可以翩翩起舞。",
"Goodnight Daisy", "Goodnight Benjamin" };
String[] Benjamin2 = { "这是电影《本杰明传奇》结尾的旁白。", "这是一个关于“错过”的故事。" };
String[] text1 = { "我想说的是,", "我们选择,同时,我们错过。" };
String[] text2 = { "抛去无可选择的选择,抑或不选择的选择,",
"很有趣的一件事:当面临(太多的)选择,人们会如何选择;", "同时,人们又会如何看待错过。" };
String[] text3 = { "在开始和结束之间,选择了什么,又会错过什么,我还不知道。" };
String[] text4 = { "你会知道么?" };
//
for (String s : Trainspotting1) {
document.add(new Paragraph(s, italic_fontChinese));
document.add(new Paragraph(" ", italic_fontChinese));
}
for (String s : Trainspotting2) {
document.add(new Paragraph(s, bold_fontChinese));
}
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" ", bold_fontChinese));
for (String s : Benjamin1) {
document.add(new Paragraph(s, italic_fontChinese));
document.add(new Paragraph(" ", italic_fontChinese));
}
for (String s : Benjamin2) {
document.add(new Paragraph(s, bold_fontChinese));
}
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" ", bold_fontChinese));
document.add(new Paragraph(" ", bold_fontChinese));
for (String s : text1) {
document.add(new Paragraph(s, bold_fontChinese));
}
document.add(new Paragraph(" ", bold_fontChinese));
for (String s : text2) {
document.add(new Paragraph(s, bold_fontChinese));
}
document.add(new Paragraph(" ", bold_fontChinese));
for (String s : text3) {
document.add(new Paragraph(s, bold_fontChinese));
}
document.add(new Paragraph(" ", bold_fontChinese));
for (String s : text4) {
document.add(new Paragraph(s, bold_fontChinese));
}
document.add(new Paragraph(" ", bold_fontChinese));
//
String[] end = { "Some people were born to sit by a river...",
"Some get struck by light...",
"Some have an ear for music...", "Some are artists...",
"Some swim...", "Some know buttons...",
"Some know Shakespeare...", "Some are mothers...",
"And some people can dance..." };
for (String s : end) {
document.add(new Paragraph(s, bold_fontChinese));
}
document.add(new Paragraph(
"by the way, some people can write code.你", impressFont)); // Chapter
Paragraph title1 = new Paragraph("Chapter 1", italic_fontChinese);
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph(
"This is Section 1 in Chapter 1中文", italic_fontChinese);
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph(
"This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
//
document.add(chapter1);
//
// 定义一个图片 Image jpeg = Image.getInstance("E:/01.jpg"); // 图片居中
jpeg.setAlignment(Image.ALIGN_CENTER);
document.add(jpeg);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
} // 关闭打开的文档
document.close();
}
}

此上的文章转载别人的,觉得挺好就借鉴过来



原创部分

  • 建立Document对象的实例

    Document document = new Document();

  • 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中,filePath是pdf的生成路径

PdfWriter.getInstance(document, new FileOutputStream(
filePath));
  • 新建一个字体,iText的方法STSongStf-Ligth 是字体,在BaseFont中设置之后,我们到处的pdf就可以兼容中文了。itext还有两种输出中文字体的设置,

    • 1 使用iTextAsian.jar中的字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", false);
    -  2 使用Windows系统字体(TrueType)

BaseFont.createFont(“C:/WINDOWS/Fonts/SIMYOU.TTF”, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

  • 使用资源地址
BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

最新文章

  1. mycat初探
  2. 查找文件并执行的shell命令
  3. 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
  4. WebService优点和缺点小结
  5. easyui tabs 真正刷新用法
  6. 学校作业-Usaco DP水题
  7. 关于mysql5.6.13的一个疑问
  8. contact form
  9. openstack controller ha测试环境搭建记录(六)——配置keystone
  10. PHP服务器脚本 PHP内核探索:新垃圾回收机制说明
  11. html字体问题
  12. Perl系列文章
  13. Linux 查找最大文件
  14. 将List按照指定大小等分的几种实现方式和效率对比及优化
  15. 重置表单中的文件上传控件(file input)的方法
  16. js中call,caller,callee,aplly
  17. rest-framework之分页器
  18. 重写&重载
  19. jQuery----JQuery动画(hide()和show())(下)
  20. sql server 2008 R2连接失败 错误:18456

热门文章

  1. django基础知识之管理静态文件css,js,images:
  2. Java实现LRU算法
  3. 基于SpringCloud的微服务架构实战案例项目
  4. Mysql CPU使用率长期100%的解决思路备忘
  5. 9.5 考试 第三题 奇袭题解(codeforce 526f)
  6. MyBatis从入门到精通:第一章的pom.xml文件
  7. py+selenium遇见IE,元素只有name属性【神奇解决】
  8. USACO-修理牛棚
  9. Excel催化剂开源第44波-窗体在Show模式下受Excel操作影响变为最小化解决方式
  10. fjnu2016-2017 低程 PROBLEM C 汪老司机