Articles in this section
Category / Section

How to add multiline JavaScript action to a PDF using C# and VB.NET

3 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add multiline JavaScript action to PDF using C# and VB.NET.

Steps to add multiline JavaScript action to PDF programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Interactive
Imports System.Drawing

 

  1. Use the following code snippet to add multiline JavaScript action to PDF.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a new page to the PDF document
PdfPage page = document.Pages.Add();
//Create the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20));
 
//Create a textbox field for name
PdfTextBoxField nameField = new PdfTextBoxField(page, "Name");
nameField.Bounds = new RectangleF(10, 40, 200, 20);
nameField.ToolTip = "Name";
document.Form.Fields.Add(nameField);
 
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80));
//Create a textbox field for email address
PdfTextBoxField emailField = new PdfTextBoxField(page, "Email address");
emailField.Bounds = new RectangleF(10, 100, 200, 20);
emailField.ToolTip = "Email address";
document.Form.Fields.Add(emailField);
 
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, new PointF(10, 140));
//Create a textbox field for phone
PdfTextBoxField phoneField = new PdfTextBoxField(page, "Phone");
phoneField.Bounds = new RectangleF(10, 160, 200, 20);
phoneField.ToolTip = "Phone";
document.Form.Fields.Add(phoneField);
 
//Create multiline JavaScript action
PdfJavaScriptAction action = new PdfJavaScriptAction(@"              
    var nameField = this.getField('Name');
    var emailField = this.getField('Email address');
    var phoneField = this.getField('Phone');                                                 
    nameField.value='Simons';
    emailField.value = 'simonsbistro@outlook.com';  
    phoneField.value = '31 12 34 56';
   ");
 
//Add the JavaScript action 
document.Actions.AfterOpen = action;
//Save the document
document.Save("JavaScriptAction.pdf");
//Close the document
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("JavaScriptAction.pdf");

 

VB.NET

'Create a new PDF document
Dim document As PdfDocument = New PdfDocument()
'Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
'Create the standard font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 16)
font = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
page.Graphics.DrawString("Name", font, PdfBrushes.Black, New PointF(10, 20))
 
'Create a textbox field for name
Dim nameField As PdfTextBoxField = New PdfTextBoxField(page, "Name")
nameField.Bounds = New RectangleF(10, 40, 200, 20)
nameField.ToolTip = "Name"
document.Form.Fields.Add(nameField)
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, New PointF(10, 80))
 
'Create a textbox field for email address
Dim emailField As PdfTextBoxField = New PdfTextBoxField(page, "Email address")
emailField.Bounds = New RectangleF(10, 100, 200, 20)
emailField.ToolTip = "Email address"
document.Form.Fields.Add(emailField)
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, New PointF(10, 140))
 
'Create a textbox field for phone
Dim phoneField As PdfTextBoxField = New PdfTextBoxField(page, "Phone")
phoneField.Bounds = New RectangleF(10, 160, 200, 20)
phoneField.ToolTip = "Phone"
document.Form.Fields.Add(phoneField)
 
'Create multiline JavaScript action
Dim action As PdfJavaScriptAction = New PdfJavaScriptAction("              
        var nameField = this.getField('Name');
        var emailField = this.getField('Email address');
        var phoneField = this.getField('Phone');                                              
        nameField.value='Simons';
        emailField.value = 'simonsbistro@outlook.com';  
        phoneField.value = '31 12 34 56';
       ")
'Add JavaScript action
document.Actions.AfterOpen = action
'Save the document
document.Save("JavaScriptAction.pdf")
'Close the document
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("JavaScriptAction.pdf")

 

A complete working sample can be downloaded from PdfSample.zip.

By executing the program, you will get the output document (Form fields are filled using JavaScript action) as follows. Output document screenshot

Take a moment to peruse the documentation, where you can find the other options like adding an action to the PDF, supported action types, adding an action to the bookmarks and adding actions to form fields with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied