Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从itextsharp批注中弹出一个窗口以显示图像和文本_C#_Javascript_Itextsharp - Fatal编程技术网

C# 从itextsharp批注中弹出一个窗口以显示图像和文本

C# 从itextsharp批注中弹出一个窗口以显示图像和文本,c#,javascript,itextsharp,C#,Javascript,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)); 上

我想在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:

JS2:

当然,这在我的书中有解释,更具体地说是在第7章。你可以找到一个完整的例子。如果您需要一个C#示例,请查找相应的示例

如果您还需要图像,请查看以下示例:

这里有一则广告,当您单击“关闭此广告”时,该广告将关闭。这也可以使用JavaScript完成。您需要将前面的代码片段与示例代码结合起来


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

我通过使用附件而不是弹出窗口解决了这个问题。下面的代码将在您的pdf中放置一个图像,单击图像,它将以完全分辨率打开图像

PdfReader reader = new PdfReader(ClassLoader.getSystemClassLoader().getResourceAsStream("source.pdf"));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("target.pdf"));

PdfAnnotation imgAttach = PdfAnnotation.createFileAttachment(stamper.getWriter(), new Rectangle(100, 100, 200, 200), "", null, "PATH/TO/image.jpeg", "image.jpeg");

PdfAppearance app = stamper.getOverContent(1).createAppearance(100, 100);
Image img = Image.getInstance("PATH/TO/image.jpeg");
img.scaleAbsolute(100, 100);
img.setAbsolutePosition(100, 100);
app.addImage(img);

imgAttach.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);

stamper.addAnnotation(imgAttach, 1);
stamper.getOverContent(1).addImage(img);
stamper.close();

我想将它添加到桌面pdf阅读器应用程序中。我想你指的是web应用程序。有没有办法从批注中弹出自定义的windows窗体。桌面/服务器,没关系。事实上,如果你查看我书中的原始示例,你会发现它是一个独立的桌面应用程序(所以我不明白你为什么声称我在谈论一个web应用程序)。至于自定义弹出窗口:您可以在ISO-32000-1规定的范围内以及PDF查看器支持的范围内进行自定义。请原谅我的无知,先生。我发现这是可能的,谢谢
"var t = this.getAnnot(this.pageNum, 'text'); t.popupOpen = false;"
PdfReader reader = new PdfReader(ClassLoader.getSystemClassLoader().getResourceAsStream("source.pdf"));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("target.pdf"));

PdfAnnotation imgAttach = PdfAnnotation.createFileAttachment(stamper.getWriter(), new Rectangle(100, 100, 200, 200), "", null, "PATH/TO/image.jpeg", "image.jpeg");

PdfAppearance app = stamper.getOverContent(1).createAppearance(100, 100);
Image img = Image.getInstance("PATH/TO/image.jpeg");
img.scaleAbsolute(100, 100);
img.setAbsolutePosition(100, 100);
app.addImage(img);

imgAttach.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);

stamper.addAnnotation(imgAttach, 1);
stamper.getOverContent(1).addImage(img);
stamper.close();