Click here to Skip to main content
12,898,335 members (54,721 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 4 Jan 2010

iTextSharp running on Compact Framework – Windows Mobile

, 4 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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);
 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:

//NETCF does not know fileWebRequest (file://...)
#if !NETCF
 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.

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


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


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionError "The document has no pages" Pin
Member 130638456-Apr-17 5:27
memberMember 130638456-Apr-17 5:27 
QuestionGreat topic and great article Pin
Vladimir Gregor(Beemobile)23-Nov-16 5:16
memberVladimir Gregor(Beemobile)23-Nov-16 5:16 
AnswerRe: Great topic and great article Pin
hjgode23-Nov-16 7:57
memberhjgode23-Nov-16 7:57 
GeneralRe: Great topic and great article Pin
Vladimir Gregor(Beemobile)30-Nov-16 6:26
memberVladimir Gregor(Beemobile)30-Nov-16 6:26 
GeneralRe: Great topic and great article Pin
hjgode30-Nov-16 18:58
memberhjgode30-Nov-16 18:58 
GeneralRe: Great topic and great article Pin
Vladimir Gregor(Beemobile)30-Nov-16 23:24
memberVladimir 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 4 Jan 2010
Article Copyright 2010 by hjgode
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid