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 D开发者_运维百科B tables).
Any hints or examples are welcome
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
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
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;
}
精彩评论