8

I am trying to embed JavaScript into pdf that is created using iTextSharp and it is working absolutely fine if it is a file as shown in the code.

But when I try to embed the javascript part into a memory stream it is not working. Are there any limitations of iTextSharp??

Dim js As New StringBuilder
                Dim pdf As String = "c:\Print2Printer.pdf"
                Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdf, FileMode.Create))
                doc.Open()

                js.Append("var pp = this.getPrintParams();")
                js.Append("var iCopies = 2;")
                js.Append("pp.interactive = pp.constants.interactionLevel.silent;")
                js.Append("for ( var i = 0; i < 3; i++ ) { pp.firstPage = i; pp.lastPage = i;")
                js.Append("this.print(pp);")
                js.Append("}")
                Dim jaction As PdfAction = PdfAction.JavaScript(js.ToString(), writer)
                writer.AddJavaScript(jaction)

                doc.Add(New Paragraph(pdfString))
                doc.Close()
2
  • 1
    If all you're doing is replacing the FileStream with a MemoryStream, then it should be functionally identical. You still have to put that memory stream somewhere you can look at it (like a file) in order to see the JS and its effects. How are you opening the memoryStream? Commented Dec 21, 2010 at 17:18
  • @rowmark, what is the argument that you have passed for New Paragraph(pdfString). Commented May 31, 2013 at 11:29

1 Answer 1

1

Your PDF is not rendered yet... I am not sure if PDF has a onreadystate event or not but see... http://mattheyan.blogspot.com/2010/06/add-javascript-to-pdf-document-with.html

In short you need a setTimeout

Here is an example using Docotic.Pdf http://www.codeproject.com/Articles/380293/Javascript-in-PDF

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.