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

这篇文章,主要介绍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表格。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用iTextPDF库中的`PdfPCell`类来添加图片。下面是一个示例代码,演示如何在`PdfPCell`中添加图片: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); PdfPTable table = new PdfPTable(1); // 创建一个 PdfPCell PdfPCell cell = new PdfPCell(); // 读取图片 Image image = Image.getInstance("path/to/image.jpg"); // 设置图片宽度和高度 image.scaleAbsolute(200, 200); // 将图片添加PdfPCell 中 cell.addElement(image); // 将 PdfPCell 添加表格table.addCell(cell); // 将表格添加文档中 document.add(table); document.close(); } catch (DocumentException | IOException e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们首先创建了一个`PdfPTable`对象,然后创建一个`PdfPCell`对象。然后,我们使用`Image.getInstance()`方法读取图片文件,并使用`scaleAbsolute()`方法设置图片的宽度和高度。 最后,我们将图片添加到`PdfPCell`中,再将`PdfPCell`添加表格中,最终将表格添加文档中。请确保将`"path/to/image.jpg"`替换为您实际的图片路径。运行代码后,将会生成一个名为`output.pdf`的PDF文件,其中包含了添加图片的单元格。 请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值