←Select platform

WriteJavaScript(string,string,IList<PDFJavaScript>,string) Method


Writes (or removes) JavaScript actions to an output file.

public static void WriteJavaScript( 
   string sourceFileName, 
   string password, 
   IList<PDFJavaScript> Script_, 
   string destinationFileName 
public static void WriteJavaScript( 
   String^ sourceFileName, 
   String^ password, 
   IList<PDFJavaScript>^ Script_, 
   String^ destinationFileName 
def WriteJavaScript(self,sourceFileName,password,Script_,destinationFileName): 



Name of the source PDF file.


The password to use if sourceFileName contains an encrypted PDF file.


List of WriteJavaScript actions to be written. If null or the array is empty, the function deletes the JavaScript from the file.


Name of the destination PDF file.


If javaScript is not null and contains valid JavaScript actions, WriteJavaScript will:

  1. Copy the PDF file from sourceFileName
  2. Add the JavaScript actions
  3. Write the result to destinationFileName

If the source PDF file contains JavaScript, those operations will be replaced by the actions from javaScript. To add JavaScript actions to the existing JavaScript actions from the source file, follow these steps:

  1. Call PDFFile.ExtractJavaScript to read the existing JavaScript actions.
  2. Add your actions to the existing list.
  3. Write all the actions (existing and new) to the output file using WriteJavaScript.

If javaScript is null, WriteJavaScript will remove any JavaScript from the file.

If destinationFileName is null or is equal to sourceFileName, the change will be performed in place and the source file will be modified.

There are several variations of this function, which take the source from a disk file or a stream and write the output to a disk file or a stream.

The actions from javaScript will be sorted in ascending order by the PDFJavaScript.Name property. This means the order in which the JavaScript actions are written to the file and executed by the JavaScript interpreter is not necessarily the order in which you add them to the javaScript list.

The example below demonstrates the result of this sort. The example adds two actions named name2 and name1. The write however, changes the order and writes them in the order name1, name2. So when you load the resulting PDF file in a viewer that supports JavaScript (eg: web browser), the messages are displayed in reverse order.

using Leadtools.WinForms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
public void TestWriteJavaScript_string_string_IList_string() 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, @"leadtools.pdf"); 
   string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, @"out.pdf"); 
   List<PDFJavaScript> list = new List<PDFJavaScript>(2); 
   PDFJavaScript item; 
   item.Name = "name2"; 
   item.Code = "app.alert(\"Hello 2\");"; 
   item.Name = "name1"; 
   item.Code = "app.alert(\"Hello 1\");"; 
   PDFFile.WriteJavaScript(srcFileName, null, list, dstFileName); 
static class LEAD_VARS 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 

Target Platforms

Help Version 22.0.2023.3.31
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.