Free Support Forum - aspose.com

Adding Javascript an existing PDF file

Hi I have a pdf file and I want to see printdialog when client users click the document.

I use the following code

FileStream

fs = new FileStream(su.MapPath("../../QDMSFiles/TEMPFOL/") + tempFileName, System.IO.FileMode.Open);

Byte[] buffer = new Byte[fs.Length];

fs.Read(buffer, 0, (

int)fs.Length);

PdfContentEditor

contentEditor = new PdfContentEditor();

contentEditor.BindPdf(fs);

System.Drawing.

Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);

contentEditor.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

contentEditor.Save(

"c:/test2.pdf");

But printdialog appears when I click rectangle I want to see print dialog when pdf load

Hi there,


Thanks for your inquiry. You can add JavaScript to OpenAction() method of document class for the desired results. Please check following code snippet. Hopefully it will serve the purpose.

Document doc = new Document(myDir+“test.pdf”);
doc.OpenAction = new JavascriptAction(“this.print({bUI:true,bSilent:false,bShrinkToFit:true});”);
doc.Save(myDir + “JavaScript_Output.pdf”);

Please feel free to contact us for any further assistance.

Best Regards,