What is the way to read PDF content on an iPad or iPhone?
You can use Quartz to parse a PDF document and extract the metadata.
Parsing a PDF
There is another simple way to read a PDF in iPhone/iPad:
Take one UIwebView (name:pdfView).
Give Iboutlet connection to it & Delegate it to FilesOwner
In Viewdidload/VIewWillApper/VIewDidApper
[self.pdfView loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"ObjC" ofType:#"pdf"]]]];
ObjC.pdf should be in resource folder
Use fastpdfkit: https://github.com/mobfarm/FastPdfKit
Firstly, save a built in pdf file in document Directory of iPhone Simulator (say it demo.pdf). then use following code in ViewController.m file at ViewDidLoad method and dont forget to add UIWebViewDelegate in ViewController.h file
-(void) viewDidLoad
{
[super viewDidLoad];
UIWebView theWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
theWebView.delegate=self;
theWebView.scalesPageToFit=YES;
[self.view addSubview:theWebView];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir=[paths objectAtIndex:0];
NSString *fileName=[documentDir stringByAppendingPathComponent:#"demo.pdf"];
NSURL *url=[NSURL fileURLWithPath:fileName];
[theWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
Related
I am developing one application. In that I'am loading the pdf in uiwebview like below.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filename1 = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:#"KeyToSelectedFile"]];
NSString *fileName=[filename1 stringByAppendingPathComponent:[default1 objectForKey:#"keyToAppearfile"]];
NSLog(#"%#",fileName);
//NSString *bookpath=[filename1 stringByAppendingPathComponent:book];
NSURL *url = [NSURL fileURLWithPath:fileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
web=[[UIWebView alloc]initWithFrame:CGRectMake(20,80, 980, 690)];
web.delegate=self;
[web loadRequest:request];
web.backgroundColor=[UIColor clearColor];
[self.view addSubview:web];
At first time this will crash the application. From second time onwards it will be showing the pdf correctly. It was not shown any error when the app was crashed. I didn't understand what's the problem.
You can also use Documents interaction controller. it is a better approach to view pdf files using that. and its implementation is quite simple too.
here is a reference link to it: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html
I hope it helps you. Cheers!!
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL) fileURL
usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController =
[UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
Just give some time to load the PDF from the web server.. On that loading process u can add a ACTIVITY INDICATOR too...
I have some PDF formate document in subfolder of Resourse folder in my application.i have try to read these document like this way.here is my code.
NSString *path = [[NSBundle mainBundle] pathForResource:chapter ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, 50.0)"]];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
In this code "path" Return Nill value,when i check in console.but when i check the string "chapter" which store my desire pdf document name ,Always it have one of the pdf document name.In Console when my program is crash it show some thing like this.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[UIWebView scrollView]: unrecognized selector sent to instance 0x6587d20'
Any help will be appriated.
Are you check out your pdf file in your project?
I test a following code. no problem.
please check your chapter.pdf file in your project. the point is relative path.
Must be case sensitive. "Chapter" / "chapter" is not equalString.
NSLog(#"path:%#",[[NSBundle mainBundle] pathForResource:#"chapter" ofType:#"pdf"]);
console:
/var/mobile/Applications/72AFA069-D83E-469A-A687-AEAFDB0B4D97/TableViewTest.app/chapter.pdf
NSLog(#"path:%#",[[NSBundle mainBundle] pathForResource:#"Chapter" ofType:#"pdf"]);
console:
(null)
self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.delegate = self;
[self.view addSubview:self.myWebView];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"chapter" ofType:#"pdf"]]]];
In your project folder name will disappear in a actual iPhone bundle.
You have created a folder called Resource-Common, but eventually disappears in NSBundle.
See the image below. Both are integrated. folder name in the project is only just visible folder in xcode.
If you want all pdf file list in NSBundle. try to following code.
NSArray *pdfCollection = [[NSBundle mainBundle] pathsForResourcesOfType:#"pdf" inDirectory:nil];
UIWebView's scrollView is a read only property according to Apple's doc: The scroll view associated with the web view. (read-only). But you treated it as like a UIScrollView that it was not. That was the error message.
It looks like a few people on stackoverflow get this to work but their code isn't posted. I'm using
[web loadData:data MIMEType:MIMEType textEncodingName:#"UTF-8" baseURL:nil];
where MIMEType is:
#"application/vnd.ms-powerpoint"
#"application/vnd.ms-word"
#"application/vnd.ms-excel"
(BTW, I've seen DOC files use mimetype #"application/msword" but the "vnd" version seems more appropriate. I tried both just in case.)
I verified that my 'data' is correct. PDF and TXT files work. When the UIWebView displays PPT, DOC, or XLS files, it's blank. I put NSLOG statements in my UIWebViewDelegate calls.
shouldStartLoadWithRequest:<NSMutableURLRequest about:blank> navType:5
webViewDidStartLoad:
didFailLoadWithError:Error Domain=NSURLErrorDomain Code=100 UserInfo=0x122503a0 "Operation could not be completed. (NSURLErrorDomain error 100.)"
didFailLoadWithError:Error Domain=WebKitErrorDomain Code=102 UserInfo=0x12253840 "Frame load interrupted"
so obviously the load is failing, but why? If I change my mimetype to #"text/plain" for a PPT file, the UIWebView loads fine and displays unprintable characters, as expected. That's telling me the 'data' passed to loadData: is ok.
Meaning my mimetypes are bad?
And just to make sure my PPT, DOC, and XLS files are indeed ok to display, I created a simple html file with anchor tags to the files. When the html file is displayed in Safari on the iPhone, clicking on the files displays correctly in Safari.
I tried to research the error code displayed in didFailLoadWithError (100) but all the documented error codes are negative and greater than 1000 (as seen in NSURLError.h).
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(#"didFailLoadWithError:%#", error); }
Have you tried using the following documented method?:
-(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];
}
// Calling -loadDocument:inView:
[self loadDocument:#"mydocument.rtfd.zip" inView:self.myWebview];
It works for these in iPhone OS 2.2.1:
Excel (.xls)
Keynote (.key.zip)
Numbers (.numbers.zip)
Pages (.pages.zip)
PDF (.pdf)
Powerpoint (.ppt)
Word (.doc)
iPhone OS 3.0 supports these additional document types:
Rich Text Format (.rtf)
Rich Text Format Directory (.rtfd.zip)
Keynote '09 (.key)
Numbers '09 (.numbers)
Pages '09 (.pages)
The only way i found to read an Office object (tested with .doc or .xls) is to save the NSData object in a temp file, then read it.
-(void)openFileUsingExtension:(NSString*)extension {
NSString *path = [NSString stringWithFormat:#"%#temp.%#",NSTemporaryDirectory(),extension];
NSLog(#"%#",path);
if ([objectFromNSData writeToFile:path atomically:YES]) {
NSLog(#"written");
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:request];
self.webview.scalesPageToFit = YES;
self.webview.delegate = self;
[self.view addSubview:self.webview];
}
}
then you can remove the file inside the UIWebViewDelegate method:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *path = [NSString stringWithFormat:#"%#temp.%#",NSTemporaryDirectory(),extension];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0, 1000, 760)];
[webView setScalesPageToFit:YES];
webView.backgroundColor=[UIColor clearColor];
NSString *ppt = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"ppt"];
NSURL *pptURL = [NSURL fileURLWithPath:ppt];
NSURLRequest *request = [NSURLRequest requestWithURL:pptURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Did you try this on the device or in the simulator only?
I find that I cannot get UIWebView to display .doc and .pages documents in the simulator (but can display .txt and .pdf ones), but both file types load and display just fine on both the iPad and iPhone devices.
Bug in simulator?
Gregor,
Sweden
After searching thru, the solution I found was because when we import xls into Xcode by drag and drop the xls file into Project->Supporting Files, we need to specific the 'Add to targets:' , we need to tick the project to add
I am wondering if anyone can advise on the best way to add a FAQ page in iphone app. I would like to display a 'local' file (perhaps HTML) with local images into a web view. Essentialy to enable users access to FAQ within the app.
Any advice on whether to use HTML or any other way of doing this will be very helpful..
Thanks in advance..
mb
I used a UIWebView and loaded a local index.html file into this.
**HelpViewController.h**
#interface HelpViewController : UIViewController {
UIWebView *webView;
}
#property (retain) IBOutlet UIWebView *webView;
#end
Obviously you will need to #sythesize webView.
**HelpViewController.m**
- (void)viewDidLoad
{
self.title = #"Help";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleDone target:self action:#selector(btnReturn)];;
NSString *helpPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:helpPath isDirectory:NO];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(#"Help loaded");
}
Hope this helps. Also, just a note, if you are presenting this as a modal, it will appear blank for a split second until the HTML file loads into the UIWebView. I got around this using:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self performSelector:#selector(showAboutWebView) withObject:nil];
}
- (void)showAboutWebView {
[webView setHidden:NO];
}
I wanted an entire page of text(multiple lines) in my settings similar to the one on iphone:
settings->general settings->About->Legal
I have not been able to do so. I tried entering title in PSGroupSpecifier, it gives me multiple lines but with a gray background. i wanted a white background.
Any help would be highly appreciated.
Thanks in advance
Use a UIWebView and store your page as a HTML file in your resources.
// Create the UIWebView
UIWebView *webView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
// Add the UIWebView to our view
[self.view addSubview:webView];
// Load the HTML document from resources
NSString *pagePath = [[NSBundle mainBundle] pathForResource:#"legal" ofType:#"html"];
NSURL *pageURL = [NSURL fileURLWithPath:pagePath];
NSURLRequest *pageReq = [NSURLRequest requestWithURL:pageURL];
[webView loadRequest:pageReq];