Search code examples
javapdfpdfbox

In PDFBox, Is it possible to add a tooltip (hint) to a mark annotation?


In a Nutshell

I've been working on a program that gets a pdf, highlights some words (via pdfbox Mark Annotation obj) and saves the new pdf.

I'd like my highlighted words to show a tooltip with some small description on it, like a hint.

For instance, I want that on my pdf, the highlighted word activated shows the tooltip important word found when I stop the mouse over it.

This is the original test pdf.

My Code

With a couple of abstractions, in a nutshell, I have:

File file = new File("path/to/myfile/mypdf.pdf");

PDDocument document = PDDocument.load(file);

PDPage page = document.getPage(0);
List<PDAnnotation> annotations = page.getAnnotations();

PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
txtMark.setRectangle(pdRectangle);
txtMark.setQuadPoints(quadPoints);
txtMark.setColor(getColor());

annotations.add(txtMark);

Current Result

Currently it generates a pdf with mark annotations like below on the word activated:

enter image description here

What I Want

Now I want to add a tooltip on it, just like I have when I add a hyperlink, like showed below, but with a free text instead. I can only have a tooltip like this if this is attached to a url, even if I added the string I need, pdfbox would internally create a uri out of it...

enter image description here

FYI: this is the annotation link code:

PDAnnotationLink link = new PDAnnotationLink();
link.setAction("www.stackoverflow.com");
link.setRectangle(pdRectangle);
link.setQuadPoints(quads); annotations.add(link);

What I've Tried and Why I'm Not Satisfied Yet

1) I've tried to add an annotation link, as showed above, but with a description instead of a url, like important word found. The result isn't good, the tooltip is transformed to something like: file:///Users/myproject/root/important word found.

Also this link annotation is not the recommended way to go since in some cases I will want to have both a URL and a Tooltip. But if I could turn it around, it would be a real consideration.

2) I've tried to add a content to my mark annotation, which works like a popup, like showed below:

enter image description here

It works... when I mouse over it, my tooltip description shows up beautifully. However, you can see that bubble icon just above the word. That's the only problem with this solution, these bubbles are quite annoying and end up overlapping important part of the text and polluting the pdf. If I could hide them or something I'd be satisfied too.

here is the doc with this annotation.

And the code to add this popup was simple adding the line below:

txtMark.setContents("Important word found");

Conclusion

Any tip to either add a tooltip nicely or remove that annotation bubble will be hugely appreciated. Thanks in advance.

EDIT

As @Tilman Hausherr suggested on comments, I've added the following line to my code:

txtMark.setTitlePopup("Important word found");

Without setting the content. I don't have the annoying bubble anymore, but now I need to double click my annotation and a not much good looking or practical popup shows up:

enter image description here

This helps a little bit since it's the best I got so far.

EDIT 2

My attempt with PDAnnotationPopup: added to my code the lines below, as suggested by @Tilman:

...
PDAnnotationPopup pdAnnotationPopup = new PDAnnotationPopup();
pdAnnotationPopup.setParent(txtMark);
pdAnnotationPopup.setContents("Important word found");

// Just to make sure
pdAnnotationPopup.setInvisible(false);
pdAnnotationPopup.setNoView(false);
pdAnnotationPopup.setNoZoom(false);
pdAnnotationPopup.setLocked(false);
pdAnnotationPopup.setHidden(false);

annotations.add(pdAnnotationPopup);

I also explored other PDAnnotationPopup parameters such as setOpen, setRectangle... and tried to keep it coexisting with txtMark.setTitlePopup.

Unfortunately none of that affected my code in any way. Only when I set setOpen(true) plus setRectangle I could see something: a completely empty popup over each of my text mark annotations.


Solution

  • So I ended up going with @Tilman suggestion, adding a setTitlePopup to my mark without setting the content. In order to the annotations and their tooltips be visible through some viewers, like pdf.js, it's also needed to call the constructAppearances method:

    txtMark.setTitlePopup("Important word found");
    txtMark.constructAppearances(new PDHighlightAppearanceHandler(txtMark, pdDocument));
    

    While this is not perfect, since it requires a double click on the annotation to be displayed and it's visible only by more robust pdf readers, like Adobe, it is the best solution I could find so far and will suffice. Also it meets my expectations perfectly if you wrap the pdf with pdf.js, like showed below:

    pdf viewed with pdf.js