PDF library iTextSharp working in Compact Framework 2: iTextSharpCF
Recently I needed a tool to combine several pictures into one file. First I thought about creating a memory bitmap and then BitBlit the source files into this single bitmap. Then I found an entry about iText and iTextSharp. This is a JAVA and .NET class library to create pdf files. As it seems very easy to use I decided to take a closer look. I found one message, where Marco states he has done a port for .NET compact framework and provided a patch to the sourceforge iTextSharp team. Unfortunately the iTextSharp team seems to ignore this port. So I started with iTextSharp and the patch to get iTextSharp running on Compact Framework.
The patch was done against version 4.0.1 of iTextSharp but there were still some rejects and some additional work to do. The main problem was the use of GetInstance where always the general WebRequest.Create(url); and then GetResponseStream() was used. A first workaround is to load the bitmap first and then provide this to doc.Add:
Bitmap myBitmap = new Bitmap(sFilename);
img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Jpeg);
else if (sFilename.ToLower().EndsWith("gif"))
img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Gif);
else if (sFilename.ToLower().EndsWith("bmp"))
img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Bmp);
else if (sFilename.ToLower().EndsWith("png"))
img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Png);
throw new NotSupportedException("Unsupported image format");
img = iTextSharp.text.Image.GetInstance(sFilename);
The GetResponseStream() returns a stream or filestream within full framework. But with Comapct Framework you will always get ‘NotSupportedException’ for local files. The CF does not support local files within GetResponseStream. The URI for a local file is ‘file://…’ and an easy replacement for getting a filestream was to use ‘new FileStream(url.LocalPath, FileMode.Open);’. I did insert a #ifdef in all source files with GetResponseStream() as I have no real need to build image instances from network files:
WebRequest w = WebRequest.Create(url);
istr = w.GetResponse().GetResponseStream();
istr = new FileStream(url.LocalPath, FileMode.Open);
There are also some more #if !NETCF inside the source files for everything that differs between full and compact framework.
The attached zip archive has all files you need to compile and use iTextSharp on your windows mobile. The Visual Studio 2005 solution includes a simple demo application that allows you to add some image files and create a PDF with the filenames and images inside. I inserted a new page for every image. The solution is written against the target “Windows Mobile 6 Prof”. So you need Windows Mobile 6 SDK installed.
The left screen shows the demo application and the right one the created pdf inside Adobe Reader Mobile.
The incomplete patch source
A tiny tutorial for iTextSharp usage
The new full source: DOWNLOAD:iTextSharpCF-4.0.1_CF - Sample source code using the modified iTextSharp library (also included)
<!-- Social Bookmarks BEGIN -->
<!-- Social Bookmarks END -->