How to directly print a PDF dynamically generated with iTextSharp using asp.net C #?

I've looked around a lot and I can't seem to find a way that works for me. I am creating a website and I have two buttons: a Download button (which works great) and a Print button that should print the PDF (or even just open a PDF document in Adobe with the print dialog open).

The biggest difference between my question and many others is that I am not trying to create a new document. My PDF is generated using a PDF template that I have already created.

I just need a way to print the document. I cannot save it to the server because I want the client to be able to print it. I tried MemoryStream but it doesn't work (I probably didn't write it correctly, code below). The title is grabbed from another page.

using (var ms = new MemoryStream())
{
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", header);   /

    Response.Buffer = true;
    Response.Clear();
    var bytes = ms.ToArray();
    PdfReader r = new PdfReader(template);         
    using (PdfStamper ps = new PdfStamper(r, Response.OutputStream))    
    {
        AcroFields af = ps.AcroFields;
        ...
        ps.FormFlattening = true;
    }
    Response.OutputStream.Write(bytes, 0, bytes.Length);
    Response.OutputStream.Flush();

      

Again, I want the client to be able to print this PDF that is generated after clicking the Print button.

+3


source to share


1 answer


Before going too far down this path, I just want to make sure that some things are explained correctly. I do not know your level of knowledge, so please don't be offended if I say something that is obvious.

First, you say that you have two buttons: Download and Print. We know what you intend to do, but it is important to know and understand that both of these options are "download". Your "Download" button will indeed "load and (maybe) prompt to save" and your "Print" button will indeed "load", then if the person has a PDF tuner configured in their browser (which most modern browsers have), PDF will otherwise prompt you to save. "This might be very obvious to you, but I just wanted to make sure it's clear. I personally don't have the PDF rendering associated with my default browser, so all PDFs are downloaded for me every time.

Second, the server is able to send one and only one "thing" back to the client in response to a request. This "thing" may have metadata (headers) associated with it, but there are very few standard meta keys that matter. One clue (content-location) is whether this response should be treated as "inline" or "attachment" relative to the initiating request. Some browsers see the latter as a "download and (possibly) save prompt", but some still run the associated application. There's another clue (mime type application/octet-stream

) in there that some use to further use browsers, which essentially say "seriously dude, this thing I'm posting is so weird that it would be impossible for you to figure it out.just save it to disk. "

This is very important because there is no "please print this" thing that you can send from the server, which is in any standard specification (or any specification I've ever seen for that matter). Also, the server can't even say "open this in a new tab or browser window". Both print and window / tab controls are client side functions, so you need to attack from that angle.



So, now for some parameters. All of these options assume your clients have modern browsers with built-in and enabled PDF renderers.

One option you could use is to wrap your PDF iniframe

, which has JavaScript in it to call window.print()

upon loading
. Google used (and probably still does) for their calendar. You will need to test this with various client renderers to make sure it works as you expect.

Another option is to try using helpPDFObject

to insert the PDF and then use the same JavaScript as mentioned above.

One of the last options - slightly change the PDF generation code and add JavaScript to the PDF - tries to print the PDF on load. JavaScript support in PDF renderers is not universal, so you need to check if this works with your clients. In addition, this setting is "baked" into the PDF you make, so if someone saves it to disk, every time they open it, they will try to print, which is very annoying.

+9


source







All Articles