您需要一些注释来实现您想要的。
让我从一个简单的文本注释开始:
假设:
-
writer
是你的PdfWriter
实例,
-
rect1
and rect2
是定义坐标的矩形,
-
title
and contents
are string
具有您想要在文本注释中显示的内容的对象,
然后您需要此代码片段来添加弹出注释:
// Create the text annotation
PdfAnnotation text = PdfAnnotation.CreateText(writer, rect1, title, contents, false, "Comment");
text.Name = "text";
text.Flags = PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_NOVIEW;
// Create the popup annotation
PdfAnnotation popup = PdfAnnotation.CreatePopup(writer, rect2, null, false);
// Add the text annotation to the popup
popup.Put(PdfName.PARENT, text.IndirectReference);
// Declare the popup annotation as popup for the text
text.Put(PdfName.POPUP, popup.IndirectReference);
// Add both annotations
writer.AddAnnotation(text);
writer.AddAnnotation(popup);
// Create a button field
PushbuttonField field = new PushbuttonField(wWriter, rect1, "button");
PdfAnnotation widget = field.Field;
// Show the popup onMouseEnter
PdfAction enter = PdfAction.JavaScript(JS1, writer);
widget.SetAdditionalActions(PdfName.E, enter);
// Hide the popup onMouseExit
PdfAction exit = PdfAction.JavaScript(JS2, writer);
widget.SetAdditionalActions(PdfName.X, exit);
// Add the button annotation
writer.AddAnnotation(widget);
两个常量尚未解释:
JS1:
"var t = this.getAnnot(this.pageNum, 'text'); t.popupOpen = true; var w = this.getField('button'); w.setFocus();"
JS2:
"var t = this.getAnnot(this.pageNum, 'text'); t.popupOpen = false;"
当然,这在我的书中进行了解释,更具体地说是在第 7 章中。您可以找到完整的示例here。如果您需要C#示例,请查找相应的示例here.
如果您还想要图像,请看一下这个示例:广告.pdf
这里有一个广告,当您单击“关闭此广告”时,该广告就会关闭。这也是使用 JavaScript 完成的。您需要将前面的代码片段与广告例子。
您需要的关键 JavaScript 方法是:getField()
and getAnnot()
。您必须更改属性才能显示或隐藏内容。