NCViewerPDF.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 EasyTipView
  26. import NextcloudKit
  27. class NCViewerPDF: UIViewController, NCViewerPDFSearchDelegate {
  28. @IBOutlet weak var pdfContainer: UIView!
  29. var metadata = tableMetadata()
  30. var imageIcon: UIImage?
  31. private var filePath = ""
  32. private var pdfView = PDFView()
  33. private var pdfThumbnailScrollView = UIScrollView()
  34. private var pdfThumbnailView = PDFThumbnailView()
  35. private var pdfDocument: PDFDocument?
  36. private let pageView = UIView()
  37. private let pageViewLabel = UILabel()
  38. private var tipView: EasyTipView?
  39. private let thumbnailViewHeight: CGFloat = 70
  40. private let thumbnailViewWidth: CGFloat = 80
  41. private let thumbnailPadding: CGFloat = 2
  42. private let animateDuration: TimeInterval = 0.3
  43. private let pageViewtopAnchor: CGFloat = UIDevice.current.userInterfaceIdiom == .phone ? 10 : 30
  44. private let window = UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }
  45. private var defaultBackgroundColor: UIColor = .clear
  46. private var pdfThumbnailScrollViewTopAnchor: NSLayoutConstraint?
  47. private var pdfThumbnailScrollViewTrailingAnchor: NSLayoutConstraint?
  48. private var pdfThumbnailScrollViewWidthAnchor: NSLayoutConstraint?
  49. private var pageViewWidthAnchor: NSLayoutConstraint?
  50. private var hideStatusBar: Bool = false {
  51. didSet {
  52. setNeedsStatusBarAppearanceUpdate()
  53. }
  54. }
  55. // MARK: - View Life Cycle
  56. required init?(coder aDecoder: NSCoder) {
  57. super.init(coder: aDecoder)
  58. }
  59. override func viewDidLoad() {
  60. filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  61. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  62. let pageCount = CGFloat(pdfDocument?.pageCount ?? 0)
  63. defaultBackgroundColor = pdfView.backgroundColor
  64. view.backgroundColor = defaultBackgroundColor
  65. navigationController?.navigationBar.prefersLargeTitles = true
  66. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: .label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  67. navigationItem.title = metadata.fileNameView
  68. // PDF VIEW
  69. pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: pdfContainer.frame.width, height: pdfContainer.frame.height))
  70. pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
  71. pdfView.document = pdfDocument
  72. pdfView.document?.page(at: 0)?.annotations.forEach({
  73. $0.isReadOnly = true
  74. })
  75. pdfView.autoScales = true
  76. pdfView.displayMode = .singlePageContinuous
  77. pdfView.displayDirection = .vertical
  78. pdfContainer.addSubview(pdfView)
  79. // PDF THUMBNAIL
  80. pdfThumbnailScrollView.translatesAutoresizingMaskIntoConstraints = false
  81. pdfThumbnailScrollView.backgroundColor = defaultBackgroundColor
  82. pdfThumbnailScrollView.showsVerticalScrollIndicator = false
  83. pdfContainer.addSubview(pdfThumbnailScrollView)
  84. pdfThumbnailScrollView.bottomAnchor.constraint(equalTo: pdfContainer.bottomAnchor).isActive = true
  85. pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: pdfContainer.safeAreaLayoutGuide.topAnchor)
  86. pdfThumbnailScrollViewTopAnchor?.isActive = true
  87. pdfThumbnailScrollViewTrailingAnchor = pdfThumbnailScrollView.trailingAnchor.constraint(equalTo: pdfContainer.trailingAnchor)
  88. pdfThumbnailScrollViewTrailingAnchor?.isActive = true
  89. pdfThumbnailScrollViewWidthAnchor = pdfThumbnailScrollView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  90. pdfThumbnailScrollViewWidthAnchor?.isActive = true
  91. pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
  92. pdfThumbnailView.pdfView = pdfView
  93. pdfThumbnailView.layoutMode = .vertical
  94. pdfThumbnailView.thumbnailSize = CGSize(width: thumbnailViewHeight, height: thumbnailViewHeight)
  95. pdfThumbnailView.backgroundColor = .clear
  96. pdfThumbnailScrollView.isHidden = true
  97. pdfThumbnailScrollView.addSubview(pdfThumbnailView)
  98. NSLayoutConstraint.activate([
  99. pdfThumbnailView.topAnchor.constraint(equalTo: pdfThumbnailScrollView.topAnchor),
  100. pdfThumbnailView.bottomAnchor.constraint(equalTo: pdfThumbnailScrollView.bottomAnchor),
  101. pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.leadingAnchor),
  102. pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.trailingAnchor, constant: (window?.safeAreaInsets.left ?? 0)),
  103. pdfThumbnailView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  104. ])
  105. let contentViewCenterY = pdfThumbnailView.centerYAnchor.constraint(equalTo: pdfThumbnailScrollView.centerYAnchor)
  106. contentViewCenterY.priority = .defaultLow
  107. let contentViewHeight = pdfThumbnailView.heightAnchor.constraint(equalToConstant: CGFloat(pageCount * thumbnailViewHeight) + CGFloat(pageCount * thumbnailPadding) + 30)
  108. contentViewHeight.priority = .defaultLow
  109. NSLayoutConstraint.activate([
  110. contentViewCenterY,
  111. contentViewHeight
  112. ])
  113. // COUNTER PDF PAGE VIEW
  114. pageView.translatesAutoresizingMaskIntoConstraints = false
  115. pageView.layer.cornerRadius = 10
  116. pageView.backgroundColor = .systemBackground.withAlphaComponent(
  117. UIAccessibility.isReduceTransparencyEnabled ? 1 : 0.5
  118. )
  119. pdfContainer.addSubview(pageView)
  120. NSLayoutConstraint.activate([
  121. pageView.topAnchor.constraint(equalTo: pdfContainer.safeAreaLayoutGuide.topAnchor, constant: pageViewtopAnchor),
  122. pageView.heightAnchor.constraint(equalToConstant: 30),
  123. pageView.leftAnchor.constraint(equalTo: pdfContainer.safeAreaLayoutGuide.leftAnchor, constant: 10)
  124. ])
  125. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  126. pageViewWidthAnchor?.isActive = true
  127. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  128. pageViewLabel.textAlignment = .center
  129. pageViewLabel.textColor = .label
  130. pageView.addSubview(pageViewLabel)
  131. NSLayoutConstraint.activate([
  132. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor),
  133. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor),
  134. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor),
  135. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor)
  136. ])
  137. // GESTURE
  138. let tapPdfView = UITapGestureRecognizer(target: self, action: #selector(tapPdfView))
  139. tapPdfView.numberOfTapsRequired = 1
  140. pdfView.addGestureRecognizer(tapPdfView)
  141. // recognize single / double tap
  142. for gesture in pdfView.gestureRecognizers! {
  143. tapPdfView.require(toFail: gesture)
  144. }
  145. let swipePdfView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  146. swipePdfView.direction = .right
  147. swipePdfView.delegate = self
  148. pdfView.addGestureRecognizer(swipePdfView)
  149. let edgePdfView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  150. edgePdfView.edges = .right
  151. edgePdfView.delegate = self
  152. pdfView.addGestureRecognizer(edgePdfView)
  153. let swipePdfThumbnailScrollView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  154. swipePdfThumbnailScrollView.direction = .right
  155. pdfThumbnailScrollView.addGestureRecognizer(swipePdfThumbnailScrollView)
  156. setConstraints()
  157. handlePageChange()
  158. }
  159. override func viewDidAppear(_ animated: Bool) {
  160. super.viewDidAppear(animated)
  161. UIView.animate(withDuration: 0.1, animations: {
  162. if let pdf = self.pdfView.document, let page = pdf.page(at: 0) {
  163. self.pdfView.go(to: page)
  164. }
  165. })
  166. showTip()
  167. }
  168. @objc func viewUnload() {
  169. navigationController?.popViewController(animated: true)
  170. }
  171. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  172. super.viewWillTransition(to: size, with: coordinator)
  173. tipView?.dismiss()
  174. coordinator.animate(alongsideTransition: { context in
  175. self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (self.window?.safeAreaInsets.right ?? 0)
  176. self.pdfThumbnailScrollView.isHidden = true
  177. }, completion: { context in
  178. self.setConstraints()
  179. self.showTip()
  180. })
  181. }
  182. override var prefersStatusBarHidden: Bool {
  183. return hideStatusBar
  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: - Tip
  197. func showTip() {
  198. if !NCManageDatabase.shared.tipExists(NCGlobal.shared.tipNCViewerPDFThumbnail) {
  199. self.tipView?.show(forView: self.pdfThumbnailScrollView, withinSuperview: self.view)
  200. }
  201. }
  202. // MARK: - NotificationCenter
  203. @objc func uploadStartFile(_ notification: NSNotification) {
  204. guard let userInfo = notification.userInfo as NSDictionary?,
  205. let serverUrl = userInfo["serverUrl"] as? String,
  206. serverUrl == self.metadata.serverUrl,
  207. let fileName = userInfo["fileName"] as? String,
  208. fileName == self.metadata.fileName
  209. else { return }
  210. NCActivityIndicator.shared.start()
  211. }
  212. @objc func uploadedFile(_ notification: NSNotification) {
  213. guard let userInfo = notification.userInfo as NSDictionary?,
  214. let serverUrl = userInfo["serverUrl"] as? String,
  215. serverUrl == self.metadata.serverUrl,
  216. let fileName = userInfo["fileName"] as? String,
  217. fileName == self.metadata.fileName,
  218. let error = userInfo["error"] as? NKError
  219. else {
  220. return
  221. }
  222. NCActivityIndicator.shared.stop()
  223. if error == .success {
  224. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  225. pdfView.document = pdfDocument
  226. pdfView.layoutDocumentView()
  227. }
  228. }
  229. @objc func favoriteFile(_ notification: NSNotification) {
  230. guard let userInfo = notification.userInfo as NSDictionary?,
  231. let ocId = userInfo["ocId"] as? String,
  232. ocId == self.metadata.ocId,
  233. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  234. else { return }
  235. self.metadata = metadata
  236. }
  237. @objc func moveFile(_ notification: NSNotification) {
  238. guard let userInfo = notification.userInfo as NSDictionary?,
  239. let ocId = userInfo["ocId"] as? String,
  240. ocId == self.metadata.ocId,
  241. let ocIdNew = userInfo["ocIdNew"] as? String,
  242. let metadataNew = NCManageDatabase.shared.getMetadataFromOcId(ocIdNew)
  243. else { return }
  244. self.metadata = metadataNew
  245. }
  246. @objc func deleteFile(_ notification: NSNotification) {
  247. guard let userInfo = notification.userInfo as NSDictionary?,
  248. let ocId = userInfo["ocId"] as? [String],
  249. let error = userInfo["error"] as? NKError
  250. else { return }
  251. if error == .success, let ocId = ocId.first, metadata.ocId == ocId {
  252. viewUnload()
  253. }
  254. }
  255. @objc func renameFile(_ notification: NSNotification) {
  256. guard let userInfo = notification.userInfo as NSDictionary?,
  257. let ocId = userInfo["ocId"] as? String,
  258. ocId == self.metadata.ocId,
  259. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  260. else { return }
  261. self.metadata = metadata
  262. navigationItem.title = metadata.fileNameView
  263. }
  264. @objc func searchText() {
  265. let viewerPDFSearch = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  266. viewerPDFSearch.delegate = self
  267. viewerPDFSearch.pdfDocument = pdfDocument
  268. let navigaionController = UINavigationController(rootViewController: viewerPDFSearch)
  269. self.present(navigaionController, animated: true)
  270. }
  271. @objc func goToPage() {
  272. guard let pdfDocument = pdfView.document else { return }
  273. let alertMessage = NSString(format: NSLocalizedString("_this_document_has_%@_pages_", comment: "") as NSString, "\(pdfDocument.pageCount)") as String
  274. let alertController = UIAlertController(title: NSLocalizedString("_go_to_page_", comment: ""), message: alertMessage, preferredStyle: .alert)
  275. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  276. alertController.addTextField(configurationHandler: { textField in
  277. textField.placeholder = NSLocalizedString("_page_", comment: "")
  278. textField.keyboardType = .decimalPad
  279. })
  280. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { [unowned self] _ in
  281. if let pageLabel = alertController.textFields?.first?.text {
  282. self.selectPage(with: pageLabel)
  283. }
  284. }))
  285. self.present(alertController, animated: true)
  286. }
  287. // MARK: - Action
  288. @objc func openMenuMore() {
  289. if imageIcon == nil { imageIcon = UIImage(named: "file_pdf") }
  290. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false, imageIcon: imageIcon)
  291. }
  292. // MARK: - Gesture Recognizer
  293. @objc func tapPdfView(_ recognizer: UITapGestureRecognizer) {
  294. pdfThumbnailScrollViewTopAnchor?.isActive = false
  295. if navigationController?.isNavigationBarHidden ?? false {
  296. navigationController?.setNavigationBarHidden(false, animated: true)
  297. hideStatusBar = false
  298. pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: pdfContainer.safeAreaLayoutGuide.topAnchor)
  299. } else {
  300. navigationController?.setNavigationBarHidden(true, animated: true)
  301. hideStatusBar = true
  302. pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: pdfContainer.topAnchor)
  303. }
  304. pdfThumbnailScrollViewTopAnchor?.isActive = true
  305. handlePageChange()
  306. closePdfThumbnail()
  307. }
  308. @objc func gestureClosePdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  309. if recognizer.state == .recognized {
  310. closePdfThumbnail()
  311. }
  312. }
  313. @objc func gestureOpenPdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  314. guard let pdfDocument = pdfView.document, !pdfDocument.isLocked else { return }
  315. OpenPdfThumbnail()
  316. }
  317. // MARK: - OPEN / CLOSE Thumbnail
  318. func OpenPdfThumbnail() {
  319. if let tipView = self.tipView {
  320. tipView.dismiss()
  321. NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerPDFThumbnail)
  322. self.tipView = nil
  323. }
  324. self.pdfThumbnailScrollView.isHidden = false
  325. self.pdfThumbnailScrollViewWidthAnchor?.constant = thumbnailViewWidth + (window?.safeAreaInsets.right ?? 0)
  326. UIView.animate(withDuration: animateDuration, animations: {
  327. self.pdfThumbnailScrollViewTrailingAnchor?.constant = 0
  328. // self.view.layoutIfNeeded()
  329. self.pdfContainer.layoutIfNeeded()
  330. })
  331. }
  332. func closePdfThumbnail() {
  333. guard !self.pdfThumbnailScrollView.isHidden else { return }
  334. UIView.animate(withDuration: animateDuration) {
  335. self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (self.window?.safeAreaInsets.right ?? 0)
  336. // self.view.layoutIfNeeded()
  337. self.pdfContainer.layoutIfNeeded()
  338. } completion: { _ in
  339. self.pdfThumbnailScrollView.isHidden = true
  340. }
  341. }
  342. // MARK: -
  343. func setConstraints() {
  344. let widthThumbnail = thumbnailViewWidth + (window?.safeAreaInsets.right ?? 0)
  345. UIView.animate(withDuration: animateDuration, animations: {
  346. // Close
  347. self.pdfThumbnailScrollView.isHidden = true
  348. self.pdfThumbnailScrollViewTrailingAnchor?.constant = widthThumbnail
  349. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  350. // self.view.layoutIfNeeded()
  351. self.pdfContainer.layoutIfNeeded()
  352. self.pdfView.autoScales = true
  353. })
  354. }
  355. @objc func handlePageChange() {
  356. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  357. guard let totalPages = pdfView.document?.pageCount else { return }
  358. let visibleRect = CGRect(x: pdfThumbnailScrollView.contentOffset.x, y: pdfThumbnailScrollView.contentOffset.y, width: pdfThumbnailScrollView.bounds.size.width, height: pdfThumbnailScrollView.bounds.size.height)
  359. let centerPoint = CGPoint(x: visibleRect.size.width/2, y: visibleRect.size.height/2)
  360. let currentPageY = CGFloat(curPage) * thumbnailViewHeight + CGFloat(curPage) * thumbnailPadding
  361. var gotoY = currentPageY - centerPoint.y
  362. let startY = visibleRect.origin.y < 0 ? 0 : (visibleRect.origin.y + thumbnailViewHeight)
  363. let endY = visibleRect.origin.y + visibleRect.height
  364. if currentPageY < startY {
  365. if gotoY < 0 { gotoY = 0 }
  366. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  367. } else if currentPageY > endY {
  368. if gotoY > pdfThumbnailView.frame.height - visibleRect.height {
  369. gotoY = pdfThumbnailView.frame.height - visibleRect.height
  370. }
  371. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  372. } else {
  373. print("visible")
  374. }
  375. pageView.alpha = 1
  376. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  377. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  378. UIView.animate(withDuration: 1.0, delay: 2.5, animations: {
  379. self.pageView.alpha = 0
  380. })
  381. }
  382. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  383. removeAllAnnotations()
  384. pdfSelection.pages.forEach { page in
  385. let highlight = PDFAnnotation(bounds: pdfSelection.bounds(for: page), forType: .highlight, withProperties: nil)
  386. highlight.endLineStyle = .square
  387. highlight.color = .systemBlue
  388. page.addAnnotation(highlight)
  389. }
  390. if let page = pdfSelection.pages.first {
  391. pdfView.go(to: page)
  392. }
  393. handlePageChange()
  394. }
  395. private func selectPage(with label: String) {
  396. guard let pdf = pdfView.document else { return }
  397. if let pageNr = Int(label) {
  398. if pageNr > 0 && pageNr <= pdf.pageCount {
  399. if let page = pdf.page(at: pageNr - 1) {
  400. self.pdfView.go(to: page)
  401. }
  402. } else {
  403. let alertController = UIAlertController(title: NSLocalizedString("_invalid_page_", comment: ""),
  404. message: NSLocalizedString("_the_entered_page_number_does_not_exist_", comment: ""),
  405. preferredStyle: .alert)
  406. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: nil))
  407. self.present(alertController, animated: true, completion: nil)
  408. }
  409. }
  410. }
  411. func removeAllAnnotations() {
  412. guard let document = pdfDocument else { return }
  413. for i in 0..<document.pageCount {
  414. if let page = document.page(at: i) {
  415. let annotations = page.annotations
  416. for annotation in annotations {
  417. page.removeAnnotation(annotation)
  418. }
  419. }
  420. }
  421. }
  422. }
  423. extension NCViewerPDF: UIGestureRecognizerDelegate {
  424. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  425. return true
  426. }
  427. }
  428. extension NCViewerPDF: EasyTipViewDelegate {
  429. func easyTipViewDidTap(_ tipView: EasyTipView) {
  430. NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerPDFThumbnail)
  431. }
  432. func easyTipViewDidDismiss(_ tipView: EasyTipView) { }
  433. }