2010-04-25 42 views
1

我在iPad上創建了一個PDF文件,但問題在於當文本行大於1行時,內容剛剛離開頁面。這是我的代碼:Cocoa創建的PDF不能正確渲染

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);} 

現在的問題在於在我寫作文的網頁,但問題是,我似乎不能指定多行「文本視圖」。

回答

0

我想你可以使用CGContextGetTextPosition來測量你繪製的文本。使用它,你可以有效地包裝文字。

您可以使用UILabel或UITextView來繪製文本。將CALayer renderInContext與您的pdf上下文一起使用。

+0

所以我可以使用[txtView.layer renderInContext:pdfContext];將文本視圖和我的內容添加到文本視圖的邊界內? – 2010-04-25 10:51:35

+0

是的,這應該工作。 – drawnonward 2010-04-25 18:41:24