tags:

views:

117

answers:

1

I've created a PDF on the iPad, but the problem is when you have a line of text greater than 1 line, the content just goes off the page. This is my code:

void CreatePDFFile (CGRect pageRect, const char *filename) {
 CGContextRef pdfContext;
 CFStringRef path;
 CFURLRef url;
 CFMutableDictionaryRef myDictionary = NULL;
 path = CFStringCreateWithCString (NULL, filename,
           kCFStringEncodingUTF8);
 url = CFURLCreateWithFileSystemPath (NULL, path,
           kCFURLPOSIXPathStyle, 0);
 CFRelease (path);
 myDictionary = CFDictionaryCreateMutable(NULL, 0,
            &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks);
 NSString *foos = @"Title";
 const char *text = [foos UTF8String];
 CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR(text));
 CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("Author"));
 pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
 CFRelease(myDictionary);
 CFRelease(url);

 CGContextBeginPage (pdfContext, &pageRect);

 CGContextSelectFont (pdfContext, "Helvetica", 12, kCGEncodingMacRoman);
 CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
 CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
 NSString *body = @"text goes here";
 const char *text = [body UTF8String];
 CGContextShowTextAtPoint (pdfContext, 30, 750, text, strlen(text));

 CGContextEndPage (pdfContext);

 CGContextRelease (pdfContext);}

Now the issue lies within me writing the text to the page, but the problem is that I can't seem to specify a multi-line "text view".

A: 

I think you can use CGContextGetTextPosition to measure text as you draw it. Using that, you can effectively wrap text.

You can use a UILabel or UITextView to draw the text. Use CALayer renderInContext with your pdf context.

drawnonward
So can I use [txtView.layer renderInContext:pdfContext]; to add the text view and i's contents within its boundaries to the text view?
Matthew Roberts
Yes, that should work.
drawnonward