0

I am using following code to set external hyperlink using itext library in Java.

Chunk chunk = new Chunk("Click to Open File");

PdfAction action = new PdfAction("externalfile.pdf");
action.put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE);
action.put(PdfName.ZOOM, PdfName.FIT);
chunk.setAction(action);

I want to set zoom level of external hyper link: when I click on hyper link file should be open and FIT page.

I tried using action.put(PdfName.ZOOM, PdfName.FIT); but it's not working.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61

1 Answers1

2

Please don't ever create PDF object manually without consulting ISO-32000-1.

You want to create a GoToR action. Such an action is expressed as a PDF dictionary that can contain the following keys:

ISO-32000-1 Table 200

There is no key named Zoom in that table, hence your code is wrong.

You need the D key and as you want to link to a page and define a zoom factor, you need to define a destination:

ISO-32000-1 Table 151

In other words, the destination needs to be a PdfArray! PdfName.FIT isn't sufficient!

(All screen shots are taken from the copy of ISO-32000-1 that is provided by Adobe on its web site.)

Update:

If you want to add a link to a remote page, you can also follow the example on page 197-198 of iText in Action - Second Edition: see the LinkActions example that uses the gotoRemotePage() method.

Internally, this method looks like this:

public static PdfAction gotoRemotePage(String filename, String dest, boolean isName, boolean newWindow) {
    PdfAction action = new PdfAction();
    action.put(PdfName.F, new PdfString(filename));
    action.put(PdfName.S, PdfName.GOTOR);
    if (isName)
        action.put(PdfName.D, new PdfName(dest));
    else
        action.put(PdfName.D, new PdfString(dest, PdfObject.TEXT_UNICODE));
    if (newWindow)
        action.put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE);
    return action;
}

Note that this assumes that you have a named destination in the target file.

I think that you'd rather want to use the constructor that takes a page number:

public PdfAction(String filename, int page) {
    put(PdfName.S, PdfName.GOTOR);
    put(PdfName.F, new PdfString(filename));
    put(PdfName.D, new PdfLiteral("[" + (page - 1) + " /FitH 10000]"));
}

Of course, this doesn't use PdfName.FIT. If you really want to define the destination yourself, you need a line that looks like this:

put(PdfName.D, new PdfLiteral("[" + (page - 1) + " /Fit]"));
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I have tried this chunk.setAction(PdfAction.gotoRemotePage(filepath,"1", false, true)); – Butani Vijay Apr 14 '15 at 08:33
  • That's indeed easier than trying to construct the `PdfAction` *manually*. I'll add a pointer to the official documentation where the method is described. – Bruno Lowagie Apr 14 '15 at 08:37
  • I don't think you understand what you're doing. If "1" is a named destination in your target PDF, you know what you're doing. If "1" is a page number, then you haven't read my answer. – Bruno Lowagie Apr 14 '15 at 08:44
  • I have updated my answer once more to show why you aren't interpreting the API documentation correctly. – Bruno Lowagie Apr 14 '15 at 08:47