2011-02-01 30 views
1

我能夠使用以下代碼生成PDF。有人可以幫助我如何將文本添加到此?提前致謝。來自iPad的PDF格式

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename 
{ 
    // Creates a mutable data object for updating with binary data, like a byte array 
    NSMutableData *pdfData = [NSMutableData data]; 

    // Points the pdf converter to the mutable data object and to the UIView to be converted 
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); 
    UIGraphicsBeginPDFPage(); 

    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 
    [aView drawRect:aView.bounds]; 

    // remove PDF rendering context 
    UIGraphicsEndPDFContext(); 

    // Retrieves the document directories from the iOS device 
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

    NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; 

    // instructs the mutable data object to write its context to a file on disk 
    [pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 
} 

回答

1

我從來沒有這樣做過(但我計劃在明天繼續)。但我認爲正確的地方是Quartz 2D programming guide。如果您需要它離線,它也可以從您的開發者文檔中獲得。

基本上你和你發佈的一樣,但是從調用drawRect開始appart:你在這個上下文中執行你想要的所有文本。

有寫上簡單的字符串上this guide

我希望它能幫助有用的信息。它確實幫助了我!

+0

謝謝。那篇文章非常有幫助。不知道爲什麼我以前沒有看到它。 – intomo 2011-02-01 23:14:20