NCViewerPDF.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //
  2. // NCViewerPDF.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/02/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import PDFKit
  25. import SwiftUI
  26. class NCViewerPDF: UIViewController, NCViewerPDFSearchDelegate, UIGestureRecognizerDelegate {
  27. var metadata = tableMetadata()
  28. var imageIcon: UIImage?
  29. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. private var filePath = ""
  31. private var pdfView = PDFView()
  32. private var pdfThumbnailScrollView = UIScrollView()
  33. private var pdfThumbnailView = PDFThumbnailView()
  34. private var pdfDocument: PDFDocument?
  35. private let pageView = UIView()
  36. private let pageViewLabel = UILabel()
  37. private let thumbnailViewHeight: CGFloat = 70
  38. private let thumbnailViewWidth: CGFloat = 80
  39. private let thumbnailPadding: CGFloat = 2
  40. private let animateDuration: TimeInterval = 0.3
  41. private var defaultBackgroundColor: UIColor = .clear
  42. private var pdfThumbnailScrollViewTrailingAnchor: NSLayoutConstraint?
  43. private var pdfThumbnailScrollViewWidthAnchor: NSLayoutConstraint?
  44. private var pageViewWidthAnchor: NSLayoutConstraint?
  45. // MARK: - View Life Cycle
  46. required init?(coder aDecoder: NSCoder) {
  47. super.init(coder: aDecoder)
  48. }
  49. override func viewDidLoad() {
  50. filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  51. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  52. let pageCount = CGFloat(pdfDocument?.pageCount ?? 0)
  53. defaultBackgroundColor = pdfView.backgroundColor
  54. view.backgroundColor = defaultBackgroundColor
  55. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  56. navigationItem.title = metadata.fileNameView
  57. // PDF VIEW
  58. pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
  59. pdfView.translatesAutoresizingMaskIntoConstraints = false
  60. pdfView.document = pdfDocument
  61. pdfView.autoScales = true
  62. pdfView.displayMode = .singlePageContinuous
  63. pdfView.displayDirection = .vertical
  64. pdfView.maxScaleFactor = 4.0
  65. pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
  66. view.addSubview(pdfView)
  67. NSLayoutConstraint.activate([
  68. pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  69. pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  70. pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)
  71. ])
  72. if UIDevice.current.userInterfaceIdiom == .pad {
  73. pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -thumbnailViewWidth).isActive = true
  74. } else {
  75. pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
  76. }
  77. // PDF THUMBNAIL
  78. pdfThumbnailScrollView.translatesAutoresizingMaskIntoConstraints = false
  79. pdfThumbnailScrollView.backgroundColor = defaultBackgroundColor
  80. pdfThumbnailScrollView.showsVerticalScrollIndicator = false
  81. view.addSubview(pdfThumbnailScrollView)
  82. NSLayoutConstraint.activate([
  83. pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  84. pdfThumbnailScrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
  85. ])
  86. pdfThumbnailScrollViewTrailingAnchor = pdfThumbnailScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  87. pdfThumbnailScrollViewTrailingAnchor?.isActive = true
  88. pdfThumbnailScrollViewWidthAnchor = pdfThumbnailScrollView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  89. pdfThumbnailScrollViewWidthAnchor?.isActive = true
  90. pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
  91. pdfThumbnailView.pdfView = pdfView
  92. pdfThumbnailView.layoutMode = .vertical
  93. pdfThumbnailView.thumbnailSize = CGSize(width: thumbnailViewHeight, height: thumbnailViewHeight)
  94. pdfThumbnailView.backgroundColor = .clear
  95. if UIDevice.current.userInterfaceIdiom == .pad {
  96. self.pdfThumbnailScrollView.isHidden = false
  97. } else {
  98. self.pdfThumbnailScrollView.isHidden = true
  99. }
  100. pdfThumbnailScrollView.addSubview(pdfThumbnailView)
  101. NSLayoutConstraint.activate([
  102. pdfThumbnailView.topAnchor.constraint(equalTo: pdfThumbnailScrollView.topAnchor),
  103. pdfThumbnailView.bottomAnchor.constraint(equalTo: pdfThumbnailScrollView.bottomAnchor),
  104. pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.leadingAnchor),
  105. pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.trailingAnchor, constant: (UIApplication.shared.keyWindow?.safeAreaInsets.left ?? 0)),
  106. pdfThumbnailView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  107. ])
  108. let contentViewCenterY = pdfThumbnailView.centerYAnchor.constraint(equalTo: pdfThumbnailScrollView.centerYAnchor)
  109. contentViewCenterY.priority = .defaultLow
  110. let contentViewHeight = pdfThumbnailView.heightAnchor.constraint(equalToConstant: CGFloat(pageCount * thumbnailViewHeight) + CGFloat(pageCount * thumbnailPadding) + 30)
  111. contentViewHeight.priority = .defaultLow
  112. NSLayoutConstraint.activate([
  113. contentViewCenterY,
  114. contentViewHeight
  115. ])
  116. // COUNTER PDF PAGE VIEW
  117. pageView.translatesAutoresizingMaskIntoConstraints = false
  118. pageView.layer.cornerRadius = 10
  119. pageView.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
  120. view.addSubview(pageView)
  121. NSLayoutConstraint.activate([
  122. pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
  123. pageView.heightAnchor.constraint(equalToConstant: 30),
  124. pageView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 10)
  125. ])
  126. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  127. pageViewWidthAnchor?.isActive = true
  128. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  129. pageViewLabel.textAlignment = .center
  130. pageViewLabel.textColor = .gray
  131. pageView.addSubview(pageViewLabel)
  132. NSLayoutConstraint.activate([
  133. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor),
  134. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor),
  135. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor),
  136. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor)
  137. ])
  138. // GESTURE
  139. let tapPdfView = UITapGestureRecognizer(target: self, action: #selector(tapPdfView))
  140. tapPdfView.numberOfTapsRequired = 1
  141. pdfView.addGestureRecognizer(tapPdfView)
  142. // recognize single / double tap
  143. for gesture in pdfView.gestureRecognizers! {
  144. tapPdfView.require(toFail: gesture)
  145. }
  146. let swipePdfView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  147. swipePdfView.direction = .right
  148. pdfView.addGestureRecognizer(swipePdfView)
  149. let swipePdfThumbnailScrollView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  150. swipePdfThumbnailScrollView.direction = .right
  151. pdfThumbnailScrollView.addGestureRecognizer(swipePdfThumbnailScrollView)
  152. let edgePdfView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  153. edgePdfView.edges = .right
  154. pdfView.addGestureRecognizer(edgePdfView)
  155. let edgeView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  156. edgeView.edges = .right
  157. view.addGestureRecognizer(edgeView)
  158. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  159. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  160. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  161. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  162. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  163. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  164. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  165. NotificationCenter.default.addObserver(self, selector: #selector(goToPage), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  166. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
  167. setConstraints()
  168. handlePageChange()
  169. }
  170. @objc func viewUnload() {
  171. navigationController?.popViewController(animated: true)
  172. }
  173. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  174. super.viewWillTransition(to: size, with: coordinator)
  175. coordinator.animate(alongsideTransition: { context in
  176. if UIDevice.current.userInterfaceIdiom == .phone {
  177. // Close
  178. self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  179. self.pdfThumbnailScrollView.isHidden = true
  180. }
  181. }, completion: { context in
  182. self.setConstraints()
  183. })
  184. }
  185. deinit {
  186. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  187. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  188. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  189. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  190. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  191. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  192. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  193. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  194. NotificationCenter.default.removeObserver(self, name: Notification.Name.PDFViewPageChanged, object: nil)
  195. }
  196. // MARK: - NotificationCenter
  197. @objc func uploadedFile(_ notification: NSNotification) {
  198. if let userInfo = notification.userInfo as NSDictionary? {
  199. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  200. if errorCode == 0 && metadata.ocId == self.metadata.ocId {
  201. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  202. pdfView.document = pdfDocument
  203. pdfView.layoutDocumentView()
  204. }
  205. }
  206. }
  207. }
  208. @objc func favoriteFile(_ notification: NSNotification) {
  209. if let userInfo = notification.userInfo as NSDictionary? {
  210. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  211. if metadata.ocId == self.metadata.ocId {
  212. self.metadata = metadata
  213. }
  214. }
  215. }
  216. }
  217. @objc func moveFile(_ notification: NSNotification) {
  218. if let userInfo = notification.userInfo as NSDictionary? {
  219. if let ocId = userInfo["ocId"] as? String, let ocIdNew = userInfo["ocIdNew"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let metadataNew = NCManageDatabase.shared.getMetadataFromOcId(ocIdNew) {
  220. if metadata.ocId == self.metadata.ocId {
  221. self.metadata = metadataNew
  222. }
  223. }
  224. }
  225. }
  226. @objc func deleteFile(_ notification: NSNotification) {
  227. if let userInfo = notification.userInfo as NSDictionary? {
  228. if let ocId = userInfo["OcId"] as? String {
  229. if ocId == self.metadata.ocId {
  230. viewUnload()
  231. }
  232. }
  233. }
  234. }
  235. @objc func renameFile(_ notification: NSNotification) {
  236. if let userInfo = notification.userInfo as NSDictionary? {
  237. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  238. if metadata.ocId == self.metadata.ocId {
  239. self.metadata = metadata
  240. navigationItem.title = metadata.fileNameView
  241. }
  242. }
  243. }
  244. }
  245. @objc func searchText() {
  246. let viewerPDFSearch = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  247. viewerPDFSearch.delegate = self
  248. viewerPDFSearch.pdfDocument = pdfDocument
  249. let navigaionController = UINavigationController(rootViewController: viewerPDFSearch)
  250. self.present(navigaionController, animated: true)
  251. }
  252. @objc func goToPage() {
  253. guard let pdfDocument = pdfView.document else { return }
  254. let alertMessage = NSString(format: NSLocalizedString("_this_document_has_%@_pages_", comment: "") as NSString, "\(pdfDocument.pageCount)") as String
  255. let alertController = UIAlertController(title: NSLocalizedString("_go_to_page_", comment: ""), message: alertMessage, preferredStyle: .alert)
  256. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  257. alertController.addTextField(configurationHandler: { textField in
  258. textField.placeholder = NSLocalizedString("_page_", comment: "")
  259. textField.keyboardType = .decimalPad
  260. })
  261. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { [unowned self] _ in
  262. if let pageLabel = alertController.textFields?.first?.text {
  263. self.selectPage(with: pageLabel)
  264. }
  265. }))
  266. self.present(alertController, animated: true)
  267. }
  268. // MARK: - Action
  269. @objc func openMenuMore() {
  270. if imageIcon == nil { imageIcon = UIImage(named: "file_pdf") }
  271. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false, imageIcon: imageIcon)
  272. }
  273. // MARK: - Gesture Recognizer
  274. @objc func tapPdfView(_ recognizer: UITapGestureRecognizer) {
  275. if navigationController?.isNavigationBarHidden ?? false {
  276. navigationController?.setNavigationBarHidden(false, animated: true)
  277. } else {
  278. navigationController?.setNavigationBarHidden(true, animated: true)
  279. }
  280. }
  281. @objc func gestureOpenPdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  282. if UIDevice.current.userInterfaceIdiom == .phone && self.pdfThumbnailScrollView.isHidden {
  283. self.pdfThumbnailScrollView.isHidden = false
  284. self.pdfThumbnailScrollViewWidthAnchor?.constant = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  285. UIView.animate(withDuration: animateDuration, animations: {
  286. self.pdfThumbnailScrollViewTrailingAnchor?.constant = 0
  287. self.view.layoutIfNeeded()
  288. })
  289. }
  290. }
  291. @objc func gestureClosePdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  292. if recognizer.state == .recognized && UIDevice.current.userInterfaceIdiom == .phone && !self.pdfThumbnailScrollView.isHidden {
  293. UIView.animate(withDuration: animateDuration) {
  294. self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  295. self.view.layoutIfNeeded()
  296. } completion: { _ in
  297. self.pdfThumbnailScrollView.isHidden = true
  298. }
  299. }
  300. }
  301. // MARK: -
  302. func setConstraints() {
  303. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  304. UIView.animate(withDuration: animateDuration, animations: {
  305. if UIDevice.current.userInterfaceIdiom == .phone {
  306. // Close
  307. self.pdfThumbnailScrollView.isHidden = true
  308. self.pdfThumbnailScrollViewTrailingAnchor?.constant = widthThumbnail
  309. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  310. } else {
  311. // Open
  312. self.pdfThumbnailScrollViewTrailingAnchor?.constant = 0
  313. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  314. }
  315. self.view.layoutIfNeeded()
  316. self.pdfView.autoScales = true
  317. })
  318. }
  319. @objc func handlePageChange() {
  320. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  321. guard let totalPages = pdfView.document?.pageCount else { return }
  322. let visibleRect = CGRect(x: pdfThumbnailScrollView.contentOffset.x, y: pdfThumbnailScrollView.contentOffset.y, width: pdfThumbnailScrollView.bounds.size.width, height: pdfThumbnailScrollView.bounds.size.height)
  323. let centerPoint = CGPoint(x: visibleRect.size.width/2, y: visibleRect.size.height/2)
  324. let currentPageY = CGFloat(curPage) * thumbnailViewHeight + CGFloat(curPage) * thumbnailPadding
  325. var gotoY = currentPageY - centerPoint.y
  326. let startY = visibleRect.origin.y < 0 ? 0 : (visibleRect.origin.y + thumbnailViewHeight)
  327. let endY = visibleRect.origin.y + visibleRect.height + thumbnailViewHeight
  328. if currentPageY < startY {
  329. if gotoY < 0 { gotoY = 0 }
  330. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  331. } else if currentPageY > endY {
  332. if gotoY > pdfThumbnailView.frame.height - visibleRect.height {
  333. gotoY = pdfThumbnailView.frame.height - visibleRect.height
  334. }
  335. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  336. } else {
  337. print("visible")
  338. }
  339. pageView.alpha = 1
  340. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  341. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  342. UIView.animate(withDuration: 1.0, delay: 2.5, animations: {
  343. self.pageView.alpha = 0
  344. })
  345. }
  346. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  347. pdfSelection.color = .yellow
  348. pdfView.currentSelection = pdfSelection
  349. pdfView.go(to: pdfSelection)
  350. }
  351. private func selectPage(with label: String) {
  352. guard let pdf = pdfView.document else { return }
  353. if let pageNr = Int(label) {
  354. if pageNr > 0 && pageNr <= pdf.pageCount {
  355. if let page = pdf.page(at: pageNr - 1) {
  356. self.pdfView.go(to: page)
  357. }
  358. } else {
  359. let alertController = UIAlertController(title: NSLocalizedString("_invalid_page_", comment: ""),
  360. message: NSLocalizedString("_the_entered_page_number_doesn't_exist_", comment: ""),
  361. preferredStyle: .alert)
  362. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: nil))
  363. self.present(alertController, animated: true, completion: nil)
  364. }
  365. }
  366. }
  367. }