Examples

Javascript actions in PDF (.NET Core/Framework)

Create auto fillable, self formatting and validating form fields using javascript in PDF using Generator for .NET. These actions can be used to perform tasks in many places such as in outlines, links, buttons or in any Document, Page or Form Field Reader Events.

How to add AutoFillable Form fields using Javascript to PDF in C#

Below are the steps and sample code to add a AutoFillable Form fields using Javascript to PDF document using the Generator product.

Steps for Adding AutoFillable Form fields using Javascript to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a TextField object and assign javascript to fill another Formfield, for OnBlue event using the JavaScriptAction object.
  4. Create the TextField object that needs to be updated automatically based on the event.
  5. Add all the Formfields to the Page.
  6. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add( page );

ceTe.DynamicPDF.PageElements.Label label1 = new ceTe.DynamicPDF.PageElements.Label("Please Enter your Date of Birth :", 50, 50, 200, 50);
TextField textField1 = new TextField("dob", 270, 50, 100, 50);
textField1.ReaderEvents.OnBlur = new JavaScriptAction(" var no = this.getField(\"dob\").value; var temp = Math.abs(new Date(Date.now()).getTime() - new Date(no).getTime()); var days = Math.ceil(temp / (1000 * 3600 * 24));
this.getField(\"age\").value = Math.floor(days/365); ");
page.Elements.Add(label1);
page.Elements.Add(textField1);
ceTe.DynamicPDF.PageElements.Label label2 = new ceTe.DynamicPDF.PageElements.Label("Your Age is :", 50, 120, 100, 50);
TextField textField2 = new TextField("age", 270, 120, 100, 50);
page.Elements.Add(label2);
page.Elements.Add(textField2);

document.Draw( "output.pdf" );

How to add Formfields with Javascript for field value formatting to PDF in C#

Below are the steps and sample code to add a Formfields with Javascript for field value formatting to PDF document using the Generator product.

Steps for Adding Formfields with Javacreate a PDF in Javascript for field value formatting to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a TextField object and assign javascript to format the input, for OnBlue event using the JavaScriptAction object.
  4. Add all the Formfields to the Page.
  5. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add( page );

ceTe.DynamicPDF.PageElements.Label label1 = new ceTe.DynamicPDF.PageElements.Label("Please Enter a Number :", 50, 50, 200, 50);
TextField textField1 = new TextField("number", 270, 50, 100, 50);
textField1.ReaderEvents.OnBlur = new JavaScriptAction(" var no = this.getField("number").value; this.getField(\"number").value = no.toFixed(2) ; ");
page.Elements.Add(label1);
page.Elements.Add(textField1);

document.Draw( "output.pdf" );

How to add Formfields with Javascript for field value validation to PDF in C#

Below are the steps and sample code to add a Formfields with Javascript for field value validation to PDF document using the Generator product.

Steps for Adding Formfields with Javascript for field value validation to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a TextField object and assign javascript to validate input, for OnBlue event using the JavaScriptAction object.
  4. Add all the Formfields to the Page.
  5. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add( page );

Label label = new Label("Please Enter a Number :", 0, 50, 150, 30);
ceTe.DynamicPDF.PageElements.Forms.TextField textField = new ceTe.DynamicPDF.PageElements.Forms.TextField("txt", 170, 30, 150, 30);
textField.DefaultValue = "0";
textField.ToolTip = "Enter only Numbers";
textField.ReaderEvents.OnBlur = new JavaScriptAction(" var no = this.getField(\"txt\").value; if( isNaN(no)) { app.alert(\"Please Enter number in the text field\"); } ");
page.Elements.Add(textField);
page.Elements.Add(label);

document.Draw( "output.pdf" );

How to add Form level Javascript Action to PDF in C#

Below are the steps and sample code to add a Form level Javascript Action to PDF document using the Generator product.

Steps for Adding Form level Javascript Action to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a JavaScriptAction object and provide the script to be executed.
  4. Create a Button object and assign Javascript to the button action.
  5. Add the Button to the Page.
  6. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add( page );

JavaScriptAction action = new JavaScriptAction("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");

Button button = new Button( "Button Name", 200, 200, 100, 25 );
button.Behavior = Behavior.Push; 
button.Label = "Submit";
button.Action = action;

page.Elements.Add( button ); 
document.Draw( "output.pdf" );

How to add Document level Javascript Action to PDF in C#

Below are the steps and sample code to add a Document level Javascript Action to PDF document using the Generator product.

Steps for Adding Document level Javascript Action to a PDF Document

  1. Create a Document object.
  2. Add the javascript to the document by creating and passing DocumentJavaScript object.
  3. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add(page);

document.JavaScripts.Add( new DocumentJavaScript( "Say Hi", "app.alert(\"Hello!!\")" ) ); 	
document.Draw( "output.pdf" );

How to Call Document Level JavaScript from a Form in C#

Below are the steps and sample code to call Document Level Javascript from a form in PDF document using the Generator product.

Steps for Calling Document Level JavaScript from a Form in a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a DocumentJavaScript object and assign the script to be executed.
  4. Create a JavaScriptAction object and set the parameters.
  5. Create a Button object to trigger the action.
  6. Add the Button to the Page.
  7. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add( page );

document.JavaScripts.Add( new DocumentJavaScript( "Say Bye", "function bye(){app.alert(\"Good Bye!!\")}" ) );
JavaScriptAction action = new JavaScriptAction("bye()");
Button button = new Button( "Button", 200, 300, 100, 25 );
button.Behavior = Behavior.Push; 
button.Label = "Say Bye";
button.Action = action;
page.Elements.Add( button ); 
	
document.Draw(@"C:\Output.pdf");

Getting Started

NuGet Package

DynamicPDF Generator is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET package. The Core Suite package includes Generator, Merger and ReportWriter.

NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

DynamicPDF Generator Information

More information can be found here:

Available in other platforms

DynamicPDF Generator is available for the following platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

  • Transparent Pricing
  • Lots of Features
  • Easy to Use
  • Great Support
  • Efficient Performance
  • Product Maturity (Over 18 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

We’re Not The Only Ones That Think We’re Great!