开发者

iText having issues with Anchors (clickable but nothing happens)

开发者 https://www.devze.com 2023-02-02 02:24 出处:网络
I\'m trying to create a simple table of contents (the document is only 4 pages long). The issue I have is that while my mouse does turn into a hand, when I click it nothing happens.

I'm trying to create a simple table of contents (the document is only 4 pages long). The issue I have is that while my mouse does turn into a hand, when I click it nothing happens. And yes the targets are on another page.

creation of a table of contents line:

Chunk chunk = new Chunk("Contact information");
chunk.setLocalGoto("Contact information");  
document.add(new P开发者_运维问答aragraph(chunk));

One of the targets:

Anchor anchor = new Anchor("Contact information", font1);
anchor.setName("Contact information");
Chapter chapter = new Chapter(new Paragraph(anchor), 1);    
chapter.setNumberDepth(0);
document.add(chapter);

The Goto String matches with the Anchor name so I don't see what I'm doing wrong.


In this example from iText in Action, the internal link uses a # in the name.

Another approach would be to use Chunks for both the link and the destination.

chunkDest.setLocalDesitination("foo");
...
chunkLink.setLocalGoto("foo"); // or "#foo"?

My reading of PdfDocument (localGoto and localDestination) leads me to believe that the order in which they're created doesn't matter... wait... Nope, shouldn't matter so long as both are actually called.

Have you actually stepped through your code to be sure they're both Actually Called?

Another option: End run. Drop down to the PDF-native code and do it there. Build your own PdfDestination for the chapter location and PdfAction for the TOC. Something like this:

PdfDestination fitH = new PdfDestination(PdfDestination.FITH);
// the destination doesn't have a page associated with it until you call
//  gotoLocalPage.  Kinda goofy, but we can work with it.
PdfAction link = PdfAction.gotoLocalPage(pageNum, fitH, writer);
chunk.setAction(link);

NOTES:

  1. You can reuse a given PdfAction if you need multiple links to the same spot.
  2. There are many ways to define a PdfDestination, I just used the one I prefer. YMMV.


Looking at the example here: ftp://ns.tnet.dp.ua/pub/ORACLE/Developers/Java_Doc_LIB/PDFLib/iText/tutorial/ch03.html it looks like for internal links you need to set the reference to be "#" + {anchor name}.

Example internal link:

Anchor anchor1 = new Anchor("This is an internal link");
anchor1.setName("link1");
Anchor anchor2 = new Anchor("Click here to jump to the internal link");
anchor.setReference("#link1"); 


Also if several years are passed since someone made this question, I will answer because yesterday I had more or less the same issue

This is my code:

//the destination anchor
Anchor target = new Anchor("a name", FONT_BOLD);
target.setName("link");
Paragraph p = new Paragraph(target);    

//the goto anchor
Anchor goto= new Anchor("go to the target "+i, FONT_BOLD);
goto.setReference("#link");

Also this piece of code doesn't work, the reason is that you cannot create a new Paragraph passing an Anchor as parameter

Paragraph p = new Paragraph(target); //this will not work


Paragraph p = new Paragraph(); //this will work
p.add(target)
0

精彩评论

暂无评论...
验证码 换一张
取 消