如何使用PDFBox创建转到“以前的视图"的链接? [英] How to use PDFBox to create a link that goes to *previous view*?

查看:95
本文介绍了如何使用PDFBox创建转到“以前的视图"的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用PDFBox,通过使用PDPageDestination可以轻松地创建指向特定页面或页面视图的链接.例如,以下代码将链接到第9页:

By using PDFBox, it is easy to create a link that goes a particular page or page view by using PDPageDestination. For example, the following code will make a link that goes to page 9:

PDAnnotationLink link         = new PDAnnotationLink();
PDPageDestination destination = new PDPageFitWidthDestination();
PDActionGoTo action           = new PDActionGoTo();

destination.setPage(document.getPage(9));
action.setDestination(destination);
link.setAction(action);

问题:
除了转到特定页面之外,我想转到上一个视图.

Problem:
Instead of going to a particular page, I would like to go to the previous view.

例如,假设在一个PDF文件中,P.1和P.2中的每个都有一个指向P.9的链接.现在,我想在P.9上放一个返回到用户位置的链接.开始.

For example, suppose in a PDF file, each of P.1 and P.2 has a link that goes to P. 9. Now I would like to put on P. 9 a link that goes back to where the user started.

如果用户从P.1开始并单击了指向P.9的链接,则他将到达P.9.当他单击P.9上的链接时,他将回到他来自的P.1.但是,如果他从P.2开始,那么P.9的链接将转回P.2.

If the user started out at P.1 and clicked the link to P.9, he arrives at P.9. When he clicks the link on P. 9, he will go back to P.1, where he came from. But If he started out at P.2, then the link at P.9 will go back to P.2 instead.

问题:如何使用PDFBox实现此目的?

Question: How do I achieve this with PDFBox?

FYI,使用Adobe Acrobat,可以通过在链接中添加执行菜单项"操作,然后选择上一个视图"作为菜单项来实现,如以下屏幕截图所示:

FYI, with Adobe Acrobat, this can be achieved by adding an "execute a menu item" action to a link, and then choosing "Previous View" as the menu item, as shown in this screenshot:

链接到Acrobat屏幕截图

推荐答案

在蒂尔曼(Tilman)的指导下,我设法解决了自己的问题.

With the guidance of Tilman, I managed to solve my own problem.

我找不到允许我添加命名动作"的功能的PDAction子类,因此我创建了自己的子类"PDActionNamed":

I cannot find a PDAction subclass that gives me the capability to add a "named action", so I created my own subclass, "PDActionNamed":

class PDActionNamed extends PDAction {

    public static final String SUB_TYPE = "Named";    

    public PDActionNamed() {
        super();
        setSubType( SUB_TYPE );
    }

    public void setN( String s ) {
        action.setName( "N", s );
    }
}

要使用子类,

PDAnnotationLink link   = new PDAnnotationLink(); 
PDActionNamed action = new PDActionNamed ();
action.setN("GoBack");    // this is one of Acrobat's default named action
link.setAction(action);

即使在不支持Java的PDF阅读器(例如SumatraPDF)上,它似乎也可以正常工作.

It seems to work even on non-Javascript-supported PDF readers (e.g. SumatraPDF).

这篇关于如何使用PDFBox创建转到“以前的视图"的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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