【问题标题】:How to read pdf file from document directory in iPhone?如何从 iPhone 的文档目录中读取 pdf 文件?
【发布时间】:2012-09-24 07:09:57
【问题描述】:

目前我正在使用 iPhone 应用程序,我在资源文件夹(本地 pdf 文件)中有一个 pdf 文件,然后我成功读取了该 pdf 文件(paper.pdf),下面我提到了读取本地 pdf 文件供您参考。

例子:

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);

然后我尝试将pdf文件(来自URL)存储在NSDocument目录中,存储成功。

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.msy.com.au/Parts/PARTS.pdf"]];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
 NSString *documentsDirectory = [paths objectAtIndex:0]; 
 NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"];
 [pdfData writeToFile:filePath atomically:YES];

然后我尝试读取该 pdf 文件(来自 NSDocument 目录),但我不知道,请帮助我。

提前致谢

【问题讨论】:

    标签: iphone ios multithreading nsdocumentdirectory pdf-reader


    【解决方案1】:

    非常简单,请使用以下代码将您的 pdf 从文档目录显示到 Webview

    从文档目录加载 PDF

     - (void)viewDidLoad
     {
    [super viewDidLoad];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
    webView.scalesPageToFit = YES;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"];
    
    NSURL *targetURL = [NSURL fileURLWithPath:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webView loadRequest:request];
    
    [self.view addSubview:webView];
    [webView release];
     }
    

    从应用程序包资源文件夹加载 PDF

     - (void)viewDidLoad
     {
    [super viewDidLoad];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
    webView.scalesPageToFit = YES;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"myPDF11" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webView loadRequest:request];
    
    [self.view addSubview:webView];
    [webView release];
     }
    

    【讨论】:

      【解决方案2】:

      尝试使用这个:

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"];
      
      NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
      pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
      CFRelease(pdfURL);
      

      来自NSURL reference

      NSURL 类与其 Core Foundation 对应的 CFURLRef 是免费的桥接。有关免费桥接的更多信息,请参阅“免费桥接”。

      【讨论】:

      • @sergio 我正在下载带有时间戳的 pdf 文件,如下所示:/Documents/myPDF.pdf1382698338.78128 那么现在我该如何追加路径组件??
      • @zeeshanshaikh:这是另一个问题,其中包含有关您在做什么的更多详细信息...
      • @sergio 请检查我的问题:stackoverflow.com/questions/19588173/…
      • @sergio 请查看我对您已回答的问题的评论,谢谢
      猜你喜欢
      • 2013-11-04
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      相关资源
      最近更新 更多