0

I have to read pdf file from my controller. How to load it ?

Thanks

Ajay_Kumar
  • 1,381
  • 10
  • 34
  • 62

2 Answers2

1

You could use UIWebView to view the pdf file.

Check the below SO post.

Display pdf from the weburl iphone sdk

Tutorial link1

From apple

Using UIWebView to display select document types

Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • @Jhaliya.How to give scroll view for scrolling pdf files. Thanks great update. – Ajay_Kumar Jun 06 '11 at 11:03
  • @Ajay_Kumar : `UIWebView` will take care of that because it's inherited from `UIScrollView`. – Jhaliya - Praveen Sharma Jun 06 '11 at 11:04
  • I wrote code for fetching pdf file. I added that pdf in my resource directory of project. NSURL *targetURL = [NSURL URLWithString:@"Chapter01Page01.pdf"]; – Ajay_Kumar Jun 06 '11 at 11:11
  • Is it write ? I am not seeing pdf file is appearing on screen. – Ajay_Kumar Jun 06 '11 at 11:11
  • Use a UIDocumentInteractionController instead for the default pinch zoom and scrolling behaviour that iOS uses by default for documents etc. – Luke Jun 06 '11 at 11:12
  • @Ajay_Kumar : Have you added **webview** object as **subview** in your main view ? – Jhaliya - Praveen Sharma Jun 06 '11 at 11:18
  • @Jhaliya. I have written [self.view addSubview:webView]; [webView release]; And i also added one webview in view from builder. And also why this is not reading my local file which is kept in resiurce directory. – Ajay_Kumar Jun 06 '11 at 11:24
  • This is loading pdf file, if i am giving URL pat as per above link. But scroll is not vertical scroll is not coming. I am seeing horizantal scroll. And also kindly know me how to read local file, which is kept in my project->resource->directory. – Ajay_Kumar Jun 06 '11 at 11:27
  • @Ajay_Kumar : i think you have'nt given the correct file path. – Jhaliya - Praveen Sharma Jun 06 '11 at 11:31
0

Here's how you can read and store your pdf locally:

First create UIWebView and include <<UIWebViewDelegate>>

  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  NSString *documentsPath = [paths objectAtIndex:0];
  NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 
 if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
           // download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
         NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]];
            //Store the downloaded file  in documents directory as a NSData format
         [pdfData writeToFile:filePath atomically:YES];
    }

     NSURL *url = [NSURL fileURLWithPath:filePath];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [yourWebView setUserInteractionEnabled:YES];
     [yourWebView setDelegate:self];
     yourWebView.scalesPageToFit = YES;
     [yourWebView loadRequest:requestObj];

Or, if you simply want to read/load pdf from your Resource folder then simply do this :

     NSString*  filePath= [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"]];
    /// or you can even read docs file as : pathForResource:@"sample" ofType:@"docx"]
     NSURL *url = [NSURL fileURLWithPath:filePath];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [yourWebView setUserInteractionEnabled:YES];
     [yourWebView setDelegate:self];
     yourWebView.scalesPageToFit = YES;
     [yourWebView loadRequest:requestObj];
Samir Jwarchan
  • 1,027
  • 11
  • 14