Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / Java

iTextSharp running on Compact Framework – Windows Mobile

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Jan 2010CPOL2 min read 16.5K   2   6
iTextSharp ported to Compact Framework

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:

#if !TEST
 Bitmap myBitmap = new Bitmap(sFilename);
 if (sFilename.ToLower().EndsWith("jpg"))
 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);
 else
 throw new NotSupportedException("Unsupported image format");
#else
 img = iTextSharp.text.Image.GetInstance(sFilename);
#endif
 doc.Add(img);

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:

//NETCF does not know fileWebRequest (file://...)
#if !NETCF
 WebRequest w = WebRequest.Create(url);
 istr = w.GetResponse().GetResponseStream();
#else
 istr = new FileStream(url.LocalPath, FileMode.Open);
#endif

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.

Screen01 Screen02

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 -->

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError "The document has no pages" Pin
Member 130638456-Apr-17 5:27
Member 130638456-Apr-17 5:27 
QuestionGreat topic and great article Pin
Vladimir Gregor(Beemobile)23-Nov-16 5:16
Vladimir Gregor(Beemobile)23-Nov-16 5:16 
AnswerRe: Great topic and great article Pin
hjgode23-Nov-16 7:57
hjgode23-Nov-16 7:57 
GeneralRe: Great topic and great article Pin
Vladimir Gregor(Beemobile)30-Nov-16 6:26
Vladimir Gregor(Beemobile)30-Nov-16 6:26 
GeneralRe: Great topic and great article Pin
hjgode30-Nov-16 18:58
hjgode30-Nov-16 18:58 
Hi
The issue is that one must parse Postscript/PDF and render to a Bitmap. The only free lib I know for that it Ghostscript.
There are Libs using Ghostscript for .Net, but I fear they do not target Compact Framework.

Possibly you can use PocketXPDF (http://pocketxpdf.sourceforge.net/). As it comes with source one may be able to use that from .Net.

Others are using Foxit for Windows Mobile to show PDF.

There may be others but I limited my g00gle research to 30 Minues for that.

~Josef
GeneralRe: Great topic and great article Pin
Vladimir Gregor(Beemobile)30-Nov-16 23:24
Vladimir Gregor(Beemobile)30-Nov-16 23:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.