How to use PDFBox to create a link that goes to *previous view*?

空扰寡人 提交于 2019-12-08 01:08:53

问题


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.

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.

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.

Question: How do I achieve this with PDFBox?

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:

Link to Acrobat screenshot


回答1:


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

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

To use the subclass,

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

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




回答2:


What you're talking about is a viewer dependant action; I don't think there is a way to do this generically but there should be ways to do this in Adobe Acrobat / Adobe Reader.

One such way is to insert a link that triggers an Action. The action could be a Javascript action and the Javascript could be relatively simple as the Acrobat Javascript API contains an "app" method called "goBack".

So, insert a link as you're doing right now. Insert not a GoTo action but a Javascript action. And set the Javascript to: "app.goBack()".

This should work in Acrobat (they have a similar example with a button form field in the Acrobat Javascript API reference. The question is whether it will work in other viewers as well and thus whether it will fulfil your business case.



来源:https://stackoverflow.com/questions/28060866/how-to-use-pdfbox-to-create-a-link-that-goes-to-previous-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!