使用iText库生成PDF格式的分层书签 [英] Hierarchical Bookmark Generation in PDF using iText library

查看:332
本文介绍了使用iText库生成PDF格式的分层书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ArrayList 中的数据,如下所示:

  static ArrayList< DTONodeDetail>树; 
public static void main(String [] args){
// TODO自动生成的方法stub
tree = new ArrayList< DTONodeDetail>();

//第一个参数 - > NodeId
// second-> NodeName
//第三个 - > 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);

返回dto;
}

我能够在树结构中显示以上数据如下所示:

  Root 
----- Node-1
------------ Node-2
------------------ Node-4
----- ------- Node-3
------------------ Node-5

我尝试使用类的itext库,但无法知道如何生成分层书签。



我的问题是我可以使用 itext java库在pdf中创建分层书签(如上所述)吗?

解决方案

在PDF术语中,书签被称为轮廓。请从我的书中查看并在构建子大纲时将父大纲作为参数传递。



根据评论更新



还有一个名为 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(轮廓); 

请注意 PdfStamper 是我们需要的课程当我们操纵现有的PDF(而不是 PdfWriter ,当我们从头开始创建PDF时使用它)。



基于另一条评论的其他更新:



要创建层次结构,您只需要添加孩子。



第一级:

  HashMap< String,Object> calendar = new HashMap< String,Object>(); 
calendar.put(标题,日历);

第二级:

  HashMap< String,Object> day = new HashMap< String,Object>(); 
day.put(标题,星期一);
ArrayList< HashMap< String,Object>> days = new ArrayList< HashMap< String,Object>>();
days.add(day);
calendar.put(孩子们,天);

第三级:

  HashMap< String,Object> hour = new HashMap< String,Object>(); 
hour.put(标题,上午10点);
ArrayList< HashMap< String,Object>> hours = new ArrayList< HashMap< String,Object>>();
hours.add(小时);
day.put(孩子们,小时);

依此类推......


I have data in ArrayList like below :

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;
}

I am able to display above data in tree structure like below :

Root
-----Node-1
------------Node-2
------------------Node-4
------------Node-3
------------------Node-5

I have try Using Chapter class of itext library but unable to know how can i generate hierarchical bookmark.

My Question is can i create hierarchical bookmark(like above) in pdf using itext java library?

解决方案

In PDF terminology, bookmarks are referred to as outlines. Please take a look at the CreateOutline example from my book to find out how to create an outline tree as shown in this PDF: outline_tree.pdf

We start with the root of the tree:

PdfOutline root = writer.getRootOutline();

Then we add a branch:

PdfOutline movieBookmark = new PdfOutline(root, 
            new PdfDestination(
                PdfDestination.FITH, writer.getVerticalPosition(true)),
            title, true);

To this branch, we add a leaf:

PdfOutline link = new PdfOutline(movieBookmark,
            new PdfAction(String.format(RESOURCE, movie.getImdb())),
            "link to IMDB");

And so on.

The key is to use PdfOutline and to pass the parent outline as a parameter when constructing a child outline.

Update based on comment:

There's also an example called BookmarkedTimeTable where we create the outline tree in a completely different way:

ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
outlines.add(map);

In this case, map is the root object to which we can add branches and leaves. Once we're finished, we add the outline tree to the PdfStamper like this:

stamper.setOutlines(outlines);

Note that PdfStamper is the class we need when we manipulate an existing PDF (as opposed to PdfWriter which is to be used when we create a PDF from scratch).

Additional update based on yet another comment:

To create a hierarchy, you just need to add kids.

First level:

HashMap<String, Object> calendar = new HashMap<String, Object>();
calendar.put("Title", "Calendar");

Second level:

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);

Third level:

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);

And so on...

这篇关于使用iText库生成PDF格式的分层书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆