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;
page2.Paragraphs.Add(Element);

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();

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

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

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

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

frm.Save();
}

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,


I
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();
Page page = doc.Pages.Add();
 
//BUTTON
ButtonField btnElement = new ButtonField();
btnElement.PartialName = “Button1”;
btnElement.Value = “button1”;
 
TextFragment Element = new TextFragment("Button: ");
//Element.Segments.Add().InlineParagraph = btnElement;
btnElement.Width = 80;
btnElement.Height = 20;
 
page.Paragraphs.Add(Element);
page.Paragraphs.Add(btnElement);
 
//Add Java Script
MemoryStream src = new MemoryStream();
doc.Save(src);
FormEditor frm = new FormEditor();
frm.BindPdf(src);
 
frm.SetFieldScript(“Button1”, “app.alert(‘Hello World!’);”);
frm.Save(“PushButton_out.pdf”);

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 9.2.0.0 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,