Can’t Display A Microsoft Word.doc document on UIView Controller - iphone

I’m using the code below to display pdfs or filename.doc Microsoft Word documents. It works fine when I use a ofType:#:”pdf”, but my code crashes when I use ofType:#:”doc” or ofType:#:”docx”. The Word document is mostly text and a few url links. Any help appreciated.
NSString *path = [[NSBundle mainBundle] pathForResource:#"C9" ofType:#"doc"];
pdfUrl = [NSURL fileURLWithPath:path];}
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];

As per your code, it looks like you're already using a UIWebView.
Here is some code I've used in the past.
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
In viewDidLoad (change webView frame to whatever you need)..
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:webView];
[self loadDocument:#"file.doc" inView:webView];

Take a look at using QLPreviewController to display these document types.
For the crash, are you sure the file exists at the specified location? Check it's actually being copied to the bundle and that there is a file at that path / URL before you use it.

Related

Getting application crash during opening a local pdf file on UIWebView

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType: #"pdf"];
NSLog(#"%#", path);
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_webView loadRequest:request];
}
just use following code for open PDF in web view
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
[self.webVctr loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
And check it out the PDF is exist or not?
This thinks may helping to you
Happy codeing..
Try this:
in the
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
It was some other issue not related to the pdf loading over web view. I cross checked and found the actual cause of crash. Anyways thanks guys for the help

uiwebview is not showing PDF properly as showing in safari

I am having a strange issue. UIwebview is not showing PDF file properly as it shows when I load the PDF file from web URL or in safari browser.
http://www.winsolz.com/temp/2.jpg
I am using the following code to open the PDF from local system.
NSString *pathFOrPDF = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"Page%#", strPDFNumber] ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:pathFOrPDF];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[myWebview loadRequest:request];
[myWebviewLandscape loadRequest:request];
Thanks.
I don't think it would fix your case but I had a pdf showing in safari and not showing in a UIWebView and my problem was that the file extension was not correct. I appended a .pdf at the end of the local file url and started working.
I've noticed that safari check also content types so maybe it is working on safari as you provide a remote url?
this might help.
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView]

Opening a PDF in App Bundle in Safari ? iPhone

I have a PDF document in my app which I want to allow users to view and email if required.
I would like to open the document in Safari which then should give me access to the forward action in the Safari PDF reader.
How can I open Safari and display the PDF ?
Thank you.
EDIT
this just came to my mind that you won't be able to see the pdf as it is in the sandbox of your app and safari is a different app , so if you want to show it you can use a WebView instead safari... you can use this code instead...
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];

iphone : how can we store pdf file in local and read it?

i want to store pdf file in local and read it . i want to read this pdf same as iphone read data in mkmapview.
means i want to create same as pdf(Acrobat) reader.how can i do this?
you can store the pdf in the application bundle and display it using a uiwebview:
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];
to load it from an url you can do something like this:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://www.mywebsite.com/myPdfFile.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
you can also download your pdf into the documentDirectory during runtime and load it from there.
i think you need this
http://github.com/akisute/iPhonePDF
from here you can find code (api) by that you can genrate pdf in iphone .
and also can do edit it.
Let me know this is what you need ?
You can open the PDF with iBooks or any other app that can read PDF files to store it locally on the Phone.

Creating a PDF programmatically on the iPhone?

Hey all, was wondering if anyone could point me to the right direction. I'm curious about how I would go about creating a PDF programmatically on the iPhone. The documentation mentions CGPDFDocumentCreateWithProvider but i'm not sure how to go about using it. Thanks!
Likewise I do not believe it is possible to view a PDF on the iPhone without an external service to render the output
This is incorrect. You can render a PDF document within a UIWebView instance, for example:
NSString *urlPathStr = [[NSBundle mainBundle] pathForResource:#"myPDFFile" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlPathStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:urlRequest];
You can also render a PDF within a UIView object for better performance.
The PDFKit library is not yet available for the iPhone.
You might look at the QuartzDemo sample application for some Quartz2D methods for rendering and manipulating a PDF document.
CGPDFDocumentCreateWithProvider or CGPDFDocumentCreateWithURL are the methods you have to use.
i tried this methode CGPDFDocumentCreateWithURL.it will create a pdf in the url that you have provided to the methode as first parameter.
Once created ,,load the url in the web view to view the pdf.
you can enter content in pdf using cgcontextRef class.
Here's how you can read and store your pdf locally in iphone application:
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];
Or, if you want to create your own pdf from bunch of text and strings,here's how i did it:
http://iosameer.blogspot.com/2012/09/creating-pdf-file-from-simple.html