Calculate fields values using JavaScript and display in text box - Javascript Aspose

Hello there,

I’m trying to add a couple of form fields to a pdf document and one another to keep their product.

It’s an easy task to add fields into a document using this doc. But I’m having difficulties with adding OnCalculate action for the third field.

Here is a code that creates a new pdf document with three fields and adds OnCalculate action for the third and it looks exactly like Adobe fields, but, unfortunately, the calculation doesn’t work:

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();
        pdfDocument.Pages.Add();

        Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField asposeTextBoxField1, asposeTextBoxField2, asposeTextBoxFieldPRD;
        Aspose.Pdf.InteractiveFeatures.Annotations.Border border;
        Aspose.Pdf.InteractiveFeatures.Annotations.Dash dash = new Aspose.Pdf.InteractiveFeatures.Annotations.Dash(1, 1);

        asposeTextBoxField1 = new Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(100, 700, 200, 750)) { PartialName = "asposeField1" };
        border = new Aspose.Pdf.InteractiveFeatures.Annotations.Border(asposeTextBoxField1) { Width = 5, Dash = dash };
        pdfDocument.Form.Add(asposeTextBoxField1);

        asposeTextBoxField2 = new Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(250, 700, 350, 750)) { PartialName = "asposeField2" };
        border = new Aspose.Pdf.InteractiveFeatures.Annotations.Border(asposeTextBoxField2) { Width = 5, Dash = dash };
        pdfDocument.Form.Add(asposeTextBoxField2);

        asposeTextBoxFieldPRD = new Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(400, 700, 500, 750)) { PartialName = "asposeFieldPRD" };
        border = new Aspose.Pdf.InteractiveFeatures.Annotations.Border(asposeTextBoxFieldPRD) { Width = 10, Dash = dash };

        asposeTextBoxFieldPRD.Actions.OnCalculate = new Aspose.Pdf.InteractiveFeatures.JavascriptAction("AFSimple_Calculate(\"PRD\", new Array (\"asposeField1\", \"asposeField2\"));");

        pdfDocument.Form.Add(asposeTextBoxFieldPRD);

        pdfDocument.Save("testDocument.pdf");

I would like to know, is it possible to add calculated fields using Aspose?
Taking into account attachments (image: comparing Aspose and Adobe fields, pdf doc: an example with Aspose and Adobe fields) I may suggest, I have missed something in fields creation, but I have no idea where.

Thank you in advance!

image.png (7.5 KB)
testDocument_withAdobeFields.pdf (7.9 KB)

@Anton_Lysenko

Please try to use the below code snippet in order to get the results as per your expectations:

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();
pdfDocument.Pages.Add();

TextBoxField asposeTextBoxField1, asposeTextBoxField2, asposeTextBoxFieldPRD;
Border border;
Dash dash = new Dash(1, 1);

asposeTextBoxField1 = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(100, 700, 200, 750)) { PartialName = "asposeField1" };
border = new Border(asposeTextBoxField1) { Width = 5, Dash = dash };
pdfDocument.Form.Add(asposeTextBoxField1);

asposeTextBoxField2 = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(250, 700, 350, 750)) { PartialName = "asposeField2" };
border = new Border(asposeTextBoxField2) { Width = 5, Dash = dash };
pdfDocument.Form.Add(asposeTextBoxField2);

asposeTextBoxFieldPRD = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(400, 700, 500, 750)) { PartialName = "asposeFieldPRD" };
border = new Border(asposeTextBoxFieldPRD) { Width = 10, Dash = dash };

string JS = @"var w = this.getField('" + asposeTextBoxField1.PartialName + "'); " +
                "var x = this.getField('" + asposeTextBoxField2.PartialName + "'); " +
                "var y = this.getField('" + asposeTextBoxFieldPRD.PartialName + "');" +
                "y.value = w.value * x.value";

asposeTextBoxField1.Actions.OnLostFocus = new JavascriptAction(JS);
asposeTextBoxField2.Actions.OnLostFocus = new JavascriptAction(JS);

pdfDocument.Form.Add(asposeTextBoxFieldPRD);

pdfDocument.Save(dataDir + "testDocument.pdf");

testDocument.pdf (3.8 KB)

Thank you for the answer! Your solution works fine but does it mean that we are not able to use AcroForm functions in “OnCalculate” action?

@Anton_Lysenko

Thanks for your feedback.

You can use same JavascriptAction with JS String in the suggested code and assign it to OnCalculate event. In case you face any issue, please let us know.