本文介绍了iTextSharp - 在悬停时显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp生成包含读者可能不知道的单词的PDF。所以我想强调这些单词,然后添加一个功能,其中(1)当用户将鼠标悬停在单词上时,会弹出一个定义,(2)如果用户点击弹出的定义,它会转到文档的定义部分。

I am using iTextSharp to generate a PDF that contains words the reader may not know. So I want to underline these words and then add a feature where (1) when the user hovers the mouse over the word, a definition pops up, and (2) if the user clicks on the popped-up definition, it goes to the Definitions section of the document.

我看到在线执行此操作的示例代码,但它总是要求您知道链接在页面上的显示位置,在我的情况下我正在生成一个结构化文档(章节和章节),所以我不知道这个词的位置。

I see example code to do this online, but it always requires that you know where the link will appear on the page, and in my case I'm generating a structured document (Chapters and Sections) so I do not know the position of the word.

我怀疑它使用PdfAnnotation和Link但是无法计算究竟如何。

I suspect it uses PdfAnnotation and Link but can't figure out exactly how.

如果文本在表格单元格中时解决方案也有效(我看到的一些解决方案存在问题),则可以获得奖励积分。

Bonus points if the solution also works when the text is in a table cell (some of the solutions I see have problems there).

ps一些进展:

当我创建我的定义部分时,我打电话

When I create my Definitions section, I call

this.Chunks[0].SetLocalDestination("Definitions");

当我将具有挑战性的单词添加到我的PDF文档时,我在Chunk上调用SetGenericTag()。然后,在我对PdfPageEvent.OnGenericTag()的覆盖中,我这样做:

When I add the challenging word to my PDF document, I call SetGenericTag() on the Chunk. Then, in my override for PdfPageEvent.OnGenericTag() I do this:

PdfAnnotation link = PdfAnnotation.CreateLink(writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, PdfAction.GotoLocalPage("Definitions", false));
writer.AddAnnotation(link);

满足我需要的一部分 - 如果我点击它带我到定义部分该文件。但是当你将鼠标悬停在单词上时仍然没有出现的定义。

which satisfies part of what I need - if I click on the word it takes me to the Definitions section of the document. But there is still no definition that appears when you hover over the word.

推荐答案

这就是我想出的:

PushbuttonField button = new PushbuttonField(writer, rect, "DefineButton-" + (++NumButtons));
PdfFormField field = button.Field;
field.Action = PdfAction.GotoLocalPage("Definitions", false);
field.UserName = definition;
writer.AddAnnotation(field);

这篇关于iTextSharp - 在悬停时显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:03