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

Free Support Forum - aspose.com

Can't add document-level Javascript to a PDF

Hello, using Aspose.PDF .NET 19.12, I’m trying to add document-level Javascript to a PDF. Here is the code:

using (Document document = new Document(new FileStream(path, FileMode.Open, FileAccess.ReadWrite)))
	document.JavaScript["hello"] = "function hello(){ app.alert('hello world'); }";

And here is the error I get:

  Message=Value could be added only to leaf
   at #=z_uXQUUTOD1x0nWp6D5Ny5DdYGhgbieGG9A==.#=zpbagGsk=(#=zFuVY$ug5e0MI6nubBkMDBMp0k6Hkv$K22g== #=zgMzEBeY=, #=zFuVY$ug5e0MI6nubBkMDBMp0k6Hkv$K22g== #=zgRwocA0=)
   at Aspose.Pdf.JavaScriptCollection.#=z7VQd2Js=(#=zDfrLeQu7MCbAmnqNqqTFh7dI979_v2ogUg== #=zWIK7VNA=, String #=zgMzEBeY=, #=zFuVY$ug5e0MI6nubBkMDBMp0k6Hkv$K22g== #=zgRwocA0=)
   at Aspose.Pdf.JavaScriptCollection.set_Item(String key, String value)
   at AddJavascriptToPDF.AddJavascriptToPDF.Add(String path) in D:\...\AddJavascriptToPDF.cs:line 44
   at AddJavascriptToPDF.Program.Main(String[] args) in D:\...\Program.cs:line 24

If I have some existing Javascript in the document, Aspose.PDF will add the hello script correctly, but not if I am adding new document-level Javascript to the document.

The PDF is a simple form with an input text field and a digital signature.


Thank you for contacting support.

Would you please share source PDF document so that we may try to reproduce and investigate it in our environment.

Thanks for your assistance. Here is the PDF:
test_sign.pdf (70.2 KB)


Thank you for contacting support.

Would you please try below code snippet and then share if it suites your requirements.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "test_sign.pdf");
doc.OpenAction = new Aspose.Pdf.Annotations.JavascriptAction("app.alert('hello world');");
doc.Save(dataDir + "yazdir_19.12.pdf");

I did try that but unfortunately it does not fulfill my requirements. I need to create a document-level function that I can call later on from an Adobe Reader plugin, not a Javascript action that executes on an event.


Thank you for your kind feedback.

We have logged a ticket with ID PDFNET-47432 in our issue management system for further investigations. We will let you know once any update will be available in this regard.