【itext7】itext7操作PDF文档之添加段落文本内容、添加List列表、添加Image图片、添加Table表格

这篇文章,主要介绍itext7操作PDF文档之添加段落文本内容、添加List列表、添加Image图片、添加Table表格。

目录

一、itext7操作PDF内容

1.1、添加段落文本内容

1.2、添加列表内容

1.3、添加图片

1.4、添加表格

(1)列宽采用点单位(pt点单位)

(2)采用百分比单位(%百分比)


一、itext7操作PDF内容

1.1、添加段落文本内容

itext中将文本抽象为一个Text对象,这个Text属于叶子元素,不能直接添加到Document里面,必须先放入布局元素(layout元素)里面,然后再将布局元素加入到Document中。itext中采用Paragraph类表示段落,这是对一个段落文字的描述,例如:将Text对象先添加到Paragraph段落对象中,然后将Paragraph段落加入到Document里面。


package itext.demo.basic;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;

import java.io.FileNotFoundException;

/**
 * @version 1.0.0
 * @Date: 2023/7/19 15:40
 * @Author ZhuYouBin
 * @Description: 文本内容操作
 */
public class TextOperation {
    public static void main(String[] args) throws FileNotFoundException {
        // 创建PDF文档
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-content.pdf"));
        // 创建文档对象
        Document document = new Document(pdfDocument);
        // 创建文本对象
        Text text = new Text("hello world");
        // 创建段落
        Paragraph paragraph = new Paragraph();
        paragraph.add(text);
        // 将段落添加到文档上面
        document.add(paragraph);
        // 关闭文档
        document.close();
        pdfDocument.close();
    }
}

运行结果如下所示:

1.2、添加列表内容

itext中使用List类表示列表对象,列表可以有序列表、无序列表,列表中的每一项使用ListItem类表示,一个List列表可以包含多个ListItem列表项,List列表可以设置缩进、列表项的符号等。


package itext.demo.basic.text;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;

import java.io.FileNotFoundException;

/**
 * @version 1.0.0
 * @Date: 2023/7/19 15:40
 * @Author ZhuYouBin
 * @Description: 添加列表内容
 */
public class TextOperation {
    public static void main(String[] args) throws FileNotFoundException {
        // 创建PDF文档
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-content.pdf"));
        // 创建文档对象
        Document document = new Document(pdfDocument);
        // 创建List列表对象
        List list = new List();
        list.setSymbolIndent(12); // 设置列表项和符号之间的缩进距离
        list.setListSymbol("@"); // 设置列表项的符号
        // 创建列表项
        for (int i = 0; i < 5; i++) {
            list.add(new ListItem("this is 00" + i + " item。"));
        }
        // 将List列表添加到文档上面
        document.add(list);
        // 关闭文档
        document.close();
        pdfDocument.close();
    }
}

运行结果如下所示:

1.3、添加图片

itext中将图片抽象成一个Image对象,图片可以从URL、File等来源进行创建,Image类中的构造方法是protected修饰的,所以不能直接使用new关键字进行创建对象,可以使用itext中提供的ImageDataFactory工具类,这个类中提供了一个create()方法可以根据不同的来源创建图片对象。


package itext.demo.basic.text;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.net.URL;

/**
 * @version 1.0.0
 * @Date: 2023/7/19 15:40
 * @Author ZhuYouBin
 * @Description: 添加图片
 */
public class TextOperation {
    public static void main(String[] args) throws Exception {
        // 创建PDF文档
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-content.pdf"));
        // 创建文档对象
        Document document = new Document(pdfDocument);
        // 创建图片对象
        URL url = new URL("https://www.toopic.cn/public/uploads/small/1658043292312165804329268.png");
        Image image = new Image(ImageDataFactory.create(url));
        image.setAutoScale(true); // 设置宽高字段缩放

        URL url2 = new URL("https://www.toopic.cn/public/uploads/small/1658043887555165804388773.jpg");
        Image image2 = new Image(ImageDataFactory.create(url2));
        image2.setAutoScale(true); // 设置宽高字段缩放
        // 将图片添加到文档上面
        document.add(image);
        document.add(image2);
        // 关闭文档
        document.close();
        pdfDocument.close();
    }
}

运行结果如下所示(添加两张图片的效果):

1.4、添加表格

itext中将表格抽象成了Table类,表格就是一张二维表,由行和列组成,其中每一行每一列都是一个单元格,单元格使用Cell类表示。创建Table对象的时候,对应的构造方法必须指定表格中每一个单元格的宽度,列宽度的单位可以是pt、也可以设置百分比,推荐使用百分比单位。

(1)列宽采用点单位(pt点单位)


package itext.demo.basic.text;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;

/**
 * @version 1.0.0
 * @Date: 2023/7/19 15:40
 * @Author ZhuYouBin
 * @Description: 添加表格【pt单位】
 */
public class TableDemo {
    public static void main(String[] args) throws Exception {
        // 创建PDF文档
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-table.pdf"));
        // 创建文档对象
        Document document = new Document(pdfDocument);
        // 创建表格
        float[] columnWidths = new float[] {
                30, 50, 60, 20
        };
        Table table = new Table(columnWidths);
        // 设置表格宽度100%
        table.setWidth(UnitValue.createPercentValue(100));
        // 设置表格标题
        table.setCaption(new Div().add(new Paragraph("this is a caption of table")));
        // 添加表头单元格,上面设置了四列,超过四列会自动换行
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        // 添加普通单元格
        table.addCell(new Cell().add(new Paragraph("cell")));
        table.addCell(new Cell().add(new Paragraph("cell")));
        table.addCell(new Cell().add(new Paragraph("cell")));
        table.addCell(new Cell().add(new Paragraph("cell")));
        // 添加表尾单元格
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        // 添加表格到PDF文档
        document.add(table);
        // 关闭文档
        document.close();
        pdfDocument.close();
    }
}

运行结果如下所示:

(2)采用百分比单位(%百分比)


package itext.demo.basic.text;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;

/**
 * @version 1.0.0
 * @Date: 2023/7/19 15:40
 * @Author ZhuYouBin
 * @Description: 添加表格【百分比单位】
 */
public class TableDemo {
    public static void main(String[] args) throws Exception {
        // 创建PDF文档
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-table.pdf"));
        // 创建文档对象
        Document document = new Document(pdfDocument);
        // 创建百分比单位的列宽度
        UnitValue[] columnWidths = new UnitValue[] {
                UnitValue.createPercentValue(25),
                UnitValue.createPercentValue(25),
                UnitValue.createPercentValue(25),
                UnitValue.createPercentValue(25)
        };
        Table table = new Table(columnWidths);
        // 设置表格宽度100%
        table.setWidth(UnitValue.createPercentValue(100));
        // 设置表格标题
        table.setCaption(new Div().add(new Paragraph("this is a caption of table")));
        // 添加表头单元格,上面设置了四列,超过四列会自动换行
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
        // 添加普通单元格
        table.addCell(new Cell().add(new Paragraph("cell")));
        table.addCell(new Cell().add(new Paragraph("cell")));
        table.addCell(new Cell().add(new Paragraph("cell")));
        table.addCell(new Cell().add(new Paragraph("cell")));
        // 添加表尾单元格
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
        // 添加表格到PDF文档
        document.add(table);
        // 关闭文档
        document.close();
        pdfDocument.close();
    }
}

运行结果如下所示:

到此,itext操作PDF内容之添加段落、列表、图片、表格就介绍完啦。

综上,这篇文章结束了,主要介绍itext7操作PDF文档之添加段落文本内容、添加List列表、添加Image图片、添加Table表格。

猜你喜欢

转载自blog.csdn.net/qq_39826207/article/details/131840016