Java 代码
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.PdfListItem;
import com.spire.pdf.lists.PdfOrderedMarker;
import com.spire.pdf.lists.PdfSortedList;
import java.awt.*;
import java.awt.geom.Point2D;
public class List2 {
public static void main(String[] args) {
//创建PdfDocument对象
PdfDocument pdf = new PdfDocument();
//设置页边距
PdfMargins margin = new PdfMargins(60, 60, 40, 40);
//添加一页
PdfPageBase page = pdf.getPages().add(PdfPageSize.A4, margin);
//初始化x,y坐标
float x = 0;
float y = 15;
//创建画刷
PdfBrush blackBrush = PdfBrushes.getBlack();
PdfBrush orangeBrush = PdfBrushes.getOrange();
PdfBrush greenBrush = PdfBrushes.getLimeGreen();
//创建两个字体
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("微软雅黑",Font.BOLD,9),true);
PdfTrueTypeFont listFont = new PdfTrueTypeFont(new Font("微软雅黑",Font.PLAIN,9),true);
//在指定位置绘制标题
String title = "第 1 章 概 述";
page.getCanvas().drawString(title, titleFont, blackBrush, x, y);
y = y + (float) titleFont.measureString(title).getHeight();
y = y + 5;
//创建两个编号列表符号
PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.Upper_Roman, listFont);//罗马数字
PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);//阿拉伯数字
//创建一个父级列表
String parentListContent = "第一节 故事线索\n"
+ "第二节 剧情发展";
PdfSortedList parentList = new PdfSortedList(parentListContent);
parentList.setFont(listFont);
parentList.setIndent(8);
parentList.setBrush(orangeBrush);
parentList.setMarker(marker1);
//创建子列表subList_1
String subListContent_1 = "历史背景\n"
+ "人物关系\n"
+ "故事矛盾";
PdfSortedList subList_1 = new PdfSortedList(subListContent_1);
subList_1.setIndent(16);
subList_1.setFont(listFont);
subList_1.setBrush(greenBrush);
subList_1.setMarker(marker2);
//创建子列表subList_2
String subListContent_2 = "案情分析\n"
+ "线索梳理\n"
+ "证物获取\n"
+ "证人供述";
PdfSortedList subList_2 = new PdfSortedList(subListContent_2);
subList_2.setIndent(16);
subList_2.setFont(listFont);
subList_2.setBrush(greenBrush);
subList_2.setMarker(marker2);
//将subList_1设置为父列表第一个元素下的子列表
PdfListItem item_1 = parentList.getItems().get(0);
item_1.setSubList(subList_1);
//将subList_2设置为父列表第二个元素下的子列表
PdfListItem item_2 = parentList.getItems().get(1);
item_2.setSubList(subList_2);
//绘制父列表到页面指定位置
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.setBreak(PdfLayoutBreakType.Fit_Page);
textLayout.setLayout(PdfLayoutType.Paginate);
parentList.draw(page,new Point2D.Float(x,y),textLayout);
//保存文档
pdf.saveToFile("MultiLevelList.pdf");
pdf.dispose();
}
}
执行程序后,在生成的PDF文档中可查看多级列表添加效果: