如何从iPhone中的文档目录中读取pdf文件?

目前我在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目录),但我不知道,请帮助我。

提前致谢

尝试使用这个:

 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参考 :

NSURL类免费与其核心基础对象CFURLRef桥接。 有关免费桥接的更多信息,请参阅“免费桥接”。

它非常简单,请使用下面的代码来显示您的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]; }