Create PDF with javascript

Generate PDF, PDF forms
7/3/2014

Downloads

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

1 static void Main(string[] args) 2 { 3 //New document 4 Document document = new Document(); 5 6 //Create some document-level JS. 7 document.JavaScripts.Add("calcJS", new JavaScript(File.ReadAllText(@"..\..\calculator.js"))); 8 9 //custom page size. 10 Page page = new Page(280, 210); 11 document.Pages.Add(page); 12 13 //Create TextBox Field for the result 14 { 15 TextShape textShape = new TextShape(25, page.Height - 40, "Result:", Font.Helvetica, 12); 16 page.VisualOverlay.Add(textShape); 17 18 TextField field = new TextField("result"); 19 Widget widget = new Widget(70, page.Height - 40, 180, 15); 20 21 //prevent editing 22 field.ReadOnly = true; 23 24 widget.BorderColor = RgbColor.Blue; 25 widget.BorderWidth = 1; 26 widget.HorizontalAlignment = HorizontalAlignment.Right; 27 28 field.Widgets.Add(widget); 29 page.Widgets.Add(widget); 30 document.Fields.Add(field); 31 } 32 33 //Create TextBox Field for the current operator 34 { 35 TextShape textShape = new TextShape(170, page.Height - 60, "Operator:", Font.Helvetica, 9); 36 page.VisualOverlay.Add(textShape); 37 38 TextField field = new TextField("operator"); 39 Widget widget = new Widget(210, page.Height - 60, 40, 10); 40 41 //prevent editing 42 field.ReadOnly = true; 43 44 widget.BackgroundColor = System.Drawing.Color.Yellow; 45 widget.Font = Font.HelveticaOblique; 46 widget.FontSize = 6; 47 widget.HorizontalAlignment = HorizontalAlignment.Center; 48 49 field.Widgets.Add(widget); 50 page.Widgets.Add(widget); 51 document.Fields.Add(field); 52 } 53 54 //Place clear total (C) and clear element (CE) button 55 placeCButton(document, page, 100, page.Height - 80); 56 placeCeButton(document, page, 140, page.Height - 80); 57 58 //Place number buttons 59 placeNumberButton(document, page, 7, 100, page.Height - 100); 60 placeNumberButton(document, page, 8, 140, page.Height - 100); 61 placeNumberButton(document, page, 9, 180, page.Height - 100); 62 63 placeNumberButton(document, page, 4, 100, page.Height - 120); 64 placeNumberButton(document, page, 5, 140, page.Height - 120); 65 placeNumberButton(document, page, 6, 180, page.Height - 120); 66 67 placeNumberButton(document, page, 1, 100, page.Height - 140); 68 placeNumberButton(document, page, 2, 140, page.Height - 140); 69 placeNumberButton(document, page, 3, 180, page.Height - 140); 70 71 placeNumberButton(document, page, 0, 100, page.Height - 160); 72 73 //Place other buttons 74 placeDotButton(document, page, 140, page.Height - 160); 75 placePlusMinButton(document, page, 180, page.Height - 160); 76 77 placePlusButton(document, page, 220, page.Height - 100); 78 placeMinusButton(document, page, 220, page.Height - 120); 79 placeDivideButton(document, page, 220, page.Height - 140); 80 placeMultiplyButton(document, page, 220, page.Height - 160); 81 82 placeEqualsButton(document, page, 220, page.Height - 180); 83 84 // write the modified document to disk, note: signing requires read-write file access 85 using (FileStream outFile = new FileStream(@"..\..\createjavascript.pdf", FileMode.Create, FileAccess.ReadWrite)) 86 { 87 document.Write(outFile); 88 } 89 90 }

VB.NET code sample

1 Shared Sub Main() 2 'New document 3 Dim document As New Document() 4 5 'Create some document-level JS. 6 document.JavaScripts.Add("calcJS", New JavaScript(File.ReadAllText("..\..\calculator.js"))) 7 8 'custom page size. 9 Dim page As New Page(280, 210) 10 document.Pages.Add(page) 11 12 'Create TextBox Field for the result 13 Dim textShape As New TextShape(25, page.Height - 40, "Result:", Font.Helvetica, 12) 14 page.VisualOverlay.Add(textShape) 15 16 Dim field As New TextField("result") 17 Dim widget As New Widget(70, page.Height - 40, 180, 15) 18 19 'prevent editing 20 field.[ReadOnly] = True 21 22 widget.BorderColor = RgbColor.Blue 23 widget.BorderWidth = 1 24 widget.HorizontalAlignment = HorizontalAlignment.Right 25 26 field.Widgets.Add(widget) 27 page.Widgets.Add(widget) 28 document.Fields.Add(field) 29 30 'Create TextBox Field for the current operator 31 textShape = New TextShape(170, page.Height - 60, "Operator:", Font.Helvetica, 9) 32 page.VisualOverlay.Add(textShape) 33 34 field = New TextField("operator") 35 widget = New Widget(210, page.Height - 60, 40, 10) 36 37 'prevent editing 38 field.[ReadOnly] = True 39 40 widget.BackgroundColor = System.Drawing.Color.Yellow 41 widget.Font = Font.HelveticaOblique 42 widget.FontSize = 6 43 widget.HorizontalAlignment = HorizontalAlignment.Center 44 45 field.Widgets.Add(widget) 46 page.Widgets.Add(widget) 47 document.Fields.Add(field) 48 49 'Place clear total (C) and clear element (CE) button 50 placeCButton(document, page, 100, page.Height - 80) 51 placeCeButton(document, page, 140, page.Height - 80) 52 53 'Place number buttons 54 placeNumberButton(document, page, 7, 100, page.Height - 100) 55 placeNumberButton(document, page, 8, 140, page.Height - 100) 56 placeNumberButton(document, page, 9, 180, page.Height - 100) 57 58 placeNumberButton(document, page, 4, 100, page.Height - 120) 59 placeNumberButton(document, page, 5, 140, page.Height - 120) 60 placeNumberButton(document, page, 6, 180, page.Height - 120) 61 62 placeNumberButton(document, page, 1, 100, page.Height - 140) 63 placeNumberButton(document, page, 2, 140, page.Height - 140) 64 placeNumberButton(document, page, 3, 180, page.Height - 140) 65 66 placeNumberButton(document, page, 0, 100, page.Height - 160) 67 68 'Place other buttons 69 placeDotButton(document, page, 140, page.Height - 160) 70 placePlusMinButton(document, page, 180, page.Height - 160) 71 72 placePlusButton(document, page, 220, page.Height - 100) 73 placeMinusButton(document, page, 220, page.Height - 120) 74 placeDivideButton(document, page, 220, page.Height - 140) 75 placeMultiplyButton(document, page, 220, page.Height - 160) 76 77 placeEqualsButton(document, page, 220, page.Height - 180) 78 79 ' write the modified document to disk, note: signing requires read-write file access 80 Using outFile As New FileStream("..\..\createjavascript.pdf", FileMode.Create, FileAccess.ReadWrite) 81 document.Write(outFile) 82 End Using 83 84 End Sub

The routines to create widgets:

C# code sample

1 private static void placeNumberButton(Document document, Page page, int number, double left, double bottom) 2 { 3 string javaScript = string.Format("number_pressed({0});", number.ToString()); 4 placeButton(document, page, left, bottom, "num" + number.ToString(), number.ToString(), javaScript); 5 } 6 7 private static void placeDotButton(Document document, Page page, double left, double bottom) 8 { 9 string javaScript = "dot_pressed();"; 10 placeButton(document, page, left, bottom, "dot", ".", javaScript); 11 } 12 13 private static void placePlusMinButton(Document document, Page page, double left, double bottom) 14 { 15 string javaScript = "plusMin_pressed();"; 16 placeButton(document, page, left, bottom, "plus_min", "+/-", javaScript); 17 } 18 19 private static void placePlusButton(Document document, Page page, double left, double bottom) 20 { 21 string javaScript = "operator_pressed('PLUS');"; 22 placeButton(document, page, left, bottom, "plus", "+", javaScript); 23 } 24 25 26 private static void placeMinusButton(Document document, Page page, double left, double bottom) 27 { 28 string javaScript = "operator_pressed('MINUS');"; 29 placeButton(document, page, left, bottom, "minus", "-", javaScript); 30 } 31 32 33 private static void placeDivideButton(Document document, Page page, double left, double bottom) 34 { 35 string javaScript = "operator_pressed('DIVIDE');"; 36 placeButton(document, page, left, bottom, "divide", "/", javaScript); 37 } 38 39 private static void placeMultiplyButton(Document document, Page page, double left, double bottom) 40 { 41 string javaScript = "operator_pressed('MULTIPLY');"; 42 placeButton(document, page, left, bottom, "multiply", "*", javaScript); 43 } 44 45 private static void placeEqualsButton(Document document, Page page, double left, double bottom) 46 { 47 string javaScript = "equal_pressed();"; 48 placeButton(document, page, left, bottom, "equals", "=", javaScript); 49 } 50 51 private static void placeCButton(Document document, Page page, double left, double bottom) 52 { 53 string javaScript = "clear_all();"; 54 placeButton(document, page, left, bottom, "c", "C", javaScript, true); 55 } 56 57 private static void placeCeButton(Document document, Page page, double left, double bottom) 58 { 59 string javaScript = "clear_entry();\nresult.value = 0;"; 60 placeButton(document, page, left, bottom, "Ce", "CE", javaScript, true); 61 } 62 63 private static void placeButton(Document document, Page page, double left, double bottom, string fieldName, string label, string javaScript) 64 { 65 placeButton(document, page, left, bottom, fieldName, label, javaScript, false); 66 } 67 68 private static void placeButton(Document document, Page page, double left, double bottom, string fieldName, string label, string javaScript, bool displayRed) 69 { 70 PushButtonField field = new PushButtonField(fieldName); 71 PushButtonWidget widget = new PushButtonWidget(left, bottom, 30, 15); 72 73 if (displayRed) 74 { 75 widget.BackgroundColor = System.Drawing.Color.Red; 76 widget.BorderColor = System.Drawing.Color.Gray; 77 widget.TextColor = System.Drawing.Color.White; 78 } 79 else 80 { 81 widget.BackgroundColor = System.Drawing.Color.LightGray; 82 widget.BorderColor = System.Drawing.Color.Gray; 83 } 84 widget.BorderStyle = BorderStyle.Beveled; 85 86 widget.Label = label; 87 88 field.Widgets.Add(widget); 89 page.Widgets.Add(widget); 90 document.Fields.Add(field); 91 92 //include action 93 widget.MouseUpActions.Add(new JavaScriptAction(javaScript)); 94 }

VB.NET code sample

1 Private Shared Sub placeNumberButton(ByVal document As Document, ByVal page As Page, ByVal number As Integer, ByVal left As Double, ByVal bottom As Double) 2 Dim javaScript As String = String.Format("number_pressed({0});", number.ToString()) 3 placeButton(document, page, left, bottom, "num" + number.ToString(), number.ToString(), _ 4 javaScript) 5 End Sub 6 7 Private Shared Sub placeDotButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 8 Dim javaScript As String = "dot_pressed();" 9 placeButton(document, page, left, bottom, "dot", ".", _ 10 javaScript) 11 End Sub 12 13 Private Shared Sub placePlusMinButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 14 Dim javaScript As String = "plusMin_pressed();" 15 placeButton(document, page, left, bottom, "plus_min", "+/-", _ 16 javaScript) 17 End Sub 18 19 Private Shared Sub placePlusButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 20 Dim javaScript As String = "operator_pressed('PLUS');" 21 placeButton(document, page, left, bottom, "plus", "+", _ 22 javaScript) 23 End Sub 24 25 26 Private Shared Sub placeMinusButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 27 Dim javaScript As String = "operator_pressed('MINUS');" 28 placeButton(document, page, left, bottom, "minus", "-", _ 29 javaScript) 30 End Sub 31 32 33 Private Shared Sub placeDivideButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 34 Dim javaScript As String = "operator_pressed('DIVIDE');" 35 placeButton(document, page, left, bottom, "divide", "/", _ 36 javaScript) 37 End Sub 38 39 Private Shared Sub placeMultiplyButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 40 Dim javaScript As String = "operator_pressed('MULTIPLY');" 41 placeButton(document, page, left, bottom, "multiply", "*", _ 42 javaScript) 43 End Sub 44 45 Private Shared Sub placeEqualsButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 46 Dim javaScript As String = "equal_pressed();" 47 placeButton(document, page, left, bottom, "equals", "=", _ 48 javaScript) 49 End Sub 50 51 Private Shared Sub placeCButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 52 Dim javaScript As String = "clear_all();" 53 placeButton(document, page, left, bottom, "c", "C", _ 54 javaScript, True) 55 End Sub 56 57 Private Shared Sub placeCeButton(ByVal document As Document, ByVal page As Page, ByVal left As Double, ByVal bottom As Double) 58 Dim javaScript As String = "clear_entry();" & Environment.NewLine & "result.value = 0;" 59 placeButton(document, page, left, bottom, "Ce", "CE", _ 60 javaScript, True) 61 End Sub 62 63 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, _ 64 ByVal javaScript As String) 65 placeButton(document, page, left, bottom, fieldName, label, _ 66 javaScript, False) 67 End Sub 68 69 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, _ 70 ByVal javaScript As String, ByVal displayRed As Boolean) 71 Dim field As New PushButtonField(fieldName) 72 Dim widget As New PushButtonWidget(left, bottom, 30, 15) 73 74 If displayRed Then 75 widget.BackgroundColor = System.Drawing.Color.Red 76 widget.BorderColor = System.Drawing.Color.Gray 77 widget.TextColor = System.Drawing.Color.White 78 Else 79 widget.BackgroundColor = System.Drawing.Color.LightGray 80 widget.BorderColor = System.Drawing.Color.Gray 81 End If 82 widget.BorderStyle = BorderStyle.Beveled 83 84 widget.Label = label 85 86 field.Widgets.Add(widget) 87 page.Widgets.Add(widget) 88 document.Fields.Add(field) 89 90 'include action 91 widget.MouseUpActions.Add(New JavaScriptAction(javaScript)) 92 End Sub

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

result-of-create-pdf-forms-with-javascript.png