Skip to main content
Skip table of contents

How to create a pop-up window to display images and text?

I'm not familiar with javascript. Are there any other options?

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

iTextSharp.text.pdf.PdfAnnotation annot =
        stamper.Writer, c.rect, PdfAnnotation.HIGHLIGHT_INVERT,
        PdfAction.JavaScript("app.alert('action!')", stamper.Writer));
The above code is used to display an alert and I want to customize it to my needs. I'm not familiar with javascript. Are there any other options ?

Posted on StackOverflow on May 31, 2014 by Buddhima Naween Rathnayake

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

Let me start with a simple text annotation:

Suppose that:

  • 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
// 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

Two constants aren't explained yet:


"var t = this.getAnnot(this.pageNum, 'text');
     t.popupOpen = true;
    var w = this.getField('button');


"var t = this.getAnnot(this.pageNum, 'text');
    t.popupOpen = false;"

You can find a full example here.

If you also want an image, please take a look at the Advertisement example.

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.

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

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.