     }
     
     open func indexForPage(_ page: Int) -> Int {
-        var currentPage = page - 1
+        let currentPage = page - 1
         if currentPage <= 0 {
-            currentPage = 0
-        }
-        if let document = document, currentPage > document.pageCount {
-            currentPage = document.pageCount - 1
-        }
-        return currentPage
+            return 0
+        } else if let pageCount = document?.pageCount, currentPage > pageCount {
+            return pageCount - 1
+        } else {
+            return currentPage
+        }
     }
     
     open func displayPage(_ page: Int, animated: Bool) {

    }
    
    private func didDisplayPage(_ scrollView: UIScrollView) {
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        let page: Int
        switch flowLayout.scrollDirection {
        case .horizontal:
            page = Int((scrollView.contentOffset.x + scrollView.frame.width) / scrollView.frame.width)
        case .vertical:


extension PDFSinglePageViewer: UICollectionViewDelegateFlowLayout {
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        switch scrollDirection {
        case .horizontal:
            var size = bounds.size
            let contentInsetHeight = contentInset.bottom + contentInset.top + 1
