iTextSharp running on Compact Framework – Windows Mobile

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 not found]

Update 9. oct. 2013: source code and example hosted at GitHub now!

15 Comments

  1. GADEA says:

    thanks from Spain!!! thank you very much!!!

  2. Eugen says:

    Hi,
    I try to get PDFToText (which uses iTextSharp library) to work on Windows Mobile, but if I try to compile my project in Visual Studio 2008, I get following error:

    Error 1 The Type “System.Uri” is defined in not referenced Assembly. Add a link to the Assembly “System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”. (translated from German)

    The original:

    Fehler 1 Der Typ “System.Uri” ist in einer nicht referenzierten Assembly definiert. Fügen Sie einen Verweis auf die Assembly “System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” hinzu.

    Perhabs you could help me with it…

    Best regards,
    Eugen

  3. admin says:

    Possibly this http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesvbcs/thread/3287c1c8-b55e-447f-b300-56e0911d2723 and this http://gmobilesync.codeplex.com/WorkItem/View.aspx?WorkItemId=10989 will help.

    Check and esnure that you are using the compact framework System…dll reference. You can remove the reference from solution explorer and then add it again using the compact framework dir below \Program Files (or “\Programme”).

    regards

    Josef

  4. […] itextSharp CF edition to PDF Library I find this link about the ITextSharp on Mobile: iTextSharp running on Compact Framework – Windows Mobile | Windows CE Programming but I don't know how can I use it with B4P. Sombody Help me […]

  5. John says:

    I managed to get iTextSharpCF 5.1.2 running under Windows CE and Compact Framework 3.5, thanks to your patches on 4.0.1,

  6. DBarzo says:

    Hi John,

    so you have successfully port iTextSharp 5.1.2 to Compact Framework?
    The community will be very grateful if you share your code! 😉

  7. Simon says:

    Exactly what I needed and works right away!
    Saved me a lot of time …

    Thanks very much
    Simon

  8. Jiten Oswal says:

    Does this library work with Windows Mobile 7 (Mango) ??

  9. Rtg23 says:

    Hi guys!

    I’m working with this excellent port but I’m having troubles signing a pdf with a digital signature certificate… anyone has done it before?

    I also ask if someone could share his iTextSharp for CE in newer versions… Like John says it have 5.1.2 in CE 3.5, would be great.

    Thanks in advance, great work!

  10. John says:

    I apologize, I don’t get notifications if comments are made on this thread. I am happy to share the patch file with those requiring it…
    email me or submit the request here
    http://www.firstdegreesoftware.com/Contact.aspx

  11. josef says:

    Hello John

    I do not know what to do on the web site you mention.

    Sorry

    ~Josef

  12. Hello guys!

    Do you know if this library can be used to display PDF files on .NET Compact Framework?

    By the way I was able to build the lib for .NET CF 3.9 using VS 2013 and deployed to Windows Embedded Compact 2013.

    Thank you!

  13. josef says:

    Hello Vladimir

    iTextSharp is creating and changing PDF but not to display. There are other tools like Adobe Reader or FoxIt reader for embedded devices.

    ~josef

  14. Gabriela says:

    Hello!

    I ask if someone could share his iTextSharp for CE…Please

    Thanks in advance

Leave a Reply