views:

290

answers:

5

I got a pdf viewer in a uiwebview, from Daniel Barry http://github.com/danberry/PDF-Viewer and it works really well, it displays a pdf doc that you can scroll up and down.

Now I would like to display just one page at the time and also have a menu underneat the pdf page with a prev and next button. Can anybody help a newbe to figure out how? Thanks!

#import "PDFViewController.h"   // PDF View Controller

@implementation PDFViewController

// Synthesize UI Element properties
@synthesize webView;
// Synthesize Variable properties
@synthesize pdfURL;

#pragma mark -
#pragma mark UIViewController methods
- (void)viewDidLoad {
    [super viewDidLoad];

    pdfURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"stuff pdf" ofType:@"pdf"]];
    [webView loadRequest:[NSURLRequest requestWithURL:pdfURL]];
    webView.transform = CGAffineTransformScale( webView.transform, 1.00, 1.00 );
}

#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
    [webView release];

    [pdfURL release];

    [super dealloc];
}

@end
A: 

I think a webview should show PDFs by using a default request, no need to have a new implementation?

Anyways... There are 2 methods you could check out and link to your prev and next button

- (void)goBack:(id)sender 
{
 if ([webView canGoBack]) {
  [webView goBack];
 }
}

- (void)goForward:(id)sender
{
 if ([webView canGoForward]) {
  [webView goForward];
 }
}

Here is the documentation of UIWebView where the methods are explained in detail: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

EDIT: Oh I think, I first misunderstood your question...

Just create an Array of the PDF URLs that you want to load. Create a counter variable that will hold the index value of the currently opened PDF.

And finally similar to the methods above create a forward and backward method and link them to the prev and next buttons. In those methods you increment or decrement the counter by 1 and load the appropriate index from the array [array objectAtIndex:counter] so the next or previous PDF is opened.

If you need more help, let me know...

jd291
Thank you for the last line :-) As I have it now its just one pdf file with several pages in it, I What I have now is loaded the pdf file in the uiwebview, and I have made the prev and next buttons, and a index button that will show a chapter menu(i hope). If you could help me trough this I would be very happy, thanks.
Claes Gustavsson
ok now i get it! sorrry my native language ain't english ;)well to navigate between the different pages I think won't be possible. Because you can not use UIWebView like a UIScrollView for example where you could read out the position of the content. I guess, you'll just have to stick with the webview as it is. Or you could divide the PDF into several PDFs (for each page one) and load those as mentioned above.
jd291
EDIT: look at this question/answer, they show a way to figure out the content position, maybe you can go from there http://stackoverflow.com/questions/1394766/iphone-uiwebview-store-the-current-scroll-position
jd291
Ok, I will divide the pdf into several pdfs.I would like to be able to load the pdf from my server, is that possible or do I have to include it from the beginning?What next? The array? Should that be in the viewController.m file?
Claes Gustavsson
You can load it from a server, no problem. The webview is even going to handle the loading for you. It should go into the viewController.m probably, I don't know how your classes are laid out. Show some code...
jd291
how do you get the code readable :-)
Claes Gustavsson
put it as an EDIT in your question. There you can use the code functionality
jd291
I dont know how you got it with a grey background?
Claes Gustavsson
select the whole code and than hit the code button with the 01001 zeros and ones ;)
jd291
You learn something new every day :-)
Claes Gustavsson
A: 

ok im gonna post this as another answer...

In the header file add

NSArray *arrURLs;
NSInteger *currentIndex;

and make the array a property (retain), synthesize it in the .m file, also release it in dealloc.

In - (void)viewDidLoad initalize the array with all the URLs and set the currentIndex to 0.

arrUrls = [[NSArray alloc] initWithObjects:@"url", @"url", ..., nil];
currentIndex = 0;

add two methods that you call something like prevPage and nextPage and link them to your buttons. In those methods you do something like...

// next page
currentIndex += 1;
NSString *str = [arrUrls objectAtIndex:currentIndex];
NSURL *url = [NSURL URLWithString:str];
[webView loadRequest:[NSURLRequest requestWithURL:url]];

I would probably create another method where you set the currentIndex and check if it is in the correct bounds (e.g. should not be smaller than 0 and must be smaller than the length of the array). Than you can change the first line and let the method call handle the in/decrementing of the variable.

hope this helps.

jd291
if this answered your question, please select it as answer. thx
jd291
Hi jd291 I got some other thing that I had to do.Im new at this and dont really understand it all, sorry to say, but Im learning.With the array- if I have a 100 pdf files to display its going to be a long one :-) With the header file-I guess you mean the ViewController.m file? At the top in the ViewController.m file I added: @synthesize NSArray *arrURLs;@synthesize NSInteger *currentIndex; and released it in dellock like this: [NSArray release]; [NSInteger release];I put the array and the two methods for the buttons - (void)goBack:(id)sender... in the -(void)viewDidLoad, is this right?
Claes Gustavsson
i see. You are just starting with everything. I think best would be if you zip your project upload it, post the link and i send you the correct version back ;)
jd291
A: 

Jd291 you will be called God instead ;-) thanks alot,Ill fix a zip first thing tomorrow. R Claes

Claes Gustavsson
Hi Jd291Here is the link to the projecthttp://www.manmade.se/mediasaints/pdfviewer/pdfviewer.zip There is a message for you in the file.Thanks again, regards Claes
Claes Gustavsson
check your emails!
jd291
A: 

Hi Jd291 I just wonder if you have had a change to see my message in the zip file? R Claes

Claes Gustavsson
A: 

Can anybody help and get a prev and next button to work with this pdf viewer? Step by step since Im a beginner, thanks.

Claes Gustavsson