This is exactly what I am looking for. Can I have the app e-mailed to me so I can play with it. I am so new that I don't understand a lot of what is stated above. If I had the app I think I could add my PDF and get it working. I hope.
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
Read your code with interest. I'm new to iPhone development so could you please send a copy of the project to me at steve.callahan@btinternet.com
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:
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 for ur code. It works fine.Can u help me how to create a table in a pdf file & also add data to it?
Thanking you.
Can someone please explain where this part of the code is supposed to go?
Code:
// Create an instance of PDFViewController
PDFViewController *controller = [[PDFViewController alloc] initWithNibName:@"PDFView" bundle:nil];
// Get the path to our documents directory
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, 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];
hey get the ipod touch software 3.1.2 and download it.
then go to itunes and press option restore and click the software then go to blackra1n and press mac logo it will download and press make it rain. then your done. FOR MAC ONLY
Congratularions for your fantastic work. I'm new on iphone development and due to this can't put the to work. Please, could you send the sample project to my email: cfbfs@hotmail.com
Thanks a lot.
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!
i have read your pdf article. Its really interesting .. plz send me the sample project... c.j.sivaram@gmail.com
Thank you ! Very good and simple tutorial !
But i've a question: if you are reading a single page document, i see that the uiwebview has as default a big bounce in each side of the screen. So the question is: how can i remove this ?
Hi Dan,
Great, well described and very useful tutorial , helped me.
I am very new in iPhone app dev, and looking a tutorial to design pdf like creating table (report), currently using html table.
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.