iText实现pdf导出
/**
* 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导出的更多相关文章
- itext之pdf导出添加水印Java工具类
import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentExce ...
- Java使用IText(VM模版)导出PDF
Java使用IText(VM模版)导出PDF: public String createPDF(ProjectManageBase projectManageBase) { Map map = new ...
- Java Itext 生成PDF文件
利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...
- C#:IText构造PDF文件
IText构造PDF文件 1.1 生成Document Document是我们要生成的PDF文件所有元素的容器,因此要生成一个PDF文档,必须首先定义一个Document对象. Document有三种 ...
- Jasperreports以及iReport4.5报表PDF导出字体完美解决方案
在使用Jasperreports以及iReport设计报表时,导出PDF是一个常见的需求.网上解决PDF导出中文显示问题相关的文章很多,无非就是设置控件的pdf font name和pdf encod ...
- java itext替换PDF中的文本
itext没有提供直接替换PDF文本的接口,我们可以通过在原有的文本区域覆盖一个遮挡层,再在上面加上文本来实现. 所需jar包: 1.先在PDF需要替换的位置覆盖一个白色遮挡层(颜色可根据PDF文字背 ...
- iReport 5.6.0 PDF导出中文不显示问题 解决方案
问题描述 iReport 5.6.0 PDF格式导出,中文不显示. 报错信息如下: Error exporting print... Could not load the following font ...
- vue实现pdf导出,解决生成canvas模糊等问题
最近公司项目需要,利用vue实现pdf导出,从而保存到本地打印出来,说起来好像也很容易,具体要怎么实现呢? 1 .我们要添加两个模块 第一个.将页面html转换成图片 npm install --sa ...
- 在linux环境下使用itext生成pdf
转载请注明出处 https://www.cnblogs.com/majianming/p/9537173.html 项目中需要对订单生成pdf文件,在不断的尝试之后,终于生成了比较满意的pdf文档. ...
随机推荐
- RSYNC部署
1 rsync简介 1.1 什么是rsync rsync: - a fast, versatile, remote (and local) file-copying toolrsync:是一种快速,多 ...
- 判断List中是否含有某个实体bean
注意:使用List.contains(Object object)方法判断ArrayList是否包含一个元素对象(针对于对象的属性值相同,但对象地址不同的情况),如果没有重写List的元素对象Obje ...
- android_alertDialog
主文件 package cn.com.sxp;import android.app.Activity;import android.app.AlertDialog;import android.con ...
- KVM :vnc 远程控制kvm创建虚拟机
一.vnc远程控制服务器 前期准备: 1.编辑/etc/hosts vi /etc/hosts 10.1.16.32 kvm 2.关闭防火墙 service iptables stop 3.关闭sel ...
- Asp.net之实现自定义跨域
跨域是指在浏览器的同源策略下导致前端和接口部署在不同域下导致无法直接访问的问题. 针对跨域有多种解决方案常见的有: JSNOP: 可参考Jquery实现,缺点是需要后端支持: Access-Con ...
- 【POJ - 1995】Raising Modulo Numbers(快速幂)
-->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1 B1 A2 B2 A3 B3 ......... AH ...
- python 的深浅拷贝问题
深浅拷贝概念 基本类型和引用类型数据拷贝的问题.因为基本类型的数据大小是固定的,所以他保存在栈内存中:而引用类型的数据大小不固定,因而保存在堆内存中,单引用类型在栈内存中只保存一个指向堆内存的指针. ...
- 百度AI之百度图像识别java版本使用
百度AI之百度图像识别java版本使用\ 官网 http://ai.baidu.com/ 创建应用 查看 appid,appkey,sk 下载sdk https://ai.baidu.com/sdk# ...
- [小米OJ] 10. 爬楼梯
dp 另: 小米oj上的测试样例是错的 ; ) function solution(line) { if (line == 0) return 0; if (line == 1) return 1; ...
- 使用Kubeadm创建k8s集群之部署规划(三十)
前言 上一篇我们讲述了使用Kubectl管理k8s集群,那么接下来,我们将使用kubeadm来启动k8s集群. 部署k8s集群存在一定的挑战,尤其是部署高可用的k8s集群更是颇为复杂(后续会讲).因此 ...