Products
PDFKit.NET 4.0
Created
7/3/2014
Tags
Generate PDF PDF forms

This code sample build a simple calculator from form fields and JavaScript. The calculator consists of a number of buttons and text fields and JavaScript that performs the calculation and presents the results.

Here is the main routine where we construct the PDF document and create all the elements of the calculator.

C# code sample //Create some document-level JS. document.JavaScripts.Add("calcJS", new JavaScript(File.ReadAllText(@"..\..\calculator.js"))); //custom page size. Page page = new Page(280, 210); document.Pages.Add(page); //Create TextBox Field for the result { TextShape textShape = new TextShape(25, page.Height - 40, "Result:", Font.Helvetica, 12); page.VisualOverlay.Add(textShape); TextField field = new TextField("result"); Widget widget = new Widget(70, page.Height - 40, 180, 15); //prevent editing field.ReadOnly = true; widget.BorderColor = RgbColor.Blue; widget.BorderWidth = 1; widget.HorizontalAlignment = HorizontalAlignment.Right; field.Widgets.Add(widget); page.Widgets.Add(widget); document.Fields.Add(field); } //Create TextBox Field for the current operator { TextShape textShape = new TextShape(170, page.Height - 60, "Operator:", Font.Helvetica, 9); page.VisualOverlay.Add(textShape); TextField field = new TextField("operator"); Widget widget = new Widget(210, page.Height - 60, 40, 10); //prevent editing field.ReadOnly = true; widget.BackgroundColor = System.Drawing.Color.Yellow; widget.Font = Font.HelveticaOblique; widget.FontSize = 6; widget.HorizontalAlignment = HorizontalAlignment.Center; field.Widgets.Add(widget); page.Widgets.Add(widget); document.Fields.Add(field); } //Place clear total (C) and clear element (CE) button placeCButton(document, page, 100, page.Height - 80); placeCeButton(document, page, 140, page.Height - 80); //Place number buttons placeNumberButton(document, page, 7, 100, page.Height - 100); placeNumberButton(document, page, 8, 140, page.Height - 100); placeNumberButton(document, page, 9, 180, page.Height - 100); placeNumberButton(document, page, 4, 100, page.Height - 120); placeNumberButton(document, page, 5, 140, page.Height - 120); placeNumberButton(document, page, 6, 180, page.Height - 120); placeNumberButton(document, page, 1, 100, page.Height - 140); placeNumberButton(document, page, 2, 140, page.Height - 140); placeNumberButton(document, page, 3, 180, page.Height - 140); placeNumberButton(document, page, 0, 100, page.Height - 160); //Place other buttons placeDotButton(document, page, 140, page.Height - 160); placePlusMinButton(document, page, 180, page.Height - 160); placePlusButton(document, page, 220, page.Height - 100); placeMinusButton(document, page, 220, page.Height - 120); placeDivideButton(document, page, 220, page.Height - 140); placeMultiplyButton(document, page, 220, page.Height - 160); placeEqualsButton(document, page, 220, page.Height - 180); // write the modified document to disk, note: signing requires read-write file access using (FileStream outFile = new FileStream(@"..\..\createjavascript.pdf", FileMode.Create, FileAccess.ReadWrite)) { document.Write(outFile); }

} ]]>

VB.NET code sample 'Create some document-level JS. document.JavaScripts.Add("calcJS", New JavaScript(File.ReadAllText("..\..\calculator.js"))) 'custom page size. Dim page As New Page(280, 210) document.Pages.Add(page) 'Create TextBox Field for the result Dim textShape As New TextShape(25, page.Height - 40, "Result:", Font.Helvetica, 12) page.VisualOverlay.Add(textShape) Dim field As New TextField("result") Dim widget As New Widget(70, page.Height - 40, 180, 15) 'prevent editing field.[ReadOnly] = True widget.BorderColor = RgbColor.Blue widget.BorderWidth = 1 widget.HorizontalAlignment = HorizontalAlignment.Right field.Widgets.Add(widget) page.Widgets.Add(widget) document.Fields.Add(field) 'Create TextBox Field for the current operator textShape = New TextShape(170, page.Height - 60, "Operator:", Font.Helvetica, 9) page.VisualOverlay.Add(textShape) field = New TextField("operator") widget = New Widget(210, page.Height - 60, 40, 10) 'prevent editing field.[ReadOnly] = True widget.BackgroundColor = System.Drawing.Color.Yellow widget.Font = Font.HelveticaOblique widget.FontSize = 6 widget.HorizontalAlignment = HorizontalAlignment.Center field.Widgets.Add(widget) page.Widgets.Add(widget) document.Fields.Add(field) 'Place clear total (C) and clear element (CE) button placeCButton(document, page, 100, page.Height - 80) placeCeButton(document, page, 140, page.Height - 80) 'Place number buttons placeNumberButton(document, page, 7, 100, page.Height - 100) placeNumberButton(document, page, 8, 140, page.Height - 100) placeNumberButton(document, page, 9, 180, page.Height - 100) placeNumberButton(document, page, 4, 100, page.Height - 120) placeNumberButton(document, page, 5, 140, page.Height - 120) placeNumberButton(document, page, 6, 180, page.Height - 120) placeNumberButton(document, page, 1, 100, page.Height - 140) placeNumberButton(document, page, 2, 140, page.Height - 140) placeNumberButton(document, page, 3, 180, page.Height - 140) placeNumberButton(document, page, 0, 100, page.Height - 160) 'Place other buttons placeDotButton(document, page, 140, page.Height - 160) placePlusMinButton(document, page, 180, page.Height - 160) placePlusButton(document, page, 220, page.Height - 100) placeMinusButton(document, page, 220, page.Height - 120) placeDivideButton(document, page, 220, page.Height - 140) placeMultiplyButton(document, page, 220, page.Height - 160) placeEqualsButton(document, page, 220, page.Height - 180) ' write the modified document to disk, note: signing requires read-write file access Using outFile As New FileStream("..\..\createjavascript.pdf", FileMode.Create, FileAccess.ReadWrite) document.Write(outFile) End Using End Sub

]]>

The routines to create widgets:

C# code sample private static void placeNumberButton(Document document, Page page, int number, double left, double bottom) { string javaScript = string.Format("number_pressed({0});", number.ToString()); placeButton(document, page, left, bottom, "num" + number.ToString(), number.ToString(), javaScript); }

private static void placeDotButton(Document document, Page page, double left, double bottom) { string javaScript = "dot_pressed();"; placeButton(document, page, left, bottom, "dot", ".", javaScript); }

private static void placePlusMinButton(Document document, Page page, double left, double bottom) { string javaScript = "plusMin_pressed();"; placeButton(document, page, left, bottom, "plus_min", "+/-", javaScript); }

private static void placePlusButton(Document document, Page page, double left, double bottom) { string javaScript = "operator_pressed('PLUS');"; placeButton(document, page, left, bottom, "plus", "+", javaScript); }

private static void placeMinusButton(Document document, Page page, double left, double bottom) { string javaScript = "operator_pressed('MINUS');"; placeButton(document, page, left, bottom, "minus", "-", javaScript); }

private static void placeDivideButton(Document document, Page page, double left, double bottom) { string javaScript = "operator_pressed('DIVIDE');"; placeButton(document, page, left, bottom, "divide", "/", javaScript); }

private static void placeMultiplyButton(Document document, Page page, double left, double bottom) { string javaScript = "operator_pressed('MULTIPLY');"; placeButton(document, page, left, bottom, "multiply", "*", javaScript); }

private static void placeEqualsButton(Document document, Page page, double left, double bottom) { string javaScript = "equal_pressed();"; placeButton(document, page, left, bottom, "equals", "=", javaScript); }

private static void placeCButton(Document document, Page page, double left, double bottom) { string javaScript = "clear_all();"; placeButton(document, page, left, bottom, "c", "C", javaScript, true); }

private static void placeCeButton(Document document, Page page, double left, double bottom) { string javaScript = "clear_entry();\nresult.value = 0;"; placeButton(document, page, left, bottom, "Ce", "CE", javaScript, true); }

private static void placeButton(Document document, Page page, double left, double bottom, string fieldName, string label, string javaScript) { placeButton(document, page, left, bottom, fieldName, label, javaScript, false); }

private static void placeButton(Document document, Page page, double left, double bottom, string fieldName, string label, string javaScript, bool displayRed) { PushButtonField field = new PushButtonField(fieldName); PushButtonWidget widget = new PushButtonWidget(left, bottom, 30, 15);

if (displayRed)
{
    widget.BackgroundColor = System.Drawing.Color.Red;
    widget.BorderColor = System.Drawing.Color.Gray;
    widget.TextColor = System.Drawing.Color.White;
}
else
{
    widget.BackgroundColor = System.Drawing.Color.LightGray;
    widget.BorderColor = System.Drawing.Color.Gray;
}
widget.BorderStyle = BorderStyle.Beveled;

widget.Label = label;

field.Widgets.Add(widget);
page.Widgets.Add(widget);
document.Fields.Add(field);

//include action
widget.MouseUpActions.Add(new JavaScriptAction(javaScript));

} ]]>

VB.NET code sample Private Shared Sub placeNumberButton(ByVal document As Document, ByVal page As Page, ByVal number As Integer, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = String.Format("number_pressed({0});", number.ToString()) placeButton(document, page, left, bottom, "num" + number.ToString(), number.ToString(), _ javaScript) End Sub Private Shared Sub placeDotButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "dot_pressed();" placeButton(document, page, left, bottom, "dot", ".", _ javaScript) End Sub Private Shared Sub placePlusMinButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "plusMin_pressed();" placeButton(document, page, left, bottom, "plus_min", "+/-", _ javaScript) End Sub Private Shared Sub placePlusButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "operator_pressed('PLUS');" placeButton(document, page, left, bottom, "plus", "+", _ javaScript) End Sub Private Shared Sub placeMinusButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "operator_pressed('MINUS');" placeButton(document, page, left, bottom, "minus", "-", _ javaScript) End Sub Private Shared Sub placeDivideButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "operator_pressed('DIVIDE');" placeButton(document, page, left, bottom, "divide", "/", _ javaScript) End Sub Private Shared Sub placeMultiplyButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "operator_pressed('MULTIPLY');" placeButton(document, page, left, bottom, "multiply", "*", _ javaScript) End Sub Private Shared Sub placeEqualsButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "equal_pressed();" placeButton(document, page, left, bottom, "equals", "=", _ javaScript) End Sub Private Shared Sub placeCButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "clear_all();" placeButton(document, page, left, bottom, "c", "C", _ javaScript, True) End Sub Private Shared Sub placeCeButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) Dim javaScript As String = "clear_entry();" & Environment.NewLine & "result.value = 0;" placeButton(document, page, left, bottom, "Ce", "CE", _ javaScript, True) End Sub Private Shared Sub placeButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double, ByVal fieldName As String, ByVal label As String, _ ByVal javaScript As String) placeButton(document, page, left, bottom, fieldName, label, _ javaScript, False) End Sub Private Shared Sub placeButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double, ByVal fieldName As String, ByVal label As String, _ ByVal javaScript As String, ByVal displayRed As Boolean) Dim field As New PushButtonField(fieldName) Dim widget As New PushButtonWidget(left, bottom, 30, 15) If displayRed Then widget.BackgroundColor = System.Drawing.Color.Red widget.BorderColor = System.Drawing.Color.Gray widget.TextColor = System.Drawing.Color.White Else widget.BackgroundColor = System.Drawing.Color.LightGray widget.BorderColor = System.Drawing.Color.Gray End If widget.BorderStyle = BorderStyle.Beveled widget.Label = label field.Widgets.Add(widget) page.Widgets.Add(widget) document.Fields.Add(field) 'include action widget.MouseUpActions.Add(New JavaScriptAction(javaScript)) End Sub

]]>

Here is how our calculator in a PDF file looks like: