. Introduction This article is about using iText to generate PDF files using a combination of images and dynamic text written on select locations on the image. The example project uses an ASP.NET MVC3 application to demonstrate the code but it should not be difficult to adapt it to your own needs. Background Me and some friends have been running a hobby web-site called malleus.dk dedicated to two role-playing games called Warhammer Fantasy and Dark Heresy.
One of the applications on our site is a 'character generator' where users can build, configure, and maintain their role-playing characters. We wanted to expand this tool with a print functionality allowing our users to have their characters printed to a nice looking character sheet. We decided that we really needed to generate a PDF document. PDF documents are supported in all browsers and seems to be the de-facto standard for documents on the web. There are a lot of PDF components available on the net, but we quickly settled on iText as it is a very mature product. IText comes with a free option under the GNU Affero Public License, or with a commercial option which gives you more freedom.
I encourage you to check out the details at. Using the example solution The example solution contains two projects - a Class Library and an ASP.NET MVC application. IText is used from the Class Library, and the web application is used for testing the Class Library. The example creates a 'diploma' for a bicycle race.
You can enter name, date, the name of the race, and the distance. The web application will produce a diploma with the entered text on top.
Feb 21, 2013 This video briefly shows how to create a PDF file using a class that can be downloaded from the links below. View the code at: http://krob636.com/Programming.
The basic flow of the example application is illustrated with images in figure 1, 2, and 3. Figure 5 - Diploma with rulers Having these rulers makes it much easier to find the positions of text and images. You can also play around with the code-page being used by iText when generating text. This is done by changing the value of the encoding parameter of the BaseFont.CreateFont method. The example uses the value 'Cp1252' which is for Western Europe. Reading the source code for iText, I can see that it also supports the values 'Cp1250' (Central- and Eastern Europe) and 'Cp1257' (the three Baltic states) but I have yet to play around with these.
Conclusion iText is a mature piece of software with tons of possibilities. I have only scratched the surface of what is possible. A quick scan of the API reveals that you can do PDF forms, drawing, encryption, etc.
I once tried to read the PDF specification and found that it is one complex beast. I very much prefer delegating the work to an API such as iText. History. 1 November 2011 - First edition. Ksyinc2451 20-Feb-14 9:35 20-Feb-14 9:35 I copied some of this code. I loaded a PDF as my background just like this example.
Only difference is that my application is not MVC. I have to say it worked amazingly well. The quality was excellent when I ran it and tested it using visual studeo. But when I copied the application to our production server the quality is not as good.
Its not horrible. But the quality of the background image is 20% less. Specially when you zoom you can tell the background image is a little fuzzy and not as sharp as when I run it on my own computer. Does anyone know what might cause this? Difference between my computer and production: My computer is better and is running the latest version of windows.
My computer is actually a windows server 2008. Production is running windows server 2003. I have latest version of Java. I don't even see Java in production site.This should matter since I am using the.net version. I am wonder (without changing the operating system) is there anything I can update in production server that would bring the quality to the same level as my own computer.
Member 4017381 5-Feb-13 23:05 5-Feb-13 23:05 Hi, thanks for the lovely article, We modified your code a bit to generate PDF forms with images. Now we would like to alter the existing PDF forms by changing the font of the textboxes we have introduced in the PDF. Unfortunately any custom font added is not accepted by PDF and only default fonts are getting set. Examples on web suggested code like: BaseFont customfont = BaseFont.CreateFont(@'D: ACMESecretAgent acmesa.TTF', BaseFont.IDENTITYH, BaseFont.EMBEDDED); customfont.Subset = false; iTextSharp.text.Font font = new iTextSharp.text.Font(customfont, 13); pdfFormFields.SetFieldProperty('29190', 'typeface', font.BaseFont, null); But the above code does not seem to work and nither does it embed the new font in PDF. Have u had any success in implimenting such feature?
Hi - I have done something like this (lots of code left out): Document document = new Document; PdfWriter writer = PdfWriter.GetInstance(document, output); document.Open; PdfTemplate sheet1 = writer.GetImportedPage(readerFront, 1). PdfTemplate sheet2 = writer.GetImportedPage(readerBack, 1). // Page 1 document.NewPage; PdfContentByte pcb = writer.DirectContentUnder; pcb.AddTemplate(sheet1, 0, 0). // Page 2 document.NewPage; pcb = writer.DirectContentUnder; pcb.AddTemplate(sheet2, 0, 0). Writer.Flush; document.Close; The sheet1 and sheet2 variables load PDF fragments for the pages, but thats irrelevant. The important part is you reassign the pcb variable after document.NewPage; Hope it helps.
Thanks Thomas. I have been able to create the iTextSharp Wrapper. Just having a bit of trouble with some of my form fields. Some are populated fine, and then others fail. I don't know why yet, as iTextSharp returns a bool true/false for setField, but when it returns false, I don't get any hint as to what was wrong with the field name. I know the first thought is that the field name is in error, but I have gone back and checked by creating an FDF file, and using the same field name, everything is fine.
Hi Ken - its not such a big step to make something similar work in a web forms application. Do the following: 1) Add a reference to itextsharp.dll and the Article.Pdf.dll in your WebForm app.
2) Create a new.aspx page - call it Print.aspx 3) In the.aspx file add the following to the page declaration: ContentType='application/pdf'. So it looks something like: <%@ Page Language='C#' ContentType='application/pdf'. 4) In the PageLoad event you create an instance of the DiplomaPrinter class with some default values for name, race, etc. 5) Pass in the Http response stream like this: printer.Create(Response.OutputStream); I think that should do the work. Kind regards Thomas.