Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

PDF Viewer Tutorial

135

Replies

  • stevecallahanstevecallahan Posts: 4New Users
    edited May 2010
    danielb21 wrote: »
    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

    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

    Many Thanks
  • jcristo2jcristo2 Posts: 1New Users
    edited May 2010
    Thank you very much for your work.

    And to all who keep asking for the code, just read the message list and download it from GitHub.:eek:
  • GoodJackGoodJack Posts: 1New Users
    edited May 2010
    Hi this is exactly what I've been looking for.
    Can you send me the source to goodjack@live.com?

    Thank you very much!!!! :)
  • NikiNiki Posts: 5New Users
    edited May 2010
    danielb21 wrote: »
    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:
    @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.
    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.
    // 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.
  • aakburnsaakburns Posts: 7New Users
    edited May 2010
    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(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];
  • francpenderfrancpender Posts: 1New Users
    edited May 2010
    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
  • cflaviocflavio Posts: 1New Users
    edited June 2010
    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.
  • ZalaZala Posts: 30Registered Users
    edited June 2010
    hey may i have the sample code too?

    stuck at the last part duno where should i be putting it.

    Please send it to win11_6@hotmail.com

    Thanks alot.
  • TterrafanTterrafan Posts: 1New Users
    edited June 2010
    Hi,
    could you please send me the sample code?
    my e-mail: tterrafan@googlemail.com
  • sivaramsivaram Posts: 32Registered Users
    edited June 2010
    DGoulian wrote: »
    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
  • bladeolsonbladeolson Posts: 31Registered Users
    edited July 2010
    I can't seem to find a way to change the background grey color around the PDF. Changeing in interface builder does nothing.

    Also, anybody know how to remove the Page count indicator?

    Blade
    <a href="http://www.olsonvox.com"; target="_blank">www.olsonvox.com</a><br />
    <br />
    olsonvox apps:<br />
    <br />
    <a href="http://itunes.apple.com/us/app/olsynth/id378761078?mt=8"; target="_blank">olsynth</a><br />
    <a href="http://itunes.apple.com/jp/ap
  • inlo Kiminlo Kim Posts: 1New Users
    edited July 2010
    Can you send me the code?
    inlo@nate.com
  • BoggardBoggard Posts: 8New Users
    edited July 2010
    I'd love to get the sample too.

    jmoral88@gmail.com

    Thank you in advance!
  • ferroch85ferroch85 Posts: 3New Users
    edited July 2010
    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 ?
  • DSADSA Posts: 26Registered Users
    edited July 2010
    Great tutorial :)
  • praveen_jmpraveen_jm Posts: 2New Users
    edited August 2010
    Hi did u figure out how to edit a PDF document in iphone
  • codesencodesen Posts: 1New Users
    edited August 2010
    hi,

    Can I've the option to search the pdf content.
  • RaptusisRaptusis Posts: 2New Users
    edited August 2010
    Thanks for the useful code. ;)
  • iThomasiThomas Posts: 1New Users
    edited August 2010
    Hi All! This is exactly what I've been looking for in two days.

    Would you mind send me the source code to thomas.wh.lau@gmail.com?

    Thank you for your help.
  • starbuckystarbucky Posts: 1New Users
    edited August 2010
    Wow! Thanks for the code and tutorial this great example of how great community support can be! Works like a charm.

    There is a bit of a declaration mixup though. Change pdfURL to pdfUrl to make it integrate with the code from the first example

    For people who want to know where to place the resource code; replace the viewDidLoad in your [yourname]viewController.m file with this:

    [PHP] - (void)viewDidLoad {
    [super viewDidLoad];

    pdfUrl = [NSURL fileURLWithPath:NSBundle mainBundle] pathForResource:@"[yourpdffilenamehere" ofType:@"pdf";
    [webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
    }
    [/PHP]

    As said, I changed pdfURL to pdfUrl in the example code supplied to integrate it with the tutorial by danielb21
  • techsmithtechsmith Posts: 1New Users
    edited September 2010
    danielb21 wrote: »
    Hey everyone,

    I've posted the project on github now because I can't keep up with all of the requests.

    You can find it here:
    danberry's PDF-Viewer at master - GitHub

    Thanks!
    Dan

    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.

    thanks a ton,:)
  • GerBOGerBO Posts: 1New Users
    edited September 2010
    Hi Daniel!!!

    Can you send me your code of PDF Viewer for check my errors code,
    Thanks.

    my email is enlaceti@gmail.com

    Good day!! :rolleyes:

    danielb21 wrote: »
    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
  • dexertmoondexertmoon Posts: 3New Users
    edited September 2010
    Hi Daniel,

    Thanks a million.

    :)
  • politespolites Posts: 2New Users
    edited October 2010
    Hi Dan!

    Thanks for this tutorial, would it be alright to send me your sample code to play with?

    thanks in advance

    Pol

    (polturtle@yahoo.com)
  • DSADSA Posts: 26Registered Users
    edited October 2010
    polites wrote: »
    Hi Dan!

    Thanks for this tutorial, would it be alright to send me your sample code to play with?

    thanks in advance

    Pol

    (polturtle@yahoo.com)

    Hey amigo check out page 2 there is a link for the code :)

    Dan great code I do have a question however,

    1)Can the white screen that precedes the loading of the PDF be eliminated by a sleep mode enabled splash screen?
Sign In or Register to comment.