首页 > 其他分享 >Itext5使用

Itext5使用

时间:2022-11-26 20:00:29浏览次数:77  
标签:document getFont String Itext5 Paragraph 使用 new Font

@​​TOC​​

itext版本

5.5.13.2

jdk

8

开发工具

idea2018

项目

maven


一、初始PDF

一,项目搭建

1.【File】→【New】→【Project】

Itext5使用_缓存

2.选择【maven】→【Next】

Itext5使用_缓存_02

3.设置相应项,【Next】

Itext5使用_ide_03

4.设置相应项目,【Finish】

Itext5使用_自定义_04

5.引入依赖

<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>
</dependencies>

Itext5使用_ide_05

至此项目搭建完成

二、HelloWord

创建示例并执行,结构如下:

Itext5使用_自定义_06

示例:

public class Main {

public static final String Path="results/helloworld/hello_world.pdf";

public static void main(String[] args) throws Exception{
//创建文档
File file = new File(Path);
file.getParentFile().mkdirs();

//初始化
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(Path));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}

​Paragraph​​:段落结果:

Itext5使用_ide_07

二、文本

itext中提供了Font类,对文本进行设置,包中自带了一些字体可供选择。​​相关API​

1.字体、字号设置

方法

说明

getFont(String fontname)

fontname:字体

size:字号

getFont(String fontname, float size)

示例:

public class FontMain {
public static final String Path="results/text/font.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(Path);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(Path));
document.open();
document.add(new Paragraph("1.hello World"));
//字体
Font font = FontFactory.getFont(BaseFont.COURIER_BOLD);
document.add(new Paragraph("2.hello World",font));

//字体,字号设置
Font font1 = FontFactory.getFont(BaseFont.COURIER_OBLIQUE,20);
document.add(new Paragraph("3.hello World ",font1));

//字体,字号,编号 设置
Font font2 = FontFactory.getFont(BaseFont.COURIER_OBLIQUE,20);
document.add(new Paragraph("4.hello World 你好",font2));

//关闭文档
document.close();
}
}

结果:

Itext5使用_缓存_08

​查看结果,发现设置的中文字体并没有打印出来,此处,需要我们自定义字体。​

1.1中文字体设置

方法

说明

getFont(String fontname, String encoding, boolean embedded, float size)

fontname:字体

encoding:编码方式

embedded:是否嵌入字体

size:字号

1.引入依赖

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>

示例:

Font font = FontFactory.getFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED,20);
document.add(new Paragraph("不要喷香水",font));

​STSongStd-Light​​:华文宋体

结果:

Itext5使用_缓存_09

1.2简单字形设置

​为了与genFont函数想结合运用,我将其属性归为文本字形。如有不对欢迎指正​

方法

说明

getFont(String fontname, String encoding, float size, int style)

fontname:字体

encoding:编码方式

size:字号

style:文本类型

style参数中,itext提供了粗体,斜体,下划线等设置

key

value

说明

NORMAL

0

BOLD

1

粗体

ITALIC

2

斜体

UNDERLINE

4

下划线

STRIKETHRU

8

中划线

BOLDITALIC

3

加粗并斜体

UNDEFINED

-1

DEFAULTSIZE

12

下划线并中划线

示例:

public class FontStyle {
public static final String Path="results/text/font_style.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(Path);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(Path));
document.open();
Font font1 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.UNDEFINED);
document.add(new Paragraph("-1:UNDEFINED:不要喷香水",font1));

Font font2 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.NORMAL);
document.add(new Paragraph("0:NORMAL:不要喷香水",font2));

Font font3 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.BOLD);
document.add(new Paragraph("1:BOLD:不要喷香水",font3));

Font font4 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.ITALIC);
document.add(new Paragraph("2:ITALIC:不要喷香水",font4));

Font font5 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.BOLDITALIC);
document.add(new Paragraph("3:BOLDITALIC:不要喷香水",font5));

Font font6 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.UNDERLINE);
document.add(new Paragraph("4:UNDERLINE:不要喷香水",font6));

Font font7 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.STRIKETHRU);
document.add(new Paragraph("8:STRIKETHRU:不要喷香水",font7));

Font font8 = FontFactory.getFont("STSongStd-Light","UniGB-UCS2-H", 20,Font.DEFAULTSIZE);
document.add(new Paragraph("12:DEFAULTSIZE:不要喷香水",font8));

document.close();
}
}

效果:

Itext5使用_自定义_10

1.3自定义字体

&emsp;&emsp;除了使用 FontFactory.getFont() 还可以使用BaseFont.createFont() 设置字体。

&emsp;&emsp;使用以下两种字体进行演示:

Itext5使用_缓存_11

方法

说明

createFont(String name, String encoding, boolean embedded)

name:字体路径

encoding:编码格式

embedded:是否嵌入字体

​embedded​​:

参数

Value

说明

EMBEDDED

true

嵌入字体,避免在不同的环境下打开字体异常

NOT_EMBEDDED

false

不嵌入

public class FontIdentityMain {
public static final String Path="results/text/font_identity.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(Path);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(file));
document.open();

//FontFactory.getFont()设置字体
Font font = FontFactory.getFont("font/迷你简嘟嘟体.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED,20);
document.add(new Paragraph("不要喷香水",font));

//通过BaseFont设置字体
BaseFont baseFont2 = BaseFont.createFont("font/麦田体.ttf",BaseFont.IDENTITY_V,BaseFont.NOT_EMBEDDED);
Font font2 = new Font(baseFont2,20);
document.add(new Paragraph("不要喷香水",font2));
document.close();

}
}

排列方式:​​IDENTITY_H​​ 表示文本横排 ​​IDENTITY_V​​表示文本竖排

结果:

Itext5使用_自定义_12

2.颜色设置

​itext提供了BaseColor函数对文本的演示进行设置,设置颜色的方法依然使用getFont()函数 ​

方法

说明

getFont(String fontname, String encoding, boolean embedded, float size, int style, BaseColor color)

name:字体路径

encoding:编码格式

embedded:是否嵌入字体

size:字号

style:字体样式

color:颜色

​BaseColor自带了一些颜色,同时也支持自定义颜色​

方法

说明

BaseColor(int red, int green, int blue, int alpha)

red:红色通道值

green:绿色通道值

blue:蓝色通道值

alpha:透明度,取值范围为0-255,值越大透明度越低

示例:

public class FontColor {
public static final String Path="results/text/font_color.pdf";
public static void main(String[] args) throws FileNotFoundException, DocumentException {
File file = new File(Path);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(Path));
document.open();
//中文设置
Font font = FontFactory.getFont("font/迷你简嘟嘟体.ttf",BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED,20,Font.NORMAL,BaseColor.PINK);
document.add(new Paragraph("不要喷香水",font));

//自定义颜色
Font font1 = FontFactory.getFont("font/迷你简嘟嘟体.ttf",BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED,20,Font.NORMAL,new BaseColor(171,118,50));
document.add(new Paragraph("不要喷香水",font1));

//自定义颜色,透明度,值越小,透明度越大
Font font2 = FontFactory.getFont("font/迷你简嘟嘟体.ttf",BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED,20,Font.NORMAL,new BaseColor(171,118,50,100));
document.add(new Paragraph("不要喷香水",font2));

document.close();

}
}

结果

Itext5使用_ide_13

3.字体缓存

在使用getFont()函数对文本进行设置时,会看到以下这个函数

方法

说明

getFont(String fontname, String encoding, boolean embedded, float size, int style, BaseColor color, boolean cached)

cached:true,表示将字体加入缓存;

false,表示字体总是新建(缺省时:如果字体来自缓存,则为true,如果字体是新的字体加入缓存)

参考文献:

1.ITEXT官网:​​https://itextpdf.com​

2.iText5 API documentation: ​​​​https://api.itextpdf.com/iText5/java/5.5.13.2​​​

标签:document,getFont,String,Itext5,Paragraph,使用,new,Font
From: https://blog.51cto.com/lvyq/5889146

相关文章