We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Cannot insert document-level JavaScript like itext does

We are currently using itext to insert javascript into our PDFs. I am evaluating Aspose to see if we can switch from itext but I have not been able to see the same insert javascript functionality. When using itext I can add the JavaScript like so...

PdfReader reader;
PdfStamper stamper;

//Get the contents of the javascript function(jsFunctionName)
string[] js_content = File.ReadAllLines(Path.Combine("insert_js.txt");

reader = new PdfReader(@input_file);
stamper = new PdfStamper(reader, new FileStream(output_file, FileMode.Create));

//Add Document-Level JavaScript
StringBuilder sb = new StringBuilder();
foreach (var line in js_content)
stamper.AddJavaScript("jsFunctionName", sb.ToString());


This produces a PDF that looks like the Capture.PNG attachment if you look at it inside Acrobat Javascripts.The code I am trying with Aspose does not even have an option to enter the script name. Am I missing something? The resulting pdf does not have any scripts added in the same pane.

string[] js_content;
js_content = File.ReadAllLines("insert_js.txt");
//// Load PDF document
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(inputFile);

//Instantiate JavascriptAction with desried JavaScript statement
JavascriptAction javaScript = new JavascriptAction(js_content.ToString());
doc.OpenAction = javaScript;


Hi there,

Thanks for your inquiry. We have noticed that Aspose.Pdf is adding JavaScript as Document Action instead Document-level, so we have logged a ticket PDFNEWNET-39455 for further investigation and resolution. We will keep you updated about the issue resolution.

We are sorry for the inconvenience caused.

Best Regards,

My evaluation of Aspose has finished. Is there any update you can give me on this bug before we can decide if Aspose is right for us?

Hi Josh,

Thanks for your inquiry. I am afraid your reported issue is still not resolved as product team is busy in resolving other issues in the queue, reported earlier. However I have raised the priority of your issue and requested our team to share ETA at their earliest. We will notify you as soon as we made some significant progress towards issue resolution.

We are sorry for the inconvenience caused.

Best Regards,

The issues you have found earlier (filed as PDFNEWNET-39455) have been fixed in Aspose.Pdf for .NET 11.1.0.

This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi Josh,

Thanks for your patience. In reference to above fix, in Aspose.Pdf for .NET 11.1.0 we have added a new property Document.JavaScript. It will be used to add Document level JavaScript. This property has type JavaScript collection and provides access to JavaScript by its key.

JavaScript collection has the following properties and methods:

string this[string key] { get; set; } - Gets or sets JavaScript by its name
IList Keys - provides list of existing keys in JavaScript collection
bool Remove(string key) - removes JavaScript by its key.

Document doc = new Document(myDir+“HelloWorld.pdf”);<o:p></o:p>


doc.JavaScript[“func1”] = “function func1() { hello(); }”;<o:p></o:p>

doc.JavaScript[“func2”] = “function func2() { hello(); }”;<o:p></o:p>


Document doc1 = new Document(myDir+“js.pd”);<o:p></o:p>

IList<string> keys = doc1.JavaScript.Keys;<o:p></o:p>

foreach (string key in keys)<o:p></o:p>


Console.WriteLine(key + " ==> " + doc1.JavaScript[key]);<o:p></o:p>



<o:p> </o:p>

Please feel free to contact us for any further assistance.

Best Regards,

Thank you for the code sample on how to do this. Where can I find this documented in the official documentation? I was having a hard time finding this until I found this thread again.


Hi Josh,

We are sorry for the inconvenience. It is good to know that you have managed to find the required information. However, we have added the details to add document level java script in following documentation link, hopefully it will help you to accomplish the task.

Best Regards,