[英]How to read pdf file from document directory in iPhone?
目前我在iPhone應用程序中工作,我在資源文件夾(本地pdf文件)中有一個pdf文件,然后我成功讀取了pdf文件(paper.pdf),下面我提到了閱讀本地pdf文件供您參考。
例:
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
然后我試圖在NSDocument目錄中存儲pdf文件(來自URL),成功存儲。
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目錄),但我不知道,請幫助我。
提前致謝
非常簡單請使用下面的代碼來顯示從文檔目錄到Webview的pdf
從文檔目錄加載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];
}
從Application Bundle資源文件夾加載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];
}
試着用這個:
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類與其Core Foundation對應物CFURLRef進行免費橋接。 有關免費橋接的更多信息,請參閱“免費橋接”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.