iTextPDF hyperlink not linking to the right place

吃可爱长大的小学妹 提交于 2019-12-11 06:58:45

问题


I have a bunch of PDF's which I have merged by this point in the code. At the beginning of the merged PDF I have a contents page which links to said PDF's respectively. These pdfAction.gotoLocalPage links sometimes don't work correctly and instead jump to between the bottom of the page and the next, however the PDF bookmark hyperlinks always work fine.

The code for the bookmark:

int pageToLinkTo=prevSectionPageCount+sectionPageCount+numberOfIndexPages+currentIndexPage+1;

document.put("Title", documentName);
document.put("Action", "GoTo");                 
document.put("Page",String.format("%d Fit", pageToLinkTo));

The code for the contents page link:

PdfAction action = PdfAction.gotoLocalPage(pageToLinkTo, new PdfDestination(PdfDestination.FIT,-1,-1,0), stamper.getWriter());                              
chunk.setAction(action);

Both of these evaluate to the same page. Could there be something wrong with the source PDF files? The only notable difference between the links which do work and the links that jump to the wrong place, is that the source PDF's have a slightly different page size (0.1 of an inch different).

Any help would be appreciated!

Thanks


回答1:


I see that you create your destination like this:

new PdfDestination(PdfDestination.FIT,-1,-1,0)

This is a strange way to create a destination so that the page is displayed to fit the viewer window. Please take a look at The ABC of PDF with iText. The book isn't finished yet, but it's free and in table 3.7, you can see which destinations take how many parameters.

If you want the page to fit the viewer window, you don't need any extra parameters:

new PdfDestination(PdfDestination.FIT)

There is a destination that takes three extra parameters:

new PdfDestination(PdfDestination.XYZ, x, y, z)

In this case x and y are coordinates and z is the zoom factor. I think that you are confusing the PDF viewer by adding x, y and z parameters when all you want it to fit the page in the viewer window.



来源:https://stackoverflow.com/questions/30378537/itextpdf-hyperlink-not-linking-to-the-right-place

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