Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sat May 05, 2018 9:06 am

Code: Select all
PdfDocument document = new PdfDocument();
         document.AllowCreateForm = (document.Form == null) ? true : false;
         document.Pages.Add();
         PdfPageBase page = document.Pages[0];

         PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f);
         PdfBrush brush = PdfBrushes.Black;
         float x = 10;
         float y = 10;
         float tempX = 0;
         float tempY = 0;

         string text = "Textbox: ";
         page.Canvas.DrawString(text, font, brush, x, y);
         tempX = font.MeasureString(text).Width + 15;
         tempY = font.MeasureString(text).Height + 15;

         string[] strarray = new string[] { "textbox1", "textbox2" };


                        PdfTextBoxField textbox1 = new PdfTextBoxField(page, "TextBox1");
         textbox1.Bounds = new RectangleF(tempX+10, y, tempX * 2, 15);
         textbox1.BorderWidth = 2.75f;
         textbox1.BorderStyle = PdfBorderStyle.Dashed;
         textbox1.Location = new PointF(35f, 50f);


                        PdfTextBoxField textbox2 = new PdfTextBoxField(page, "TextBox2");
         textbox2.Bounds = new RectangleF(tempX+20, y, tempX * 2, 15);
         textbox2.BorderWidth = 2.75f;
         textbox2.BorderStyle = PdfBorderStyle.Beveled;
         textbox2.Location = new PointF(35f, 100f);


                        PdfTextBoxField textbox3 = new PdfTextBoxField(page, "TextBox3");
         textbox3.Bounds = new RectangleF(tempX+30, y, tempX * 2, 15);
         textbox3.BorderWidth = 2.75f;
         textbox3.BorderStyle = PdfBorderStyle.Inset;
         textbox3.Location = new PointF(35f, 150f);

         textbox3.Actions.Calculate=new PdfJavaScriptAction(PdfJavaScript.GetSimpleCalculateString("MIN", strarray));
         
         document.SaveToFile(("C:/Files/") + "tboxforecolor.pdf", FileFormat.PDF);


the generated pdf has all the things correct. even the calculate part is attached when saw in pdf editor. But when it comes to execution, textbox3 doesnt show minimum value. The same doesnt work for AVG, SUM, PROD, etc.

please advise.

chintalpatel89
 
Posts: 16
Joined: Sun Dec 25, 2016 9:54 am

Mon May 07, 2018 7:26 am

Dear chintalpatel89,

Thanks for your inquiry.
There was something wrong in your code. You need to use the textboxs' name("TextBox1" and "TextBox2") instead of the objects' name("textbox1" and "textbox2") when setting the calculation. Please change this code
Code: Select all
textbox3.Actions.Calculate=new PdfJavaScriptAction(PdfJavaScript.GetSimpleCalculateString("MIN", strarray));

to this code:
Code: Select all
textbox3.Actions.Calculate = new PdfJavaScriptAction(PdfJavaScript.GetSimpleCalculateString("MIN", new string[] { "TextBox1", "TextBox2" }));

Yet, I found the result file still had the issue that the calculation didn't work. I have posted the issue to our Dev team. We will let you know once there is any progress.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed May 16, 2018 4:04 pm

Thank you. Looking forward for the fix.

chintalpatel89
 
Posts: 16
Joined: Sun Dec 25, 2016 9:54 am

Thu May 17, 2018 8:27 am

Dear chintalpatel89,

Thanks for posting.
After further investigation, the calculation could work. Please refer to following code:
Code: Select all
            PdfDocument document = new PdfDocument();
            document.AllowCreateForm = (document.Form == null) ? true : false;
            document.Pages.Add();
            PdfPageBase page = document.Pages[0];

            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f);
            PdfBrush brush = PdfBrushes.Black;
            float x = 10;
            float y = 10;
            float tempX = 0;
            float tempY = 0;

            string text = "Textbox: ";
            page.Canvas.DrawString(text, font, brush, x, y);
            tempX = font.MeasureString(text).Width + 15;
            tempY = font.MeasureString(text).Height + 15;
            string[] strarray = new string[] { "textbox1", "textbox2" };

            PdfTextBoxField textbox1 = new PdfTextBoxField(page, "TextBox1");
            textbox1.Bounds = new RectangleF(tempX + 10, y, tempX * 2, 15);
            textbox1.BorderWidth = 2.75f;
            textbox1.BorderStyle = PdfBorderStyle.Dashed;
            textbox1.Location = new PointF(35f, 50f);

            PdfTextBoxField textbox2 = new PdfTextBoxField(page, "TextBox2");
            textbox2.Bounds = new RectangleF(tempX + 20, y, tempX * 2, 15);
            textbox2.BorderWidth = 2.75f;
            textbox2.BorderStyle = PdfBorderStyle.Beveled;
            textbox2.Location = new PointF(35f, 100f);

            PdfTextBoxField textbox3 = new PdfTextBoxField(page, "TextBox3");
            textbox3.Bounds = new RectangleF(tempX + 30, y, tempX * 2, 15);
            textbox3.BorderWidth = 2.75f;
            textbox3.BorderStyle = PdfBorderStyle.Inset;
            textbox3.Location = new PointF(35f, 150f);
            string s = PdfJavaScript.GetSimpleCalculateString("MIN", new string[] { "TextBox1", "TextBox2" });
            textbox3.Actions.Calculate = new PdfJavaScriptAction(s);
            //must do this
            document.Form.Fields.Add(textbox1);
            document.Form.Fields.Add(textbox2);
            document.Form.Fields.Add(textbox3);
            document.SaveToFile("13278.pdf", FileFormat.PDF);

Any question, just feel free to contact us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed May 23, 2018 8:19 am

Dear chintalpatel89,

Did you try the code I provided ? Did it solve your issue ?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Sun May 27, 2018 12:50 pm

Yes. It worked. Thank you very much. :-)

chintalpatel89
 
Posts: 16
Joined: Sun Dec 25, 2016 9:54 am

Mon May 28, 2018 2:17 am

Dear chintalpatel89,

Thanks for your feedback.
Just feel free to contact us if there is any question.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF