/**

* 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);

iText实现pdf导出的更多相关文章

  1. itext之pdf导出添加水印Java工具类

    import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentExce ...

  2. Java使用IText(VM模版)导出PDF

    Java使用IText(VM模版)导出PDF: public String createPDF(ProjectManageBase projectManageBase) { Map map = new ...

  3. Java Itext 生成PDF文件

    利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...

  4. C#:IText构造PDF文件

    IText构造PDF文件 1.1 生成Document Document是我们要生成的PDF文件所有元素的容器,因此要生成一个PDF文档,必须首先定义一个Document对象. Document有三种 ...

  5. Jasperreports以及iReport4.5报表PDF导出字体完美解决方案

    在使用Jasperreports以及iReport设计报表时,导出PDF是一个常见的需求.网上解决PDF导出中文显示问题相关的文章很多,无非就是设置控件的pdf font name和pdf encod ...

  6. java itext替换PDF中的文本

    itext没有提供直接替换PDF文本的接口,我们可以通过在原有的文本区域覆盖一个遮挡层,再在上面加上文本来实现. 所需jar包: 1.先在PDF需要替换的位置覆盖一个白色遮挡层(颜色可根据PDF文字背 ...

  7. iReport 5.6.0 PDF导出中文不显示问题 解决方案

    问题描述 iReport 5.6.0 PDF格式导出,中文不显示. 报错信息如下: Error exporting print... Could not load the following font ...

  8. vue实现pdf导出,解决生成canvas模糊等问题

    最近公司项目需要,利用vue实现pdf导出,从而保存到本地打印出来,说起来好像也很容易,具体要怎么实现呢? 1 .我们要添加两个模块 第一个.将页面html转换成图片 npm install --sa ...

  9. 在linux环境下使用itext生成pdf

    转载请注明出处 https://www.cnblogs.com/majianming/p/9537173.html 项目中需要对订单生成pdf文件,在不断的尝试之后,终于生成了比较满意的pdf文档. ...

随机推荐

  1. C++学习书籍推荐《More Exceptional C++(英文)》下载

    百度云及其他网盘下载地址:点我 作者简介 Herb Sutter is the author of three highly acclaimed books, Exceptional C++ Styl ...

  2. junit中test用法

    Test注解 有两个值, expected, timeout expect后面接异常类, timtout后面接时间, 符合则为ture 如 @Test (expected = NullPointExc ...

  3. Bzoj 2563: 阿狸和桃子的游戏 题解

    2563: 阿狸和桃子的游戏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 970  Solved: 695[Submit][Status][Discu ...

  4. 双剑合璧——掌握 cURL 和 Dig 走天涯

    如今随着大量的应用转移到网络,作为开发者,会经常做一些通讯测试,例如从网站获取信息.模拟用户向网站提交或者上传数据,查看应用通讯情况等等,现在变成了非常重要的任务. 一起来认识 cURL cURL 是 ...

  5. excel报表开发-- 根据datatable个数自动生成新sheet

    总结一下很久之前做的报表小程序,今日有问题又调试了一下. DB中存在一个表,记录了ID<自增长>,SqlStatement<sql查询语句>及其他必要字段,比如SheetNam ...

  6. request 中url拼接排序参数与签名算法

    一.参数要求: { appId:应用在后台创建应用时分配的应用编号,与应用密钥一一对应 sign:按照当前请求参数名的字母序进行升序排列(排序时区分大小写,除sign外,其它值不为空的参数都参与签名) ...

  7. py+selenium IE 用driver.close()却把两个窗口都关了【已解决】

    环境:py3  selenium  unittest 测试浏览器:IE10 目标:在单个文件中,有多个用例,执行完A用例,由于打开了新的窗口,必须关闭新的窗口,才不会影响下一条用例的执行. 问题:按例 ...

  8. Your project specifies TypeScriptToolsVersion 2.3, but a matching compiler ...... 出现这种警告解决方式

  9. dijkstra算法学习笔记

    dijkstra是一种单源最短路径算法,即求一个点到其他点的最短路.不能处理负边权. 最近某种广为人知的算法频繁被卡,让dijkstra逐渐成为了主流,甚至在初赛中鞭尸了SPFA(? dijkstra ...

  10. [leetcode] 19. Remove Nth Node From End of List (Medium)

    原题链接 删除单向链表的倒数第n个结点. 思路: 用两个索引一前一后,同时遍历,当后一个索引值为null时,此时前一个索引表示的节点即为要删除的节点. Runtime: 13 ms, faster t ...