Skip to main content
Skip table of contents

How to define multiple actions for a PushbuttonField?

How do I keep a page redirect or how do I set it in code?

In a PDF, I can set up a button to:

  1. Submit the FDF data to my RestAPI
  2. Redirect to a webpage (to tell user 'Thanks') and that works perfectly.

But I need to do this using code... I am using iTextSharp in the following way:

  • pb is my pushbutton...
  • sURL is the URL I need the data to go to...

This is my code:

 

PdfFormField pff = pb.Field;
pff.SetAdditionalActions(PdfName.A, PdfAction.CreateSubmitForm(sURL, null, PdfAction.SUBMIT_XFDF));
af.ReplacePushbuttonField("submit", pff);
This does replace the button with the submit action. How do I keep the page redirect or how do I set it in code? I have also tried PdfName.AA and PdfName.U based on examples and I am not certain what the different names are for. Unfortunately, none of them solve my problem.

Apparently, the SetAdditionalActions() replaces the existing actions. I have also tried using annotations and just adding a button that doesn't exist.

Posted on StackOverflow on Apr 18, 2014 by JohnKochJr

You are looking for a concept called chained actions:

Chunk chunk = new Chunk("print this page");
PdfAction action = PdfAction.javaScript(
    "app.alert('Think before you print!');", stamper.getWriter());
action.next(PdfAction.javaScript(
    "printCurrentPage(this.pageNum);", stamper.getWriter()));
action.next(new PdfAction("https://www.panda.org/savepaper/"));
chunk.setAction(action);

In this case, we have an action that shows an alert, prints a page and redirects to an URL. These actions are chained to each other using the next() method.

In C#, this would be:

Chunk chunk = new Chunk("print this page");
PdfAction action = PdfAction.JavaScript(
    "app.alert('Think before you print!');", stamper.Writer);
action.Next(PdfAction.JavaScript(
    "printCurrentPage(this.pageNum);", stamper.Writer));
action.Next(new PdfAction("https://www.panda.org/savepaper/"));
chunk.SetAction(action);
JavaScript errors detected

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

If this problem persists, please contact our support.