How to read pdf file from document directory in iPhone?

后端 未结 2 1913
余生分开走
余生分开走 2021-02-06 17:00

Currently i am working in iPhone application, i have an pdf file in resource folder (Local pdf file) then i read that pdf file (paper.pdf) successfully, below i have mentioned r

2条回答
  •  醉酒成梦
    2021-02-06 17:40

    its very simple please use below code to display your pdf from document directory to Webview

    load PDF from Document Directory

     - (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];
     }
    

    load PDF from Application Bundle Resource Folder

     - (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];
     }
    

提交回复
热议问题