[#3004] - Pdf Bookmark with JasperReports calculates wrong position

Category:
Bug report
Priority:
Normal
Status:
Resolved
Project: Severity:
Minor
Resolution:
Fixed
Component: Reproducibility:
Always
Assigned to:
0

The position of the bookmarks are calculated with the wrong position. They use relative position of the text elements instead of absolute position on a page.

I am using the anchorNameExpression-tag and the bookmarkLevel-attribute on textField elements which automatically create bookmark in pdf when the jasperPrint is exported as pdf.

What I do is the following:

I got a jrxml which contains the following elements

- textField1 (position x=10, y=10)
- a frame2 (position x= 50, y=50), which contains itself a textField2 (position x=5, y=5)

I have added the following anchors in jrxml:

- textField1: anchorName="bookmark1", bookmarkLevel=1
- textField2: anchorName="bookmark2", bookmarkLevel=1

What happens is that the bookmarks in pdf will have the following positions:

bookmark1: position x=10, y=10
bookmark2: position x=5, y=5

-> the bookmarks should have the absolut position on the page, calculated with all its surrounding container:

bookmark2: position x=55 ( sum of the x-position of the frame and the x-position of the textField2). Same for y-position

v5.1.0
Neobye's picture
15
Joined: May 6 2013 - 6:01am
Last seen: 1 day 23 hours ago

13 Comments:

#1
  • Assigned:nobody»

I guess that I found the problem:

The method JRPdfExporter.setAnchor(Chunk, JRPrintAnchor, JRPrintElement) uses the x- and y-position of the JRPrintElement (which is a relative position) for creating the anchor.
The method signature should be enhanced with the arguments which should be used then in the call of method addBookmark(anchor.getBookmarkLevel(), anchor.getAnchorName(), bookmarkPosX, bookmarkPosY):
- int bookmarkPosX
- int bookmarkPosY

In the method PdfTextRenderer.draw() they values for leftX and minY are already calculated and should be given as arguments to the JRPdfExporter.getPhrase(...) which will then call the setAnchor(...) whith those arguments.

I have attached 2 fixed files (JasperReport 5.1.0), where I implemented my bugfix. I will test it now in my environment

AttachmentSize
File pdftextrenderer.java4.14 KB
File jrpdfexporter.java73.38 KB
File jrpdfexporter.java73.38 KB
#2
  • Assigned:» anonymous

Sorry, the previous attachments wont work, here are the correct files

AttachmentSize
File jrpdfexporter.java73.38 KB
#3
AttachmentSize
File pdftextrenderer.java4.16 KB
#4
AttachmentSize
File simplepdftextrenderer.java4.38 KB
#5

I think changing the addBookmark call in JRPdfExporter.setAnchor to addBookmark(anchor.getBookmarkLevel(), anchor.getAnchorName(), element.getX() + getOffsetX(), element.getY() + getOffsetY()); might do. Could you test this solution and let us know if it's fine as far as you are concerned?

#6

I was looking for some offsetX and offsetY before...
Where do I get the method getOffsetX() and getOffsetY() from?
I'm using jasperReports 5.1.0. Is this part of 5.1.2 ?

#7

I tried to correctly fix the code for bookmark positioning in pdf. Attached is an zip file that contains a sample report that uses the typical problematic elements like frame, table, subreport.
A output pdf is attached as well that contains the generated output with the correct bookmarks.

But I had to calculate the bookmark positions outside the JRPdfExporter in the class PdfTextRenderer and SimplePdfTextRenderer, because inside the JRPdfExporter I don't have the necessary information to calculate the offsetX and offsetY.

Please check if the proposed bugfix is acceptable.
Regards

AttachmentSize
Package icon bugfix_for_issue3004.zip23.74 KB
#8

The getOffset methods are defined in JRAbstractExporter, which JRPdfExporter extends.

#9

Your test report works fine (as far as I can tell) with getOffsetX() and getOffsetY(). Could you confirm that?

#10

I tested with getOffsetX() and getOffsetY() as you proposed.
It seems to work more or less, but the x and y position are not 100% correctly calculated.
For example within tables (bookmark4 in my sample) the y position is wrong, and for bookmark5 the x-position is not absolutely correct.

Just have a look at my example when you look at it with zoom 2400% in AcrobatReader.

#11
  • Assigned:anonymous» teodord

Hi,

I took the PdfBookmarkPositionTest.pdf file you attached and the bookmarks seem to be placed correctly. I think that if the zoom is not big enough, the repositioning of the page to the bookmark anchor cant be performed because the PDF viewer will always try to keep the edge of the paper attached to the viewer edge. Simply put, the page will no longer move it the scrollbar has already reached the scroll limit. I suspect this is what you experienced when you said that starting with bookmark 5 the x position is wrong. Try again with heigher zoom and pay attention to where the scroll is.

This is fixed now and will be part of the next release.

Thank you,
Teodor

#12
  • Resolution:Open» Fixed
  • Status:New» Resolved
#13

Hy Teodor,

Thank's for your reply. I know what you mean with the scrollbar and the zoom factor. I just verified it and saw the effect you described.
But I don't really remember, if the attached PdfBookmarkPositionTest.pdf was produced with my code changes or not.
To verify the correct bugfix, you should create a pdf with my attached jrxml and see if the bookmarks are still correct.

regards

Feedback