Jon Lennart Aasenden
Jon Lennart Aasenden

Reputation: 4020

Create PDF via monotouch on iPhone?

Is it possible to generate a PDF through monotouch (iPhone)? I noticed that reading and rendering a PDF has great support in the Apple API - but what about creating a simple PDF "on the fly"?

I want to generate a PDF report and add it as an email attachment from within my app. The only thing i found was a C# package called "itextsharp", but it feels like overkill for my humble needs (I just need to dump a couple of DB tables).

Any hints or examples are welcome

Upvotes: 1

Views: 2083

Answers (3)

TChadwick
TChadwick

Reputation: 878

This class will convert a UIImage to a pdf document and will hopefully help anyone else looking to do this. (downside is that if those UIImages are large there is no compression)

public static Stream CreatePDF (UIImage image)
    {
        NSMutableData data = new NSMutableData();
        UIGraphics.BeginPDFContext(data, new RectangleF(new PointF(0,0), image.Size), null);
        UIGraphics.BeginPDFPage(new RectangleF(new PointF(0,0), image.Size), null);
        image.Draw(new RectangleF(0, 0, image.Size.Width, image.Size.Height));
        UIGraphics.EndPDFContent();

        using (NSData imageData  = data)
        {
            Byte[] myByteArray = new Byte[imageData.Length];
            System.Runtime.InteropServices.Marshal.Copy (imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
            fs = new MemoryStream(myByteArray);
        }
        return fs;

    }

Upvotes: 1

Mike Bluestein
Mike Bluestein

Reputation: 706

There is an example showing how to do this in the rough cuts version of my MonoTouch book here:

http://my.safaribooksonline.com/book/programming/iphone/9780131388291/graphics-and-animation/157

Upvotes: 1

Dimitris Tavlikos
Dimitris Tavlikos

Reputation: 8170

I do not have an example, but support for creating PDF documents is native in iOS and the framework is available in MonoTouch.

Apple documentation

Upvotes: 1

Related Questions