pdfjs
pdfjs copied to clipboard
Annotations (Highlight, Links, ...)
#41
Link Annotations are added with b1240a923e9e51493d012582724b78cbe1afae95
Are there any plans to add named destinations for links? e.g. go to page N, go to location X,Y on page N, etc.
If it helps I added support for this into the (now dead?) PDFKit but as this library is more up to date and looks like its maintained it would be good to get it added too - I don't have any time myself at the moment or I'd add it myself.
@rhodgkins I am open to adding named destinations, but I do not have the use case. Reading the PDF specification is the most time consuming part of adding a feature. So, is it a lot of work to shortly summarize what PDF operations and objects need to be added to the PDF to get named destinations to work?
@rkusa Ha - yeh I totally agree I've summarised below the things needed!
From the users perspective there's 2 types of link destinations, pages and anchors:
- Anchors - user gives a name of a destination to
.text()
method. - Pages - user just gives a page number to the
.text()
method, and that method goes and looks up the named destination for that page.
For the pages, the best idea would be to add a new named destination, with the name based on the pages object ID each time a new page is added.
For anchors there would also need to be a way in the API to create them by name and X Y value of the current page. Or making an UI item (text, image, etc.) a destination with a name.
If an anchor is used that hasn't been define as a named destination then the link just won't work - thats just user error imo!
PDF spec
In the document catalog there needs to be a Names
entry with a sub entry of Dests
which is a PDF Name Tree object which is similar to a PDF dictionary object, but has ordered entries:
nameTree = {
key2: <Page1 ref ID> 10 100
key1: <Page2 ref ID>
key3: <Page4 ref ID>
}
key1
: links to start of page 2
key2
: links to page 1 at location 10 pixels from the left and 100 pixels from the top (might need to flip the coordinate system but not sure how you've implemented this)
key3
: links to start of page 4
Each entry should be of the format:
({key}) [{page ref} /XYZ {x or null} {y or null} null]
Page ref is the standard format, e.g. 6 0 R
So:
{catalog ref}
<<
/Type /Catalog
/Pages {pages ref}
/Names {dests ref}
>>
{dests ref}
<<
/Dests <<
/Limits [(key1) (key3)]
/Names [
(key1) [{page2 ref} /XYZ null null null]
(key2) [{page2 ref} /XYZ 10 100 null]
(key3) [{page2 ref} /XYZ null null null]
]
>>
>>
Then for the links, instead of a URI link dictionary its a reference to a GoTo:
{goto for key1 ref}
<<
/S /GoTo
/D (key1)
>>
{annotation ref}
<<
/Subtype /Link
/A {goto for key1 ref}
/Type /Annot
/Rect {bounding rect of object being linked}
/Border [0 0 0]
>>
Example PDF attached with some links in
Also images could also be links, so I guess a link
option could be added to them too?
@rkusa - also as a side question, its been at alpha for v2 for a few months now - any idea when it'll go full v2?
@rhodgkins Thanks al lot for the summary! I've added it to the list of possible things to do during a long flight in the upcoming days 🙂
I am using pdfjs in production and it seems to work fine (at least for my use case), so there is probably no good reason to stick to the alpha
prefix anymore. I'll probably remove it for the next release.
@rkusa no problem, and thanks for the info about the release!
@rhodgkins Just wanted to let you know that while I have a first implementation of destinations, I found some parts of the library that need improvement before I am pushing the implementation. I hope to find the time at the end of this week. Your description and the other PR was really helpful, thanks again for that!
@rhodgkins Just wanted to let you know that destinations are ~not~ implemented in the master
branch. I'll do a release soon.
@rkusa just to clarify:
destinations are not implemented in the
master
branch
Did you mean they are implemented - I spotted them in a commit when having a look around!
Thanks again for implementing this into the library!
Oops, that not shouldn't be there. So yes they are implemented in the master
branch. Here are also some examples: https://github.com/rkusa/pdfjs/tree/master/test/pdfs/annotations
The only thing I haven't implemented is a goToPage
method, because I think this isn't too useful yet. My feeling is that I have to add some events, like onNewPage
or similar, first - otherwise a user of the library isn't always aware of what goes on which page.