ITextSharp Zoom Preference on PDF


As many of you know, ITextSharp is a tool often used to programmatically create a PDF using .NET C#. This can be very useful for having to design forms or images with data in it dynamically. Maybe you want the ability to have an image of a ticket and fill in the blank fields with dynamic data an end user provides you. This was actually the task I was just faced with and I discussed it in more detail in another post. This post will be about customizing the adobe plugin viewer settings to have things like the tool bar hidden and the zoom settings at a specific zoom.

The Problem

So now that we discussed a reason for IText, lets come up with a scenario for setting the Viewer Settings. lets say that the image of these tickets were scanned in at 600dpi and the adobe tool is scaling it down to 96dpi. Obviously this is too small to work with given where it started, so it needs to be scaled correctly. After we do the scaling magic and remove any margins etc… now we are left with an image that’s zoom is defaulting to 240%, which is a great size for testing and working with, but not for viewing lets say three checks on one page (8.5 x 11 inch). So, as usual, you leave that for last and get all the field coordinates where they need to be on the ticket and now it’s time to deal with that scaling issue.

Fixing the Problem

Among the many things that iTextSharp has, it also allows you to set some of the settings in the Adobe plugin prior to the rendering of the PDF. Below is the code to fix the zoom issue.

using iTextSharp.text;

using iTextSharp.text.pdf;

using System.IO;

string filePath = @"C:\PDFFolder\SuperDuperPdf.pdf";

Document doc = new Document();

PdfReader reader = new PdfReader(filePath);

using (MemoryStream docStream = new MemoryStream())


          PdfWriter writer = PdfWriter.GetInstance(doc, docStream );


          PdfContentByte pdfByte = writer.DirectContent;

          float zoomNumber = 100.0;
          int pageNumberToOpenTo = 1;

          PdfDestination magnify = new PdfDestination(PdfDestination.XYZ, -1, -1, zoomNumber/100 );
          PdfAction zoom = PdfAction.GotoLocalPage(pageNumberToOpenTo, magnify, writer);



          File.WriteAllBytes(filePath, docStream.ToArray());


Now, lets break this down a bit. The first line that may be unfamiliar is the instantiation of the PdfDestination object. This object is used to define the destination of where a particular action will take the user and any settings that will follow.

PdfDestination magnify = new PdfDestination(PdfDestination.XYZ, -1, -1, zoomNumber/100 );

This should always be defined after the Open() method on the PdfDocument otherwise you will get Object reference errors. My favorite Smile. You will also notice that the PdfDestination object will define the zoom level. This level is based on a float of 0-1. In this case 1 is 100%, so dividing your number by 100 will get you the right percentage. 100.0 / 100 = 1. If you were to set the zoomNumber to 300.0, you would get 300% ( 300/100= 3 ) and so on.

The next thing to set is the PdfAction. This object tells ITextSharp that you wish to perform some action prior to the rendering of the plugin. by passing in the PdfWriter (writer) you are linking the action and writer together and ITextSharp will know what to do with it.

int pageNumberToOpenTo = 1;

PdfAction zoom = PdfAction.GotoLocalPage(pageNumberToOpenTo, magnify, writer);

The action we are using here to cheat a little bit is the GoToLocalPage method. This method as it sounds redirects you to a certain page and with that allows you to set the magnification on the page the  user is being sent to. Notice that the page number starts at 1, not 0 for any loops you may wish to place this in.

Now all you have to do is update the document with whatever else you need to put into the document and close the Pdf.


I hope this helps others. it took me a while to figure out how to zoom correctly.


Happy Coding Smile


About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, ASP.NET, C# and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s