Malcolm Cartney
Malcolm Cartney

Reputation: 65

Local pdf file in webView

Im trying to load local stored pdf file to webView Controler, pdf file is next to project files. But simply nothing appears in webview.

- (void)viewDidLoad
{
    webView.delegate = self;
    NSString *indexPath = [NSBundle pathForResource:@"doc" ofType:@"pdf" inDirectory:nil];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

Upvotes: 0

Views: 639

Answers (3)

kagmanoj
kagmanoj

Reputation: 5368

Yes, this can be done with the UIWebView.

If you are trying to display a PDF file residing on a server somewhere, you can simple load it to your web view directly:

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];

NSURL *targetURL = [NSURL     URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];
[webView release];

Or if you have a PDF file bundled with your application (in this example named "document.pdf"):

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];

NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];
[webView release];

You can find more information here: Technical QA1630: Using UIWebView to display select document types.

Upvotes: 1

Mani
Mani

Reputation: 17585

Use this..

NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

Upvotes: 0

Rajesh Choudhary
Rajesh Choudhary

Reputation: 1340

Try This

    - (void)viewDidLoad
    {
        webView.delegate = self;
    NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"doc" ofType:@"pdf" inDirectory:nil];

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

Upvotes: 0

Related Questions