We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Modifying the Action of a PDF form's PushButton?

Hello,


I’m writing a simple program that iterates over every page in a PDF and modifies any form PushButtons on the page. I need to modify the PushButton’s Action attributes. Basically, the buttons on these PDFs that I am looping over attempt to open a file on the system at a certain location, and we are needing to modify the path. I’m able to locate all of the PushButtons in a PDF, but I can’t seem to figure out how to modify this Action attributes. Is this possible with Aspose? I can’t even seem to be able to display the tooltip or label of the PushButton. All that I can seem to get is the name of it.

Here is my code, at the moment. It displays the name of all of the PushButtons in the PDF:

using System;
using System.IO;
using System.Text;
using System.Collections;
using Aspose.Pdf.Kit;

namespace UpdatePdfLinks
{
class Program
{
static void Main(string[] args)
{
// set aspose license
License l = new License();
l.SetLicense(@“Aspose.Total.lic”);

if (args.Length != 1)
{
Console.WriteLine(“Usage: UpdatePdfLinks.exe <input_file>”);
return;
}

// store the input file
string input_file = args[0];

if (!File.Exists(input_file))
{
Console.WriteLine(“The specified input file does not exist.”);
return;
}

// open the pdf form editor
Form f = new Form(input_file, input_file + “.foo.pdf”);

// loop over all the form field names
foreach (string field_name in f.FieldNames)
{
// we’re looking for only pushbuttons
if (field_name.Length > 0 && f.GetFieldType(field_name) == FieldType.PushButton)
{
// get the page number of this button
int page_number = f.GetFieldFacade(field_name).PageNumber;
Console.WriteLine(f.GetFullFieldName(field_name));
}
}

Console.ReadKey();
}
}
}

Hi Ryan,


Thanks for contacting support and sorry for replying you late. We are working over this requirement and will get back to you soon. We apologize for the delay and inconvenience.

Hi Ryan,


Thanks for your patience.

Please try using the code snippet specified below to modify form action for non-XFA forms (where document.Form.XFA == null )

[C#]
Document doc = new Document(“PdfwithAcroForm.pdf”);
foreach (WidgetAnnotation field in doc.Form)
{
if (field is ButtonField)
{
(field as ButtonField).OnActivated = new GoToURIAction(“www.aspose.com”);
}
}
doc.Save(“FormWithActions.pdf”);

For XFA form (document.Form.XFA != null)
[C#]

Document doc = new Document(“PdfDynamicForm.pdf”);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.Form.XFA.XDP.NameTable);
nsmgr.AddNamespace(“tpl”, doc.Form.XFA.Template.NamespaceURI);
foreach (XmlNode field in doc.Form.XFA.Template.SelectNodes("//tpl:field", nsmgr))
{
if (field.SelectSingleNode(“tpl:ui/tpl:button”, nsmgr) != null)
{
XmlNode eventNode = field.SelectSingleNode(“tpl:event”, nsmgr);
if (eventNode == null)
{
eventNode = doc.Form.XFA.XDP.CreateElement(“tpl:event”, doc.Form.XFA.Template.NamespaceURI);
XmlAttribute attr = doc.Form.XFA.XDP.CreateAttribute(“activity”);
attr.Value = “click”;
eventNode.Attributes.Append(attr);
field.AppendChild(eventNode);
}
XmlNode submit = doc.Form.XFA.XDP.CreateElement(“tpl:submit”, doc.Form.XFA.Template.NamespaceURI);
XmlAttribute target = doc.Form.XFA.XDP.CreateAttribute(“target”);
target.Value = “www.aspose.com”;
submit.Attributes.Append(target);
eventNode.AppendChild(submit);
}
doc.Save(“XFAFormWithActions.pdf”);
In case it does not satisfy your requirements or you have any further query, please feel free to contact.

Thanks, codewarrior. I tried out the code. The PDF I am testing with is an XFA form, it looks like. There is a problem, though. The second line of code you provided does not work for me. The ‘doc.Form.XFA.XDP’ reference is null at that point in time. XDP is null, that is.


Is the example you provide accurate, or is something on my end not working as intended, you think?