从itextsharp注释弹出一个窗口以显示图像和文本 [英] pop-up a window from itextsharp annotation to display images and text

查看:144
本文介绍了从itextsharp注释弹出一个窗口以显示图像和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#项目中添加并弹出窗口,通过单击itextsharp注释来显示图像和文本。

I want to add and pop-up window in C# project to display an image and text by clicking on itextsharp annotation.

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。或者我可以使用任何其他选项吗?

above code is used to display the alert and i want to customize it to my needs can someone please give me an option.i'm not familiar with javascript. or can i use any other options ?

推荐答案

您需要几个注释才能达到您想要的效果。

You need a couple of annotations to achieve what you want.

让我先从一个简单的文本注释开始:

Let me start with a simple text annotation:

假设:


  • writer 是你的 PdfWriter 实例,

  • rect1 rect2 是定义坐标的矩形,

  • title 内容 string 包含您要显示的内容的对象在文本注释中,

  • writer is your PdfWriter instance,
  • rect1 and rect2 are rectangles that define coordinates,
  • title and contents are string objects with the content you want to show in the text annotation,

然后你需要这段代码片段来添加弹出注释:

Then you need this code snippet to add the popup annotations:

// 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#示例,请查找相应的示例此处

This is, of course, explained in my book, more specifically in chapter 7. You can find a full example here. If you need a C# example, please look for the corresponding example here.

如果您还想要一张图片,请看一下这个例子: advert.pdf

If you also want an image, please take a look at this example: advertisement.pdf

当您点击关闭此广告时,您的广告将关闭。这也是使用JavaScript完成的。您需要将之前的代码段与广告示例的代码结合使用。

Here you have an advertisement that closes when you click "close this advertisement". This is also done using JavaScript. You need to combine the previous snippet with the code of the Advertisement example.

您需要的关键JavaScript方法是: getField() getAnnot()。您必须更改属性才能显示或隐藏内容。

The key JavaScript methods you'll need are: getField() and getAnnot(). You'll have to change the properties to show or hide the content.

这篇关于从itextsharp注释弹出一个窗口以显示图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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