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

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 =
    iTextSharp.text.pdf.PdfAnnotation.CreateLink(
        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
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);

Two constants aren't explained yet:

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;"

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.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now