Friday, August 10, 2012

Reading and Downloading PDF in iOS App

In this blog, i will show you how to read PDF/docs file in your app and the way of downloading and saving PDF from its downloading links to apps documents directory. In the similar way , we can read docs file too.

First create UIWebView in your pdf display view, because we are going to use UIWebView to  display the pdf or docs file.
 Include <UIWebViewDelegate>
eg: IBOutLet UIWebView *yourWebView;

Section 1: Only Reading PDF/Docs file

/**************************************************************/

Note : Follow this section if you are reading your pre-stored files. If you are reading the pre-stored files like those in Resource folder , you don't need to go the next section.That means , in this section we assume that your pdf/docs file is already present in your project resource.
In your ViewDidLoad :
         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];
/**************************************************************/


Section 2: Downloading, Saving & Reading PDF/Docs file

 /**************************************************************/
Note : Follow this section , if you need to download your file from web, and save it to your app and finally want to read it.
1. Reading the local path of PDF file
      NSArray *paths =       NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
      NSString *documentsPath = [paths objectAtIndex:0];
      NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 

2. Checking if pdf/docs file is already present in the app's documents directory , if not then lets download it.
       // this step 2 is not needed if you are not going to download any file
-->
      if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
               // download file , here "http://pdfdownloadingpage?s.pdf" = pdf downloading link
             NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL  URLWithString:@"http://pdfdownloadingpage?s.pdf"]];
                //Store the downloaded file  in documents directory as a NSData format
             [pdfData writeToFile:filePath atomically:YES];
        }

3.  Now lets create URL request for the file stored and read it on UIWebView
  In your ViewDidLoad :
-->
        NSURL *url = [NSURL fileURLWithPath:filePath];
         NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
         [yourWebView setUserInteractionEnabled:YES];
         [yourWebView setDelegate:self];
         yourWebView.scalesPageToFit = YES;
         [yourWebView loadRequest:requestObj];
/**************************************************************/

2 comments:

  1. This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating these kinds of informative things...
    PSD to Wordpress
    wordpress website development

    ReplyDelete