2

I used following code to paginate the UIWebView.

self.webView.paginationMode = UIWebPaginationModeLeftToRight;
self.webView.paginationBreakingMode = UIWebPaginationBreakingModePage;
self.webView.scrollView.pagingEnabled = YES;

Now I want to know the total number of pages it created. The IOS7 document says UIWebView has a property called pageCount. I tried it, but it always returns 1.

How can I get the number of pages it created after pagination?

Thanks.

user890207
  • 369
  • 1
  • 4
  • 14

2 Answers2

4

If you want only the number of pages you can use CoreGraphics

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:@"your path"]);
int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFDocumentRelease(pdf);

If you want to count pages for HTML content you need to set delegate

self.webView.delegate = self;

and to implement UIWebViewDelegate method

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSUInteger count1 = webView.pageCount; // for iOS7 only!

    NSUInteger count2 = webView.scrollView.contentSize.width / webView.scrollView.frame.size.width; // for horizontal paging

    NSUInteger count3 = webView.scrollView.contentSize.height / webView.scrollView.frame.size.height; // for vertical paging
}

Note that you can get pageCount only after full rendering of the requested HTML page.

malex
  • 9,374
  • 3
  • 53
  • 71
  • I am using UIWebView to display HTML instead of PDF. Is there a way to show the total paginated page numbers? – user890207 Jan 26 '14 at 15:21
  • Thanks. This works. I also notice I can put pageCount inside `viewDidAppear` and it also works. – user890207 Jan 26 '14 at 16:22
  • Exactly. Rendering is done by the time of viewDidAppear. But if you want to load pages by some user request you need delegates. – malex Jan 26 '14 at 18:08
  • Note that you can set the pageLength property to split in multiple columns – Marc-Alexandre Bérubé Oct 02 '14 at 15:42
  • @malex: Will this work for document as well like word, excel which i am displaying in UIWebView. I am looking for Pagecount and horizontal/Vertical scrolling of these documents. – Arun Gupta Mar 07 '15 at 11:00
  • @ArunGupta, it should work for any web content. Anyway It's better to try. – malex Mar 07 '15 at 14:51
  • @Malex: I tried for different documents and pagecount (webView.scrollView.contentSize.height / webView.scrollView.frame.size.height) property seems to be working in case of vertical scrolling. It is possible to get the current page or page number when scrolling. – Arun Gupta Mar 24 '15 at 08:32
1

UIWebView is a subclass of UIScrollView. You can detect UIWebView page change event for scroll view delegate method:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

This delegate will be called each time the webView scrolls. Maintain your page number yourself. You can use scrollView.contentOffset.x and scrollView.contentOffset.y to determine the scrolling direction.

You can find more help here.

EDIT We can find page number of scrollview from contentoffset. Try something similar for webView. int page = scrollView.contentOffset.x / scrollView.frame.size.width;

Community
  • 1
  • 1
Rashad
  • 10,519
  • 4
  • 40
  • 67
  • I need to know the total number of pages before I scroll the page. I need to pass this value to the PageControl display the dots. In fact, I used similar function to get page `self.pageControl.numberOfPages = self.webView.scrollView.contentSize.width / pageWidth;`, but the `scrollView.contentSize.width` only shows one page width. I would expect it shows the width of all pages. – user890207 Jan 26 '14 at 15:18
  • Did you tried scrollView.contentOffset.x inted of contentSize.width?? – Rashad Jan 26 '14 at 15:36