I have received several requests asking how to view a PDF after creating it natively in the iPhone. Below is a guide to doing just that. It uses the method I am currently using - a UIWebView. This provides you scrolling, zooming and navigating multiple pages without having to write any extra code. So, on to the code!
[LIST=1]
[*]In your project navigate to or create the directory you want to have the PDF Viewer.
[*]Create a new UIView subclass named 'PDFViewController'.
[*]Also create a new view in your resources directory named 'PDFView'.
[/LIST]
This is what your PDFViewController.h should look like:
@interface PDFViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webView;
NSURL *pdfUrl;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) NSURL *pdfUrl;
@end
Here we have created UIWebView object named webView, (which has an outlet to Interface Builder) and an NSURL object named pdfUrl.
This is what your PDFViewController.m should look like:
#import \"PDFViewController.h\"
@implementation PDFViewController
@synthesize webView, pdfUrl;
#pragma mark -
#pragma mark UIViewController methods
// View Did Load method -- Load the PDF
- (void)viewDidLoad {
[super viewDidLoad];
// Tells the webView to load pdfUrl
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
}
// Dealloc method -- webView, pdfURL
- (void)dealloc {
[webView release];
[pdfUrl release];
[super dealloc];
}
@end
We synthesize our properties, release them in our deallocate method, and in our ViewDidLoad method, tell the webView to load pdfUrl.
Next open PDFView in Interface Builder.
[LIST=1]
[*]Make File's Owner Class Identity 'PDFViewController'.
[*]Add a Web View on top of the View.
[*]Hook up the view outlet on File's Owner to the View, and the webView outlet on File's Owner to the WebView.
[*]Hook up the delegate outlet on Web View to File's Owner.
[*]Save the file and quit Interface Builder.
[/LIST]
Now the only other thing we need to do is Create an instance of PDFViewController, set the pdfUrl property and push the view. I am assuming here that you have some basic knowledge and am not going to get too detailed as to where you put the code that follows. Make sure that in the controller you place this code you have imported 'PDFViewController.h', otherwise it won't work.
// Create an instance of PDFViewController
PDFViewController *controller = [[PDFViewController alloc] initWithNibName:@\"PDFView\" bundle:nil];
// Get the path to our documents directory
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// This should be our documents directory
NSString *saveDirectory = [documentPath objectAtIndex:0];
// Our PDF is named 'Example.pdf'
NSString *saveFileName = @\"Example.pdf\";
// Create the full path using our saveDirectory and saveFileName
NSString *finalPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
// Set the pdfUrl to our finalPath
controller.pdfUrl = [NSURL fileURLWithPath:finalPath];
// Push 'controller'
[self.navigationController pushViewController:controller animated:YES];
// Release 'controller'
[controller release];
So, above we created an instance of PDFViewController, created a string that contains the path to our documents directory and a file named 'Example.pdf' in that directory. We set that string to pdfUrl for our controller, and pushed our controller.
That about does it, viewing PDFs on the phone couldn't be much easier than that! Please let me know if you have any questions or if something presented in this tutorial doesn't click.
Thanks!
Replies
The same approach may be useful to read Microsoft Office Documents i.e. Word, Excel and PPT, if required.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomefirst of all, thank you so much for such an easy-to-understand tutorial!
It's great!
Could you show me how to show a PDF File that I want to deliver with my app? I mean especially what file path I'd have to type, to point a PDF File that must be found by my PDFView later on the Phone...
thank you (and sorry for my bad English)
nevermind
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI'm just getting started at this and what I'm trying to do is to make a simple app where I can click on the icon and a pdf comes up that can be expanded etc. I have created a simple app and then used this tutorial and it's not getting any errors but my pdf isn't showing up. My pdf is a resource file. I have put the code above in a view controller in the loadView section. I have no idea if this is correct - I've tried several other things as well. Can someone tell me where the code above is supposed to go.
Thanks!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIm really new to this and i've tried doing this but it just isnt working for me...
Umm, can someone make a youtube video or something or message me because this really isn't working for me...
Thanks Guys
George.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks!:)
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI've been extremely busy and haven't been able to reply till just now. I'm creating a simple project for this right now and will send to all that have PM'ed me.
Thanks!
Dan
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHello!
Thanks very much for what you've done here!
I'd also love to get that project, if you would be willing to share.
I'm at [email]brian@slopart.com[/email]
It is much appreciated!!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomeanybody? please I tried several times with no success... thanx in advance
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeofType:@"pdf"
Notice the dot is missing.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeCould i also please get you to send me the example project please
[EMAIL="sander15@bigpond.net.au"]sander15@bigpond.net.au[/EMAIL]
Thank you
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks for this very good tutorial!
Please show me the exact position of this code.
I can't make it work correct.
Thanks Martin
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI'm one of those who is trying to load the pdf from the Resources folder.
[email]kksherman@austin.rr.com[/email]
Thanks
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeCould i also please get you to send me the example project please
[email]momolgtm@gmail.com[/email]
Thank you
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHello, could you email it to me please as well, thanks. I've messaged you.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI am also looking for same kind of application. Could you please send me code, if you are willing to share. My mail id is [email]sambasivarao@web1infotech.com[/email]
Thanks
shiva
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks for the tutorial. It was really a detailed on.
It would be great if you can send me the sample code too.
My email id : [email]s_utreja@yahoo.com[/email]
Thanks,
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeMany thanks! (Andrew @ Aderrington.co.uk)
Andrew
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome