Dave's Notebook

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. The reason we produce PDFs programmatically at all is because we need to dynamically generate some information on the page.  Most of the time, this information comes out of a database and gets placed on the same location of the page each time the page is generated.  The rest of the information is static. So what I normally do is have my designer or project manager create a PDF for me with form fields located where he wants the information to go.  Using the form fields, he can define the font, size, color, and position he wants to display the text with.  All I have to worry about is getting the text into the field. This works out nicely because once I’ve filled in the forms, he can move them around until he’s happy with them without asking for my help. We’ve already covered setting the MIME type information in our first post, so the rest of this discussion will assume you’ve already done that. The next thing you’ll want to do is load the PDF document that has the form fields in it and create a stamper object.  The stamper is what we use to grab the form fields object which we will use to set the form field values.

Read More

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.

Read More

C# “” better than string.Empty?

arct-013I recently read an article that argued that “” is “Better than String.Empty”

The argument is that since string.Empty doesn’t work in all situations, we should not use it at all.  He further argues that since the compiler can’t optimize code using string.Empty, the performance gains we might lose due to our lack of this optimization further supports the argument that we should not use it at all.

But at what price?

Read More

VB.NET - Char from String with Option Strict

G04B0079 So here’s the question:

I’m using String.Split() and need to pass in a Char or a Char array as the parameter.  If I pass in a string String.Split(“/“) I get an error “Option Strict On disallows implicit conversions from ‘String’ to ‘Char’.”

Obviously, the easiest way to fix this would be to turn off Option Strict, but I would prefer to keep it on.  So how do I pass in the Char instead of a String in this situation?”

There are actually several ways to accomplish what you are trying to do.

Read More

Infinite 302 Loop - How would you even know?

trav-053 It happens eventually to all web developers.  It’s happened to me twice in the last week.  You make one simple change to your web site and then you can no longer access it.  When you browse to it in IE you get some completely useless “We can’t access the page you are looking for” error.  In FireFox, it at least tells you that you have a “Redirect Loop” and in IE6, if you are fortunate enough to still be writing for that browser, you can see that it is blinking at you as it tries to retrieve the page(s) multiple times.  But how do you know what’s redirecting to what? For that matter, other than running a browser that told you, how would you even know it was a 302 error?

Read More

ASP.NET Application_Error Detecting 404's

misc_vol3_046 For many of you, this is going to be a “Duh!” kind of post.  But while working on this today, I found so many people asking this question and so many others giving the wrong answer, I’m compelled to post anyhow. If you know the answer, then you are welcome to stop reading now.  I didn’t write this for you.  I wrote this for the hundreds of people who will search for this information and won’t be able to find the answer.  The fact of the matter is, that’s why I write most of what I write–so people searching for the information can find it. So here’s the question: I’ve set up an Application_Error event handler in my Global.asax file and I have implemented a server transfer for errors.  Now I want to set up a specific page to handle 404 errors.  How do I detect a 404 error and call the 404-specific page? The main answer to this question involves retrieving the exception that triggered the event in the first place.  To do that, we call Server.GetLastError():

Read More