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

Free Support Forum - aspose.com

Add JavaScript to PushButton in PDF via Aspose.PDF for Java

I’m currently using Aspose.Pdf.Generator.Pdf to build my PDF.

I have added a button to my PDF using the following code:

FormField btnElement = new FormField() {
FormFieldType = FormFieldType.PushButton,
FieldName = “btnButton”,
FieldValue = “Press”
Element = new Text("Button: ");
Element.Segments.Add().InlineParagraph = btnElement;

Now I want to add JavaScript functionality to that button. what is the best way to achieve this? the FormEditor apparently does not accept the Aspose.Pdf.Generator.Pdf type. I was thinking I could save to a stream, pass that to the FormEditor, add my functionality, then save that to another stream, and load it back into the Generator… but that seems like a hell of a work around.

I mean the Generator will let me “generate” a button, and it will let me “generate” javascript, but how the heck can I generate a link between the two?

Hi John,

Thanks for your inquiry. I’m afraid Aspose.Pdf.Generator doesn’t support the feature to add java script to PushButton. I’ve logged a feature request as PDFNEWNET-35834 for the purpose. We will update you as soon as its resolved.

Moreover, Please note Aspose.Pdf.Generator used to create a PDF file from scratch so as a workaround you can create your PDF document using Aspose.Pdf.Generator and later pass it to FormEditor to add java script to PushButton.

We are sorry for the inconvenience caused.

Best Regards,

I tried that, and it’s still not working, see the code below:

public void genPDF(String FileName) {
//Generate a new PDF
Pdf pdf = new Pdf();
Section sec1= pdf.Sections.Add();

FormField btnElement = new FormField() {
FormFieldType = FormFieldType.PushButton,
FieldName = “Button1”,
FieldValue = “Button1”
Text Element = new Text("Button: ");
Element.Segments.Add().InlineParagraph = btnElement;

//Add Java Script
MemoryStream src = new MemoryStream();
FormEditor frm = new FormEditor(src, Context.Response);

frm.SetFieldScript(“Button1”, “app.alert(‘Hello World!’);”);

frm.AddField(FieldType.PushButton, “Button2”, “Button2”, 1, 10, 30, 110, 130);
frm.SetFieldScript(“Button2”, “app.alert(‘Hello World!’);”);


Am I doing something wrong?

Hi John,

Thanks for your feedback. We are looking into the matter and will get back to you as soon as possible.

Best Regards,

Hi John,

have tested the scenario and I am able to reproduce the same problem that JavaScript is not being added to button already present in PDF file (button created with Aspose.Pdf.Generator). For the
sake of correction, I have logged it in our issue tracking system as PDFNEWNET-35837. We
will investigate this issue in details and will keep you updated on the status
of a correction. <o:p></o:p>

We apologize for your inconvenience.

The issues you have found earlier (filed as PDFNEWNET-35837) have been fixed in Aspose.Pdf for .NET 8.7.0.

This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi John,

Thanks for your patience. As mentioned above your reported issue has been fixed. Please note we have fixed your reported issue in new DOM approach. Please check following code snippet for adding PushButton and adding javascript to the button. Hopefully it will help you to accomplish your requirements.

Document doc = new Document();<o:p></o:p>
Page page = doc.Pages.Add();<o:p></o:p>
ButtonField btnElement = new ButtonField();<o:p></o:p>
btnElement.PartialName = “Button1”;<o:p></o:p>
btnElement.Value = “button1”;<o:p></o:p>
TextFragment Element = new TextFragment("Button: ");<o:p></o:p>
//Element.Segments.Add().InlineParagraph = btnElement;<o:p></o:p>
btnElement.Width = 80;<o:p></o:p>
btnElement.Height = 20;<o:p></o:p>
//Add Java Script<o:p></o:p>
MemoryStream src = new MemoryStream();<o:p></o:p>
FormEditor frm = new FormEditor();<o:p></o:p>
frm.SetFieldScript(“Button1”, “app.alert(‘Hello World!’);”);<o:p></o:p>

Please feel free to contact us for any further assistance.

Best Regards,

Out of curiosity, could someone detail the reason behind why the code in my third post doesn’t work (and required a change of code to function).

As of version this issue is still present… but I would expect that with the form editor I could look-up and edit any form field in the PDF (this includes adding javascript to them). Is there an issue trying to location the form field by name because it’s a segment of an text element?

We just had a re-occurrence of issue with another programmer, and in both my original case and this new occurrence it was assumed that the form editor could locate and modify any form field within the PDF.

Hi John,

Thanks for your inquiry. Please note old Generator (Aspose.Pdf.Generator) has a bug in adding button field. Due to this, your document does not contain AcroForm and that is why button with name “Button1” could not be found. The button is visible on the page because the button is in Annotations array on the page. So it is recommended to use new DOM approach to add button as suggested above.

We are sorry for the inconvenience caused.

Best Regards,