ProgBlogger
ProgBlogger

Reputation: 305

How to fire a button action created via iTextSharp

I have tried different variants but some why my button doesn't work.

        var fileName = "buttonFile.pdf";
        using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
        using (var doc = new Document(PageSize.A4))
        using (var writer = PdfWriter.GetInstance(doc, fs))
        {
            doc.Open();
            var button = new PushbuttonField(writer, new Rectangle(300, 300, 330, 330), "Text")
            {
                Text = "Click meh!"
            };
            PdfAppearance theButton = button.GetAppearance();
            theButton.SetAction(PdfAction.JavaScript("app.execMenuItem('SaveAs')", writer), 300, 300, 330, 330);
            writer.AddAnnotation(button.Field);
            doc.Close();
        }`

I haven't seen any code of creating buttons via iTextSharp so I tried to use examples from java analogs but I'm not sure that this is the appropriate way to create buttons.

Upvotes: 0

Views: 836

Answers (1)

Chris Haas
Chris Haas

Reputation: 55417

Grab the Field instead of the Appearance:

var f = button.Field;
f.Action = PdfAction.JavaScript("app.execMenuItem('SaveAs')", writer);
writer.AddAnnotation(f);

Upvotes: 2

Related Questions