Itext5使用

itext版本5.5.13.2jdk8
开发工具idea2018项目maven

前言

  

一、初始PDF

一,项目搭建

1.【File】→【New】→【Project】
在这里插入图片描述
2.选择【maven】→【Next】
在这里插入图片描述
3.设置相应项,【Next】
在这里插入图片描述
4.设置相应项目,【Finish】
在这里插入图片描述
5.引入依赖

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

在这里插入图片描述
至此项目搭建完成

二、HelloWord

创建示例并执行,结构如下:
在这里插入图片描述

示例:

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:段落
结果:
在这里插入图片描述

二、文本

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();
    }
}

结果:
在这里插入图片描述
查看结果,发现设置的中文字体并没有打印出来,此处,需要我们自定义字体。

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:华文宋体
结果:
在这里插入图片描述

1.2简单字形设置

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

方法说明
getFont(String fontname, String encoding, float size, int style) fontname:字体
encoding:编码方式
size:字号
style:文本类型

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

keyvalue说明
NORMAL0
BOLD1粗体
ITALIC2斜体
UNDERLINE4下划线
STRIKETHRU8中划线
BOLDITALIC3加粗并斜体
UNDEFINED-1
DEFAULTSIZE12下划线并中划线

示例:

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();
    }
}

效果:在这里插入图片描述

1.3自定义字体

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

  使用以下两种字体进行演示:
在这里插入图片描述

方法说明
createFont(String name, String encoding, boolean embedded) name:字体路径
encoding:编码格式
embedded:是否嵌入字体

embedded:

参数Value说明
EMBEDDEDtrue嵌入字体,避免在不同的环境下打开字体异常
NOT_EMBEDDEDfalse不嵌入
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表示文本竖排

结果:
在这里插入图片描述

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

    }
}

结果
在这里插入图片描述

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
3.itext文档: https://itextpdf.com/sites/default/files/2021-02/itext_so.pdf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不要喷香水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值