PDFs and iTextSharp
http://itextsharp.sourceforge.net/
For the last day and a half, I've been using iTextSharp to create a PDF document, display it in an IFRAME on a web page then print it out. Here I'm going to share some of the tips and techniques that I found while making this happen.
First, there doesn't seem to be an easy way to print a PDF from a web page. What I ended up doing is executing a little javascript function from the iFrame's onLoad event. Since this event fires when the PDF starts loading, not when it finishes, I have a timer that waits for a few seconds to give the PDF time to load before it tries to print the contents of the iFrame. The function looks like this:
From what I was able to discover on the net, there is no way to print directly to the printer without first displaying the printer confirmation dialog using this method. It may be possible to do this by attaching javascript directly to the PDF document, but as doing so would have required me to do a lot of research into a part of iTextSharp not directly essential for this project, I opted not to pursue that path. A dialog is not such a big deal.
As far as iTextSharp goes:
What I like:
http://itextsharp.sourceforge.net/
For the last day and a half, I've been using iTextSharp to create a PDF document, display it in an IFRAME on a web page then print it out. Here I'm going to share some of the tips and techniques that I found while making this happen.
First, there doesn't seem to be an easy way to print a PDF from a web page. What I ended up doing is executing a little javascript function from the iFrame's onLoad event. Since this event fires when the PDF starts loading, not when it finishes, I have a timer that waits for a few seconds to give the PDF time to load before it tries to print the contents of the iFrame. The function looks like this:
Of course, to get this far one has first to set the SRC of the iFrame. This is easily possible through javascript but it is also possible in the codebehind, using the attributes collection, as follows:
var mTID;
//function waits 7 seconds before trying to print the iframe, to give the PDF time to load.
function StartClock()
{
mTID=setInterval('PrintPDF()',5000);
}
//function prints the contents of the iframe. Hopefully the iframe will be occupied
// by acrobat reader, which will override the browser's print functionality.
function PrintPDF()
{
frames['PDFLINK'].focus();
frames['PDFLINK'].print();
clearInterval(mTID);
alert('print');
}
' the iframe has runat="server" and ID="PDFLINK"
PDFLINK.Attributes("src") = oReport.DocumentURL '<--URL of PDF document to display
From what I was able to discover on the net, there is no way to print directly to the printer without first displaying the printer confirmation dialog using this method. It may be possible to do this by attaching javascript directly to the PDF document, but as doing so would have required me to do a lot of research into a part of iTextSharp not directly essential for this project, I opted not to pursue that path. A dialog is not such a big deal.
As far as iTextSharp goes:
What I like:
- Doesn't require installation (IE: not COM) so I can run it on a hosted server.
- Very popular, so there's a lot of info out there, and it seems to be actively updated and supported.
- It's open source, so if I needed to I could dig into the code to see how something really does work.
- It's an open source project, so the documentation sometimes falls a version or two behind the production code. IE: The documentation says "do something", you try it and it doesn't work because the way to do that thing has changed in the most current version. Note: This doesn't happen often.
- As a way to create PDF documents, iTextSharp(and I suppose any other direct PDF creation tool) is going to be labor intensive. You spend a lot of time creating a test document, changing a line thickness or a cell width then recreating the document. This is not iTextSharp's fault--that's just the way it is, and is something to think about if you're considering using iTextSharp as a reporting tool.
0 Comments:
Post a Comment
<< Home