Surjit Joshi
Surjit Joshi

Reputation: 3515

How to read .docx file & show its contents in UITextView?

I have tried with this:

  BOOL success; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSError *error; 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                     NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0]; 


  NSString *writableDBPath = [documentsDirectory 
                              stringByAppendingPathComponent:@"FT1.docx"]; 
  success = [fileManager fileExistsAtPath:writableDBPath];
  NSLog(@"writableDBPath : %@", writableDBPath);

  NSString *str = [NSString stringWithContentsOfFile:writableDBPath 
                            encoding:NSUTF8StringEncoding error:&error];
  NSLog(@"My Data From File : %@",str);

But str prints (null). The file is present in Document Directory.

So, how should I do it?

Upvotes: 0

Views: 1855

Answers (2)

npereira
npereira

Reputation: 41

You can use this free library to render the docx document. They also offer and SDK for native editing, but it's paid. Have a look: http://www.mswordsdk.com

-(void)presentWordViewController:(NSString *)path {
if (nil==self.wordViewController) {
    self.wordViewController=[[WordViewController alloc] initWithNibName:nil bundle:nil];
    UIBarButtonItem *fixedSpace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpace.width=80;
    self.wordViewController.titleBar.items=[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.wordViewController action:@selector(dismissWordViewController)] ,
                                            fixedSpace,
                                            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                            self.wordViewController.pageInfoBarButtonItem,
                                            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                            [[UIBarButtonItem alloc] initWithTitle:@"PDF" style:UIBarButtonItemStyleBordered target:self action:@selector(pdfQuickLook:)],
                                            nil];
}

if (nil==self.modalNavigationConroller) {
    self.modalNavigationConroller=[[UINavigationController alloc]initWithRootViewController:self.wordViewController];
    [self presentViewController:self.modalNavigationConroller animated:YES completion:nil];
} else {
    if (nil==self.modalNavigationConroller.presentingViewController) {
        [self presentViewController:self.wordViewController.navigationController animated:YES completion:nil];
    }
}
[self.wordViewController loadDocument:[WordViewControllerDocument documentWithURL:[NSURL fileURLWithPath:path] andTitle:nil]];

}

Upvotes: 4

Dhruv
Dhruv

Reputation: 2163

Try this :

- (void)viewDidLoad
{
 webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [webView setDelegate:self];
    NSURL *url = [NSURL fileURLWithPath:yourFilePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *document = [theWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText"];
    NSLog(@"%@",document);
}

Upvotes: 0

Related Questions