从iPad写PDF

时间:2011-02-01 22:20:28

标签: objective-c ipad ios4

我可以使用以下代码生成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 个答案:

答案 0 :(得分:1)

我从来没有这样做过(但是,我计划明天再开始)。但我认为正确的去处是Quartz 2D programming guide。如果您需要离线,也可以从开发人员文档中获取它。

基本上你和你发布的一样,但是从调用drawRect开始:你在那个上下文中执行你想要的所有文本绘图。

有关在this guide

上编写简单字符串的有用信息

我希望它有所帮助。它确实帮助了我!