如何从应用程序访问保存到iPhone的PDF?

时间:2012-03-01 05:33:54

标签: iphone pdf filesystems

我创建了一个生成PDF的应用程序并将其保存到NSDocumentDirectory。我想让用户很容易找到文件并显示它或使用他们的iPhone打印它。如何找到此文件并轻松查看?

以下是我用来创建PDF的代码:

       //USE PDFGENERATOR TO CREATE A PDF FILE
    PDFViewController* pdf = [[PDFViewController alloc] init];

    NSString *header = lblTitle.text;
    NSLog(@"fullQuote=%@", header);

    NSString *body = [NSString stringWithFormat:@"%@ \n\n - LRH \n %@", lblExcerpt.text, lblDesc2.text];
    NSLog(@"fullQuote=%@", body);

    pageSize = CGSizeMake(612, 792);
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf", lblTitle.text];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

    [pdf generatePdf :pdfFileName:header:body:nil];

1 个答案:

答案 0 :(得分:0)

有一个很好的教程here,它演示了如何在应用程序中打开 .pdf,.xls 文件。

您必须引用的主要类是QLPreviewControllerhere

这是您必须为此

调用的数据源方法
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index 
{
   // Break the path into it's components (filename and extension)
   NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];

  // Use the filename (index 0) and the extension (index 1) to get path

  NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];

 return [NSURL fileURLWithPath:path];
}

也有人想提及这个问题: iPhone - Opening word and excel file without using UIWebview