从itextsharp注释弹出一个窗口以显示图像和文本
我想在C#项目中添加并弹出窗口,通过单击itextsharp注释来显示图像和文本。
iTextSharp.text.pdf.PdfAnnotation annot = iTextSharp.text.pdf.PdfAnnotation.CreateLink(stamper.Writer, c.rect, PdfAnnotation.HIGHLIGHT_INVERT, PdfAction.JavaScript("app.alert('action!')", stamper.Writer));
上面的代码用于显示警报,我想根据我的需要自定义它可以有人请给我一个选项。我不熟悉的JavaScript。 或者我可以使用任何其他选项吗?
你需要一些注释来实现你想要的。
让我从一个简单的文本注释开始:
假设:
-
writer
是你的PdfWriter
实例, -
rect1
和rect2
是定义坐标的矩形, -
title
和contents
是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章中有解释。你可以在这里找到一个完整的例子。 如果您需要C#示例,请在此处查找相应的示例。
如果您还想要一张图片,请看一下这个例子: advertisement.pdf
当您点击“关闭此广告”时,您会在此处关闭广告。 这也是使用JavaScript完成的。 您需要将以前的代码段与Advertisement示例的代码组合在一起。
您需要的关键JavaScript方法是: getField()
和getAnnot()
。 您必须更改属性才能显示或隐藏内容。