itextpdf生成列表基本用法

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

在上一篇文章使用itextpdf生成表格基本用法中,介绍了生成表格的基本步骤和一些常用设置,同样的,在某些需求下,我们需要在pdf中展示列表,体现条理性,itextpdf同样支持,这个开源库支持很多种列表风格来满足大家需求,由于列表的每项风格基本一致,所以使用起来也非常简单,这里介绍几个常用的

1、有序列表

	/**
	 * 添加有序列表
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createOrderedListPdf() throws IOException, DocumentException {
		Document document = new Document();
		// 创建PdfWriter对象
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		// 打开文档
		document.open();
		// 添加有序列表
		List orderedList = new List(List.ORDERED);
		orderedList.add(new ListItem("Item 1"));
		orderedList.add(new ListItem("Item 2"));
		orderedList.add(new ListItem("Item 3"));
		document.add(orderedList);
		// low level
		PdfContentByte cb = writer.getDirectContent();
		cb.fill();
		cb.sanityCheck();
		// 关闭文档
		document.close();
	}

这个List是itext包下面的,表示一个列表,不是java常用的那个List,代码非常简单,就不作多的解释了,附上效果图:

2、无序列表

	/**
	 * 添加无序列表
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createUnorderedListPdf() throws IOException, DocumentException {
		Document document = new Document();
		// 创建PdfWriter对象
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		// 打开文档
		document.open();
		// 添加无序列表
		List unorderedList = new List(List.UNORDERED);
		unorderedList.add(new ListItem("Item 1"));
		unorderedList.add(new ListItem("Item 2"));
		unorderedList.add(new ListItem("Item 3"));
		document.add(unorderedList);
		// low level
		PdfContentByte cb = writer.getDirectContent();
		cb.fill();
		cb.sanityCheck();
		// 关闭文档
		document.close();
	}

 

3、罗马数字列表(RomanList)

	/**
	 * 添加罗马数字列表
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createRomanListPdf() throws IOException, DocumentException {
		Document document = new Document();
		// 创建PdfWriter对象
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		// 打开文档
		document.open();
		// 添加roman列表
		RomanList romanList = new RomanList();
		romanList.add(new ListItem("Item 1"));
		romanList.add(new ListItem("Item 2"));
		romanList.add(new ListItem("Item 3"));
		document.add(romanList);
		// low level
		PdfContentByte cb = writer.getDirectContent();
		cb.fill();
		cb.sanityCheck();
		// 关闭文档
		document.close();
	}

4、希腊字母列表(GreekList) 

	/**
	 * 添加希腊字母列表
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createGreekListPdf() throws IOException, DocumentException {
		Document document = new Document();
		// 创建PdfWriter对象
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		// 打开文档
		document.open();
		// Add Greek list
		GreekList greekList = new GreekList();
		greekList.add(new ListItem("Item 1"));
		greekList.add(new ListItem("Item 2"));
		greekList.add(new ListItem("Item 3"));
		document.add(greekList);
		// low level
		PdfContentByte cb = writer.getDirectContent();
		cb.fill();
		cb.sanityCheck();
		// 关闭文档
		document.close();
	}

5、ZapfDingbats列表

	/**
	 * 创建ZapfDingbats列表
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createZapfDingbatsListPdf() throws IOException, DocumentException {
		Document document = new Document();
		// 创建PdfWriter对象
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		// 打开文档
		document.open();
		// ZapfDingbatsList List Example
		ZapfDingbatsList zapfDingbatsList = new ZapfDingbatsList(43, 30);
		zapfDingbatsList.add(new ListItem("Item 1"));
		zapfDingbatsList.add(new ListItem("Item 2"));
		zapfDingbatsList.add(new ListItem("Item 3"));
		document.add(zapfDingbatsList);
		// low level
		PdfContentByte cb = writer.getDirectContent();
		cb.fill();
		cb.sanityCheck();
		// 关闭文档
		document.close();
	}

 

6、列表嵌套

	/**
	 * 创建嵌套列表
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void createNestedListPdf() throws IOException, DocumentException {
		Document document = new Document();
		// 创建PdfWriter对象
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		// 打开文档
		document.open();
		// List and Sublist Examples
		List nestedList = new List(List.UNORDERED);
		nestedList.add(new ListItem("Item 1"));
		// 子列表
		List sublist = new List(true, false, 30);
		sublist.setListSymbol(new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 6)));
		sublist.add("A");
		sublist.add("B");
		nestedList.add(sublist);
		nestedList.add(new ListItem("Item 2"));
		// 子列表
		sublist = new List(true, false, 30);
		sublist.setListSymbol(new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 6)));
		sublist.add("C");
		sublist.add("D");
		nestedList.add(sublist);
		document.add(nestedList);
		// low level
		PdfContentByte cb = writer.getDirectContent();
		cb.fill();
		cb.sanityCheck();
		// 关闭文档
		document.close();
	}

 

上面列举的都是比较常用的列表,在一般项目已经够使用了 ,更多扩展功能请参考itextpdf官网API。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
iText是一个用于生成PDF文件的Java库。在使用iText生成PDF文件时,你需要使用一些常用的类和方法。其中,最常用的类是com.itextpdf.text.Document,它代表了一个PDF实例。你可以通过创建一个Document实例,然后打开、添加内容,最后关闭该实例来生成一个PDF文件。另外,还有一些其他常用的类,如com.itextpdf.text.Paragraph表示一个缩进的文本段落,com.itextpdf.text.Chapter表示PDF的一个章节,com.itextpdf.text.Font包含了所有规范好的字体,com.itextpdf.text.List表示一个列表,com.itextpdf.text.Anchor表示一个锚等等。此外,还有一些用于读取和修改PDF文件的类,如com.itextpdf.text.pdf.PdfReader和com.itextpdf.text.pdf.PdfWriter。你可以使用PdfReader读取PDF文件,使用PdfWriter将内容写入到PDF文件中。下面是一个使用iText生成简单PDF文件的示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class TestPDFDemo { public static void main(String[] args) throws FileNotFoundException, DocumentException { // 创建一个新的Document对象 Document document = new Document(); // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test.pdf")); // 打开文档 document.open(); // 添加一个内容段落 document.add(new Paragraph("Hello World!")); // 关闭文档 document.close(); } } ``` 这段代码会创建一个名为"test.pdf"的PDF文件,并在其中添加一个内容为"Hello World!"的段落。你可以根据自己的需求修改代码来生成不同的PDF文件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

u010142437

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

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

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

打赏作者

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

抵扣说明:

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

余额充值