Docotic.Pdf Library Help > Samples > Actions > JavaScript Action

This sample shows how to use JavaScript actions.

You can execute javascript code as the result of an action. Use PdfDocument.CreateJavaScriptAction() method to create a javascript action.

C#
using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class JavaScriptAction
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "JavaScriptAction.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.OnOpenDocument = pdf.CreateJavaScriptAction("app.alert(\"Hello from JavaScript.\",3)");

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class JavaScriptAction
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "JavaScriptAction.pdf"

            Using pdf As New PdfDocument()
                pdf.OnOpenDocument = pdf.CreateJavaScriptAction("app.alert(""Hello from JavaScript."",3)")

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace