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

Free Support Forum - aspose.com

Please help with Aspose.Pdf.Generator.Pdf with Javascript

I have a PDF content that I would like to insert some javascript (for “silent-print” from APS.Net page.

Could I please have some help with the code below?

//Get PDF contents
//This is only a test PDF content, you can use any available PDF
//In my app, the contents will be read from Docx byte array into stream
//The real contents is stored in Oracle
Aspose.Words.Document asposeWordDoc = new Aspose.Words.Document(@“C:\TFS\Aspose\Aspose.Test\View\source.docx”);
//Convert to PDF stream
Aspose.Words.Saving.PdfSaveOptions pdfOptions =
new Aspose.Words.Saving.PdfSaveOptions();
asposeWordDoc.Save(pdfStream, pdfOptions);

//Read PDF stream into PDF instance
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf(pdfStream);

//Instantiate Javascript object
pdf.JavaScripts = new Aspose.Pdf.Generator.JavaScripts();
//The next two lines are from your sample
pdf.JavaScripts.Add(“app.alert(“hello world”);”);

//Save the new PDF content into a new stream
//This is where there error pop-ups:
//You are in direct-to-file mode,please use Close() instead of Save().
MemoryStream resultStream = new MemoryStream();

//I also tried to dump the PDF content from pdfStream above but it is empty.
File.WriteAllBytes(@“C:\temp\source.With.Javascript.pdf”, pdfStream.ToArray());

What I am trying to do here is to log how many times the PDF content is printed from ASP.Net page by the client and not allow the content to be saved. If you have any idea or know why the error above occurred, please help.
Thank you very much in advance.

Hi Phong,

Thanks for using our products.

As far as I can understand from your code snippet, you are trying to read the contents of source docx file and saving them into pdfStream object. Then you are passing pdfStream object as an argument to Aspose.Pdf.Generator.Pdf object and trying add some JavaScript to it. Please note that Aspose.Pdf.Generator.Pdf object only supports the capability to create PDF documents from
scratch and I am afraid it does not support the feature to open an existing PDF
file or other file format. As per your requirement, if you need to apply some
security restrictions on the PDF document, you may try using Document class in
Aspose.Pdf namespace or an object of PdfFileSecurity class (present inside
Aspose.Pdf.Facades namespace). For more information, please visit the following

Please note that you may also apply security restrictions
over resultant document being generated with Aspose.Words and mark the files as read only by using ReadOnly value of ProtectionType enumeration. Please try using following code line Document.Protect(ProtectionType.AllowOnlyFormFields);. For more information, please checkout Protecting Documents

In case I have not properly understood your requirement or you have any further query, please feel free to contact. We apologize for your inconvenience.

Hello Nayyer,

Yes, you have understood the scenario perfectly.
I will try the other methods as you have suggested and will ask for more help if needed.
Thank you very much for the promptly reply.

I’m actually interested in adding an auto-print JavaScript snippet to a
PDF that I originally opened from a file as a Aspose.Words.Document
object. The Aspose.Pdf.Generator.Pdf API says I should be able to
create an Aspose.Pdf.Generator.Pdf object from a Stream.

I get an error with the following code:

Document doc = new Document(fileIn);

MemoryStream inputMemStream = new MemoryStream();

doc.Save(inputMemStream, SaveFormat.Pdf);

inputMemStream.Position = 0;

Pdf pdf = new Pdf(inputMemStream);

pdf.JavaScripts = new Aspose.Pdf.Generator.JavaScripts();



Unhandled Exception: System.ApplicationException: You are in direct-to-file mode,please use Close() instead of Save()

Is there a way to easily add auto-print JavaScript to an existing pdf?


Thanks for using our products.

In order to add JavaScript to existing PDF documents, please try using CreateJavaScriptLink method of PdfContentEditor class. Please take a look over following code snippet. For your reference, I have also attached the resultant PDF document that I have generated.


// create PDFContentEditor object
PdfContentEditor editor = new PdfContentEditor();
// bind the source PDF document
// add java script and specify the region where JavaScript link will be added. Also specify the page number where link will be added
editor.CreateJavaScriptLink(“this.print({bUI:true,bSilent:false,bShrinkToFit:true});”, new System.Drawing.Rectangle(0, 0, 100, 100), 1, System.Drawing.Color.Red);
// save updated PDF document