我在ArrayList中有如下数据:
static ArrayList<DTONodeDetail> tree;
public static void main(String[] args) {
// TODO Auto-generated method stub
tree=new ArrayList<DTONodeDetail>();
//first argument->NodeId
//second->NodeName
// third -> ParentNodeId
tree.add(getDTO(1,"Root",0));
tree.add(getDTO(239,"Node-1",1));
tree.add(getDTO(242,"Node-2",239));
tree.add(getDTO(243,"Node-3",239));
tree.add(getDTO(244,"Node-4",242));
tree.add(getDTO(245,"Node-5",243));
displayTree(tree.get(0));
}
public static DTONodeDetail getDTO(int nodeId,String nodeName,int parentID)
{
DTONodeDetail dto=new DTONodeDetail();
dto.setNodeId(nodeId);
dto.setNodeDisplayName(nodeName);
dto.setParentID(parentID);
return dto;
}我能够在tree structure中显示上述数据,如下所示:
Root
-----Node-1
------------Node-2
------------------Node-4
------------Node-3
------------------Node-5我尝试使用itext库的章节类,但无法知道如何生成分层书签。
我的问题是,我是否可以使用itext java库在pdf中创建分层书签(如上面所示)?
发布于 2015-02-26 17:06:58
在PDF术语中,书签被称为大纲。请看我书中的CreateOutline示例,以了解如何创建大纲树,如下面的PDF:tree.pdf所示

我们从树的根开始:
PdfOutline root = writer.getRootOutline();然后我们添加一个分支:
PdfOutline movieBookmark = new PdfOutline(root,
new PdfDestination(
PdfDestination.FITH, writer.getVerticalPosition(true)),
title, true);在这个树枝上,我们添加了一片叶子:
PdfOutline link = new PdfOutline(movieBookmark,
new PdfAction(String.format(RESOURCE, movie.getImdb())),
"link to IMDB");诸若此类。
关键是在构造子大纲时使用PdfOutline并将父大纲作为参数传递。
基于注释的更新:
还有一个名为BookmarkedTimeTable的例子,其中我们以完全不同的方式创建了大纲树:
ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
outlines.add(map);在这种情况下,map是我们可以添加分支和叶子的根对象。完成之后,我们将大纲树添加到PdfStamper中,如下所示:
stamper.setOutlines(outlines);请注意,PdfStamper是我们在操作现有PDF时所需要的类(与PdfWriter相反,后者是我们从头创建PDF时使用的)。
基于另一条评论的附加更新:
要创建层次结构,只需添加孩子。
第一级:
HashMap<String, Object> calendar = new HashMap<String, Object>();
calendar.put("Title", "Calendar");第二级:
HashMap<String, Object> day = new HashMap<String, Object>();
day.put("Title", "Monday");
ArrayList<HashMap<String, Object>> days = new ArrayList<HashMap<String, Object>>();
days.add(day);
calendar.put("Kids", days);第三级:
HashMap<String, Object> hour = new HashMap<String, Object>();
hour.put("Title", "10 AM");
ArrayList<HashMap<String, Object>> hours = new ArrayList<HashMap<String, Object>>();
hours.add(hour);
day.put("Kids", hours);等等..。
https://stackoverflow.com/questions/28734490
复制相似问题