Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I'm trying to print a pdf. Actually I'm showing it to the user with a Response, and works fine. I would like to show even the print dialog (or printing directly), because I need to force the printing of 2 copies.

This is my code, but it doesn't work, nothing happens (after the response)!

TestPdf is a method inside an aspx page that is inside an IFrame in another page. The master page call the slave passing the bytearray of the pdf. Then in the IFrame the pdf is shown, but not the print dialog.
What's wrong?
Thank you in advance

What I have tried:

C#
protected void TestPdf(byte[] fileStream)
        {
            var outputStream = new MemoryStream();
            var pdfReader = new iTextSharp.text.pdf.PdfReader(fileStream);
            var pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputStream);
            pdfStamper.SetPageAction(iTextSharp.text.pdf.PdfWriter.PAGE_OPEN, new iTextSharp.text.pdf.PdfAction(iTextSharp.text.pdf.PdfAction.PRINTDIALOG), 1);
            var writer = pdfStamper.Writer;
            writer.AddJavaScript(GetAutoPrintJs());
            pdfStamper.Close();
            var content = outputStream.ToArray();
            outputStream.Close();
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(content);
            Response.End();
            outputStream.Close();
            outputStream.Dispose();
        }
protected string GetAutoPrintJs()
        {
            var script = new StringBuilder();
            script.Append("var pp = getPrintParams();");
            script.Append("pp.interactive= pp.constants.interactionLevel.full;");
            script.Append("pp.NumCopies=eval(2);");
            script.Append("print(pp);");
            return script.ToString();
        }
Posted
Comments
ZurdoDev 5-May-17 7:37am    
Does iTextSharp allow you to specify how many copies? You should some C# code that looks like JavaScript but that won't run when you Response.End(), which is necessary for the attachment to come down.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900