在 PDFBox 中,是否可以向标记注释添加工具提示(提示)? [英] In PDFBox, Is it possible to add a tooltip (hint) to a mark annotation?

查看:57
本文介绍了在 PDFBox 中,是否可以向标记注释添加工具提示(提示)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之

我一直在开发一个程序,该程序可以获取 pdf、突出显示一些单词(通过 pdfbox Mark Annotation obj)并保存新的 pdf.

我希望突出显示的字词显示带有一些小说明的工具提示,例如提示.

例如,我希望在我的 pdf 上,当我将鼠标停在它上面时,突出显示的词 activated 会显示工具提示 important word found.

我想要的

现在我想在上面添加一个工具提示,就像我添加超链接时一样,如下所示,但用自由文本代替.如果将它附加到 url,我只能有这样的工具提示,即使我添加了我需要的字符串,pdfbox 也会在内部创建一个 uri...

仅供参考:这是注释链接代码:

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

我尝试了什么以及为什么我不满意

1) 我尝试添加注释链接,如上所示,但使用说明而不是网址,例如找到重要词.结果不好,工具提示变成了类似的东西:file:///Users/myproject/root/important word found.

此外,不推荐使用此链接注释,因为在某些情况下,我希望同时拥有 URL 和工具提示.但如果我能扭转局面,那将是一个真正的考虑.

2) 我尝试在我的标记注释中添加内容,其工作方式类似于弹出窗口,如下所示:

它有效...当我将鼠标悬停在它上面时,我的工具提示描述会显示得很漂亮.但是,您可以在单词上方看到气泡图标.这是此解决方案的唯一问题,这些气泡非常烦人,最终会重叠文本的重要部分并污染 pdf.如果我能把它们隐藏起来,我也会很满意.

这有点帮助,因为它是我目前得到的最好的.

编辑 2

我对 PDAnnotationPopup 的尝试:按照@Tilman 的建议将以下几行添加到我的代码中:

<代码>...PDAnnotationPopup pdAnnotationPopup = new PDAnnotationPopup();pdAnnotationPopup.setParent(txtMark);pdAnnotationPopup.setContents("找到重要词");//只想确认一下pdAnnotationPopup.setInvisible(false);pdAnnotationPopup.setNoView(false);pdAnnotationPopup.setNoZoom(false);pdAnnotationPopup.setLocked(false);pdAnnotationPopup.setHidden(false);annotations.add(pdAnnotationPopup);

我还探索了其他 PDAnnotationPopup 参数,例如 setOpensetRectangle... 并尝试使其与 txtMark.setTitlePopup 共存.

不幸的是,这些都没有以任何方式影响我的代码.只有当我设置 setOpen(true) 加上 setRectangle 时,我才能看到一些东西:我的每个文本标记注释上都有一个完全空白的弹出窗口.

解决方案

所以我最终接受了@Tilman 的建议,在没有设置内容的情况下为我的标记添加了一个 setTitlePopup.为了使注释及其工具提示可以通过某些查看器(如 pdf.js)可见,还需要调用constructAppearances 方法:

txtMark.setTitlePopup("找到重要词");txtMark.constructAppearances(new PDHighlightAppearanceHandler(txtMark, pdDocument));

虽然这并不完美,因为它需要双击要显示的注释,而且只有更强大的 pdf 阅读器才能看到它,比如 Adob​​e,这是我目前能找到的最好的解决方案,就足够了.如果你用

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:

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...

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:

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:

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.

解决方案

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:

这篇关于在 PDFBox 中,是否可以向标记注释添加工具提示(提示)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆