How to add javascript code to execute on starup in PDF document using ByteScout PDF SDK

  • Home
  • /
  • Articles
  • /
  • How to add javascript code to execute on starup in PDF document using ByteScout PDF SDK

The example demonstrates how to add JavaScript actions to document events with ByteScout PDF SDK.

Program.vb:

'*******************************************************************
'       ByteScout PDF SDK
'                                                                   
'       Copyright © 2016 Bytescout, http://www.bytescout.com        
'       ALL RIGHTS RESERVED                                         
'                                                                   
'*******************************************************************

Imports Bytescout.PDF

'''
<summary>
''' This example demonstrates how to add JavaScript actions to document events.
''' </summary>

Class Program

    Shared Sub Main()

        ' Create new document
        Dim pdfDocument = New Document()
        pdfDocument.RegistrationName = "demo"
		pdfDocument.RegistrationKey = "demo"
		' Add page
        pdfDocument.Pages.Add(New Page(PaperFormat.A4))

        ' Add action to document open event
        Dim onOpenDocument = New JavaScriptAction("app.alert(""OnOpenDocument"",3)")
        pdfDocument.OnOpenDocument = onOpenDocument

        ' Add action to document close event
        Dim onBeforeClosing = New JavaScriptAction("app.alert(""OnBeforeClosing"",3)")
        pdfDocument.OnBeforeClosing = onBeforeClosing

        ' Save document to file
        pdfDocument.Save("result.pdf")

        ' Cleanup 
		pdfDocument.Dispose()

        ' Open document in default PDF viewer app
        Process.Start("result.pdf")

    End Sub

End Class

Program.cs:

 //*******************************************************************
//       ByteScout PDF SDK		                                     
//                                                                   
//       Copyright © 2016 ByteScout - http://www.bytescout.com       
//       ALL RIGHTS RESERVED                                         
//                                                                   
//*******************************************************************

using System.Diagnostics;
using Bytescout.PDF;

namespace JavaScriptActionExample
{
	///
<summary>
	/// This example demonstrates how to add JavaScript actions to document events.
	/// </summary>

	class Program
	{
		static void Main()
		{
			// Create new document
			Document pdfDocument = new Document();
			pdfDocument.RegistrationName = "demo";
			pdfDocument.RegistrationKey = "demo";
			// Add page
			pdfDocument.Pages.Add(new Page(PaperFormat.A4));

			// Add action to document open event
			JavaScriptAction onOpenDocument = new JavaScriptAction("app.alert(\"OnOpenDocument\",3)");
			pdfDocument.OnOpenDocument = onOpenDocument;

			// Add action to document close event
			JavaScriptAction onBeforeClosing = new JavaScriptAction("app.alert(\"OnBeforeClosing\",3)");
			pdfDocument.OnBeforeClosing = onBeforeClosing;
			
			// Save document to file
			pdfDocument.Save("result.pdf");

			// Cleanup 
			pdfDocument.Dispose();

			// Open document in default PDF viewer app
			Process.Start("result.pdf");
		}
	}
}
prev
next