Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

Graves Robber
($1.99)

African Adventure
($0.99)

iTazer
($0.99)

ArtStudio
($3.99)

Pigs Vs Wolves
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 04-22-2009, 11:35 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default 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!
  1. In your project navigate to or create the directory you want to have the PDF Viewer.
  2. Create a new UIView subclass named 'PDFViewController'.
  3. 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.
  1. Make File's Owner Class Identity 'PDFViewController'.
  2. Add a Web View on top of the View.
  3. Hook up the view outlet on File's Owner to the View, and the webView outlet on File's Owner to the WebView.
  4. Hook up the delegate outlet on Web View to File's Owner.
  5. 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!
danielb21 is offline   Reply With Quote
Old 04-24-2009, 01:58 AM   #2 (permalink)
Apple Evangelist
 
sukumar_77's Avatar
 
Join Date: Aug 2008
Location: Mumbai
Posts: 191
Default 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
sukumar_77 is offline   Reply With Quote
Old 05-04-2009, 04:05 PM   #3 (permalink)
New Member
 
Join Date: May 2009
Posts: 1
Default

Quote:
Originally Posted by danielb21 View Post
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!
  1. In your project navigate to or create the directory you want to have the PDF Viewer.
  2. Create a new UIView subclass named 'PDFViewController'.
  3. 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.
  1. Make File's Owner Class Identity 'PDFViewController'.
  2. Add a Web View on top of the View.
  3. Hook up the view outlet on File's Owner to the View, and the webView outlet on File's Owner to the WebView.
  4. Hook up the delegate outlet on Web View to File's Owner.
  5. 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
pierresinne is offline   Reply With Quote
Old 05-14-2009, 03:39 PM   #4 (permalink)
New Member
 
Join Date: May 2009
Posts: 14
Default

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
nevermind is offline   Reply With Quote
Old 05-15-2009, 12:39 AM   #5 (permalink)
Apple Evangelist
 
sukumar_77's Avatar
 
Join Date: Aug 2008
Location: Mumbai
Posts: 191
Default

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
sukumar_77 is offline   Reply With Quote
Old 08-03-2009, 04:46 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 2
Default

Quote:
Originally Posted by sukumar_77 View Post
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!
shermankimb is offline   Reply With Quote
Old 08-04-2009, 03:07 AM   #7 (permalink)
Apple Evangelist
 
sukumar_77's Avatar
 
Join Date: Aug 2008
Location: Mumbai
Posts: 191
Default

Quote:
Originally Posted by shermankimb View Post
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
sukumar_77 is offline   Reply With Quote
Old 08-19-2009, 12:17 PM   #8 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 3
Post 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 View Post
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!
  1. In your project navigate to or create the directory you want to have the PDF Viewer.
  2. Create a new UIView subclass named 'PDFViewController'.
  3. 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.
  1. Make File's Owner Class Identity 'PDFViewController'.
  2. Add a Web View on top of the View.
  3. Hook up the view outlet on File's Owner to the View, and the webView outlet on File's Owner to the WebView.
  4. Hook up the delegate outlet on Web View to File's Owner.
  5. 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!
DGoulian is offline   Reply With Quote
Old 08-26-2009, 12:10 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 1
Default 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.
GeorgeMGM is offline   Reply With Quote
Old 08-26-2009, 03:07 PM   #10 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 3
Default 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!
DGoulian is offline   Reply With Quote
Old 08-27-2009, 09:09 AM   #11 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default

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
danielb21 is offline   Reply With Quote
Old 09-06-2009, 05:52 PM   #12 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 5
Default project me pretty please!

Quote:
Originally Posted by danielb21 View Post
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!!
Brianu is offline   Reply With Quote
Old 09-09-2009, 06:18 PM   #13 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 30
Default

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 View Post

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];
alexandergre is offline   Reply With Quote
Old 09-21-2009, 06:46 PM   #14 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 9
Default

Quote:
Originally Posted by sukumar_77 View Post
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.
rmonson is offline   Reply With Quote
Old 09-24-2009, 09:00 PM   #15 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 44
Default

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
Coaster is offline   Reply With Quote
Old 09-26-2009, 01:44 PM   #16 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 1
Default

Quote:
Originally Posted by sukumar_77 View Post
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
Martin_Kuhn is offline   Reply With Quote
Old 10-06-2009, 03:20 PM   #17 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 2
Default 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
shermankimb is offline   Reply With Quote
Old 10-19-2009, 05:12 AM   #18 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 171
Smile

Quote:
Originally Posted by danielb21 View Post
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.
wolverine is offline   Reply With Quote
Old 10-23-2009, 06:14 AM   #19 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 171
Smile 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.
wolverine is offline   Reply With Quote
Old 10-23-2009, 11:10 AM   #20 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 3
Default PDF Viewer Tutorial

Quote:
Originally Posted by danielb21 View Post
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
momolgtm is offline   Reply With Quote
Old 10-25-2009, 06:02 PM   #21 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 2
Default

Quote:
Originally Posted by danielb21 View Post
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.
hobnob is offline   Reply With Quote
Old 10-28-2009, 05:24 PM   #22 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 3
Default

Could you email the example to me as well? sjachec1@gmail.com
magick is offline   Reply With Quote
Old 10-30-2009, 02:34 PM   #23 (permalink)
shiva
 
shiva.0537's Avatar
 
Join Date: Jun 2009
Location: Hyderabad
Age: 23
Posts: 54
Send a message via Skype™ to shiva.0537
Default

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
shiva.0537 is offline   Reply With Quote
Old 11-13-2009, 07:21 AM   #24 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 5
Default 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,
Sumit is offline   Reply With Quote
Old 12-10-2009, 06:58 PM   #25 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 28
Default

Hi, Any chance someone could forward the example project file on to me aswell??
Many thanks! (Andrew @ Aderrington.co.uk)
Andrew
aderrington is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 23,627
Threads: 38,383
Posts: 168,657
Top Poster: smasher (2,540)
Welcome to our newest member, comi
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:06 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0