 |
|
 |
|
 |
04-22-2009, 11:35 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Posts: 48
|
PDF Viewer Tutorial
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!
- 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'.
This is what your PDFViewController.h should look like:
Code:
@interface PDFViewController : UIViewController {
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:
Code:
#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.
- 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.
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.
Code:
// 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!
|
|
|
04-24-2009, 01:58 AM
|
#2 (permalink)
|
|
Apple Evangelist
Join Date: Aug 2008
Location: Mumbai
Posts: 191
|
Good Tutorial !!!!
Thanks Daniel for your great work.
The same approach may be useful to read Microsoft Office Documents i.e. Word, Excel and PPT, if required.
__________________
Thanks & Regards,
SushiL
|
|
|
05-04-2009, 04:05 PM
|
#3 (permalink)
|
|
New Member
Join Date: May 2009
Posts: 1
|
Quote:
Originally Posted by danielb21
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!
- 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'.
This is what your PDFViewController.h should look like:
Code:
@interface PDFViewController : UIViewController {
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:
Code:
#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.
- 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.
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.
Code:
// 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!
|
thanks daniel, I'm new to development in iPhone apps, can you assist me with what mean from step 5 downwards, you lost me there. thanks Pierre
|
|
|
05-14-2009, 03:39 PM
|
#4 (permalink)
|
|
New Member
Join Date: May 2009
Posts: 14
|
Hi
first 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
|
|
|
05-15-2009, 12:39 AM
|
#5 (permalink)
|
|
Apple Evangelist
Join Date: Aug 2008
Location: Mumbai
Posts: 191
|
If you are adding a pdf file as a resource file, then following code can be used.
Code:
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"pdfFileName" ofType:@".pdf"];
NSURL *pdfURL = [NSURL URLWithString:pdfPath];
__________________
Thanks & Regards,
SushiL
|
|
|
08-03-2009, 04:46 PM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 2
|
Quote:
Originally Posted by sukumar_77
If you are adding a pdf file as a resource file, then following code can be used.
Code:
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"pdfFileName" ofType:@".pdf"];
NSURL *pdfURL = [NSURL URLWithString:pdfPath];
|
I'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!
|
|
|
08-04-2009, 03:07 AM
|
#7 (permalink)
|
|
Apple Evangelist
Join Date: Aug 2008
Location: Mumbai
Posts: 191
|
Quote:
Originally Posted by shermankimb
I'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!
|
Are you testing onj simulator? If YES, then please test it on device as you will not get any output on simulator.
__________________
Thanks & Regards,
SushiL
|
|
|
08-19-2009, 12:17 PM
|
#8 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 3
|
Can you post the project please?
danielb21 - This is a great article - exactly what I have been looking for. Unfortunately, I can't get the code to work after several attempts. Can you post the project for download or email it to me at dgiii@cox.net. Thanks!
Quote:
Originally Posted by danielb21
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!
- 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'.
This is what your PDFViewController.h should look like:
Code:
@interface PDFViewController : UIViewController {
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:
Code:
#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.
- 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.
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.
Code:
// 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!
|
|
|
|
08-26-2009, 12:10 PM
|
#9 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 1
|
Confused
Hi Guys,
Im 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.
|
|
|
08-26-2009, 03:07 PM
|
#10 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 3
|
Me Too
I have had trouble getting this to work, and I am an experienced programmer. has the author actualy tried this code? Maybe the author would be kind enough to post the working project? Or email to me at dgiii@cox.net.
Thanks!
|
|
|
08-27-2009, 09:09 AM
|
#11 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Posts: 48
|
Hey everyone,
I'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
|
|
|
09-06-2009, 05:52 PM
|
#12 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Posts: 5
|
project me pretty please!
Quote:
Originally Posted by danielb21
Hey everyone,
I'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
|
Hello!
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 brian@slopart.com
It is much appreciated!!
|
|
|
09-09-2009, 06:18 PM
|
#13 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 30
|
Where shall I paste this code? in the AppDelegate?
anybody? please I tried several times with no success... thanx in advance
Quote:
Originally Posted by danielb21
Code:
// 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];
|
|
|
|
09-21-2009, 06:46 PM
|
#14 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 9
|
Quote:
Originally Posted by sukumar_77
If you are adding a pdf file as a resource file, then following code can be used.
Code:
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"pdfFileName" ofType:@".pdf"];
NSURL *pdfURL = [NSURL URLWithString:pdfPath];
|
This works well but you have to remove the dot (".") from the file type or it doesn't work. so the "ofType" parameter should be as follows:
ofType:@"pdf"
Notice the dot is missing.
|
|
|
09-24-2009, 09:00 PM
|
#15 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 44
|
I am afraid i am another that is having trouble with this, i got the PDF create tutorial working, just this one i am doing something wrong.
Could i also please get you to send me the example project please
sander15@bigpond.net.au
Thank you
|
|
|
09-26-2009, 01:44 PM
|
#16 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 1
|
Quote:
Originally Posted by sukumar_77
If you are adding a pdf file as a resource file, then following code can be used.
Code:
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"pdfFileName" ofType:@".pdf"];
NSURL *pdfURL = [NSURL URLWithString:pdfPath];
|
Hallo.
Thanks for this very good tutorial!
Please show me the exact position of this code.
I can't make it work correct.
Thanks Martin
|
|
|
10-06-2009, 03:20 PM
|
#17 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 2
|
Example Project
I'm now getting back to working on this and also still having trouble. I would love to see the example project!
I'm one of those who is trying to load the pdf from the Resources folder.
kksherman@austin.rr.com
Thanks
|
|
|
10-19-2009, 05:12 AM
|
#18 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 171
|
Quote:
Originally Posted by danielb21
Hey everyone,
I'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
|
Please send the project to me too.
|
|
|
10-23-2009, 06:14 AM
|
#19 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 171
|
Many thanks for providing such a simple way
I checked this thing and its working perfectly in the simulator as well as device. I just added a PDF as a resource and checked it. And its fine.
|
|
|
10-23-2009, 11:10 AM
|
#20 (permalink)
|
|
Registered Member
Join Date: Oct 2009
Posts: 3
|
PDF Viewer Tutorial
Quote:
Originally Posted by danielb21
Hey everyone,
I'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
|
Could i also please get you to send me the example project please
momolgtm@gmail.com
Thank you
|
|
|
10-25-2009, 06:02 PM
|
#21 (permalink)
|
|
Registered Member
Join Date: Oct 2009
Posts: 2
|
Quote:
Originally Posted by danielb21
Hey everyone,
I'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
|
Hello, could you email it to me please as well, thanks. I've messaged you.
Last edited by hobnob; 10-25-2009 at 06:06 PM.
|
|
|
10-28-2009, 05:24 PM
|
#22 (permalink)
|
|
Registered Member
Join Date: Oct 2009
Posts: 3
|
Could you email the example to me as well? sjachec1@gmail.com
|
|
|
10-30-2009, 02:34 PM
|
#23 (permalink)
|
|
shiva
Join Date: Jun 2009
Location: Hyderabad
Age: 23
Posts: 54
|
Hi,
I am also looking for same kind of application. Could you please send me code, if you are willing to share. My mail id is sambasivarao@web1infotech.com
Thanks
shiva
|
|
|
11-13-2009, 07:21 AM
|
#24 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 5
|
Nice tutorial
Hi,
Thanks 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 : s_utreja@yahoo.com
Thanks,
|
|
|
12-10-2009, 06:58 PM
|
#25 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Posts: 28
|
Hi, Any chance someone could forward the example project file on to me aswell??
Many thanks! (Andrew @ Aderrington.co.uk)
Andrew
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is On
|
|
|
|
» Advertisements |
» Online Users: 383 |
| 31 members and 352 guests |
| akayler, alex@xomo, Argus, Batman, BlowFishSurvivor, Bomiz96, bravetarget, comi, coulls, Eagle11, elite1967, ffjones54, GinsbergWright, helen412, iSkythe, JamesT0723, joshsroka, Kryckter, lepetitapps, level4, markbuchanan, Mike J, mistashizzle, moonshiner, Mr Jack, NeoNate, rhuettl, riq, Roman, slaveofstyle, Smash22 |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 23,627
Threads: 38,383
Posts: 168,657
Top Poster: smasher (2,540)
|
| Welcome to our newest member, comi |
|