Home » 2009 » June

iTextSharp – Adding Images

Maple leaves in Autumn.

Last week I showed how to use form fields to control placement of dynamic data.

But what if you want to dynamically place images in your PDF?  You can stuff them into a form field like you can with text.

However, one of the items you can retrieve from the form field is its location on the screen.  Using this, a little math, and some iTextSharp image code, we can place images in our PDF where the form field was located.  Here’s how I do it.

Continue reading “iTextSharp – Adding Images”

jQuery Dialog – With Validation Controls

sahara

Chances are, you’ll eventually want to use a dialog box in combination with some form elements, and when you do, you’ll probably want to implement some validation.

True, there are some great validation routines available in jQuery, but they only validate on the client side.  They are, after all, Javascript.

As you are probably aware, the advantage of using validation controls with ASP.NET is that they validate on both the client side and the server side and even if we assume that everyone using our web page is using Javascript and has it turned on, there is still the possibility that someone will turn Javascript off so they can circumvent your validations.

So how do we use validation controls AND allow the form to be a jQuery dialog?

Continue reading “jQuery Dialog – With Validation Controls”

iTextSharp – The easy way

iTextSharp The Easy Way

When I first started generating PDFs dynamically, I was overwhelmed by the complexity of the API.  Not just with iTextSharp, but it seemed that all of the APIs were complex.

In looking through the API and comparing it to what I was actually trying to accomplish, I found there was a very small subset of classes and methods that I needed to use to accomplish the task at hand.  Now that I’ve learned more, I still use this same subset of commands for 90% of what I need to do in iTextSharp.

Continue reading “iTextSharp – The easy way”

PDFs Using iTextSharp

iStock_000002747386Medium There are several libraries on the market now that allow you to create PDF documents from your .NET applications.  The one I’ve chosen to use is extSharp, an open source library that is a port of the open source library for Java,  iText.

While there are several sites on the Internet that provide examples of how to use iText, I’ve found that the documentation for iTextSharp is a little harder to come by.  So I thought it might be helpful if I provided some posts on how I use iTextSharp along with some of the gotchas I’ve encountered along the way.

-more–>

To use iTextSharp, you will need to add a reference to the library in your code, or simply drop the code into your bin directory of your ASP.NET application.

The main trick in translating the iText samples and documentation to iTextSharp is that iTextSharp makes the Java properties in iText (getPropertyName()/setPropertyName()) .NET properties, (PropertyName).

You’ll also need to know that the namespace for iTextSharp is different from iText.  Be sure to include the following line at the top of any code you write that uses iTextSharp

using iTextSharp.text;
using iTextSharp.text.pdf;

To get your ASP.NET page to return a PDF file, you’ll want to add the following code at the top of your Page_Load event handler:

Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("ContentType", "application/pdf");
Response.AddHeader("Content-Disposition", 
    "inline;filename=\"FileName.pdf\"");

The Response.Clear() line clears out any input that has already been sent back to the browser.

The next line tells the browser that what is coming back is a PDF, not HTML.

The “Content-Disposition” line allows the browser to know that we want the file IN the browser window rather than downloading the file.

Finally, yes, I know there are two ContentType lines.  But I took this from working code and while I can’t remember why I wrote it like that, it works.  If you can get it working with only one line, go for it.

Now that we have the basics out of the way, we can concentrate on actually generating PDFs in future posts.