CPDFViewController.swift 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. //
  2. // CPDFViewController.swift
  3. // PDFViewer-Swift
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import ComPDFKit
  14. import AVFAudio
  15. import AVFoundation
  16. import MobileCoreServices
  17. import ComPDFKit_Tools
  18. class CPDFViewController: CPDFViewBaseController,CPDFFormBarDelegate,CPDFSoundPlayBarDelegate,CPDFAnnotationBarDelegate,CPDFToolsViewControllerDelegate,CPDFNoteOpenViewControllerDelegate,CPDFEditToolBarDelegate,CPDFSignatureViewControllerDelegate,CPDFKeyboardToolbarDelegate,CPDFDigitalSignatureToolBarDelegate,CImportCertificateViewControllerDelegate,CDigitalTypeSelectViewDelegate
  19. ,CPDFSigntureVerifyViewControllerDelegate,CSignatureTypeSelectViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate, CreateCertificateInfoViewControllerDelegate{
  20. var annotationBar: CPDFAnnotationToolBar?
  21. var formBar: CPDFFormToolBar?
  22. var soundPlayBar: CPDFSoundPlayBar?
  23. var annotationManage: CAnnotationManage?
  24. var toolBar: CPDFEditToolBar?
  25. var baseVC: CPDFEditViewController?
  26. var editMode: CPDFEditMode = .all
  27. var signatureAnnotation: CPDFSignatureWidgetAnnotation?
  28. var addImageRect: CGRect = CGRect.zero
  29. var addImagePage: CPDFPage?
  30. var digitalSignatureBar: CPDFDigitalSignatureToolBar?
  31. lazy var signtureViewController: CPDFSigntureViewController = {
  32. return CPDFSigntureViewController.init()
  33. }()
  34. var signtureVerifyViewController: CPDFSigntureVerifyViewController?
  35. var pkcs12DocumentPickerViewController: UIDocumentPickerViewController?
  36. var isSelctSignature: Bool = false
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. let editingConfig = CPDFEditingConfig.init()
  40. editingConfig.editingBorderWidth = 1.0
  41. editingConfig.editingOffsetGap = 5
  42. self.pdfListView?.editingConfig = editingConfig
  43. self.initAnnotationBar()
  44. self.initWithEditTool()
  45. self.initWithFormTool()
  46. self.initDigitalSignatureBar()
  47. switch self.configuration?.enterToolModel {
  48. case .viewer:
  49. self.enterViewerMode()
  50. case .annotation:
  51. self.enterAnnotationMode()
  52. case .form:
  53. self.enterFormMode()
  54. case .edit:
  55. self.enterEditMode()
  56. case .pageEdit:
  57. self.enterViewerMode()
  58. self.enterPDFPageEdit()
  59. case .signature:
  60. enterSignatureMode()
  61. default:
  62. break
  63. }
  64. NotificationCenter.default.addObserver(self, selector: #selector(signatureHaveChangeDidChangeNotification(_:)), name: CSignatureHaveChangeDidChangeNotification, object: nil)
  65. NotificationCenter.default.addObserver(self, selector: #selector(signatureTrustCerDidChangeNotification(_:)), name: CSignatureTrustCerDidChangeNotification, object: nil)
  66. NotificationCenter.default.addObserver(self, selector: #selector(PDFPageDidRemoveAnnotationNotification), name: NSNotification.Name.CPDFPageDidRemoveAnnotation, object: nil)
  67. }
  68. func initAnnotationBar() {
  69. if(self.pdfListView != nil) {
  70. self.annotationManage = CAnnotationManage.init(pdfListView: self.pdfListView!)
  71. self.annotationBar = CPDFAnnotationToolBar.init(annotationManage: self.annotationManage!)
  72. }
  73. var height:CGFloat = 44.0
  74. if #available(iOS 11.0, *) {
  75. height += self.view.safeAreaInsets.bottom
  76. }
  77. self.annotationBar?.frame = CGRect(x: 0, y: self.view.frame.size.height - height, width: self.view.frame.size.width, height: height)
  78. self.annotationBar?.delegate = self
  79. self.annotationBar?.parentVC = self
  80. if(self.annotationBar != nil) {
  81. self.view.addSubview(self.annotationBar!)
  82. }
  83. }
  84. func initWithEditTool() {
  85. if self.toolBar == nil && self.pdfListView != nil {
  86. self.toolBar = CPDFEditToolBar.init(pdfView: self.pdfListView!)
  87. }
  88. self.toolBar?.delegate = self
  89. if(toolBar != nil) {
  90. self.view.addSubview(self.toolBar!)
  91. }
  92. }
  93. func initWithFormTool() {
  94. if self.formBar == nil {
  95. self.formBar = CPDFFormToolBar.init(annotationManage: self.annotationManage)
  96. }
  97. self.formBar?.delegate = self;
  98. self.formBar?.parentVC = self;
  99. if(formBar != nil) {
  100. self.view.addSubview(self.formBar!)
  101. }
  102. }
  103. func initDigitalSignatureBar() {
  104. self.digitalSignatureBar = CPDFDigitalSignatureToolBar(pdfListView: self.pdfListView!)
  105. var height: CGFloat = 60.0
  106. if #available(iOS 11.0, *) {
  107. height += self.view.safeAreaInsets.bottom
  108. }
  109. self.digitalSignatureBar?.delegate = self
  110. }
  111. override func viewDidLayoutSubviews() {
  112. super.viewDidLayoutSubviews()
  113. if self.popMenu?.superview != nil {
  114. if #available(iOS 11.0, *) {
  115. self.popMenu?.showMenu(in: CGRect(x: self.view.frame.size.width - self.view.safeAreaInsets.right - 250, y: (self.navigationController?.navigationBar.frame)!.maxY, width: 250, height: CGFloat((self.configuration?.showMoreItems.count ?? 0) * 50)))
  116. } else {
  117. self.popMenu?.showMenu(in: CGRect(x: self.view.frame.size.width - 250, y: (self.navigationController?.navigationBar.frame)!.maxY, width: 250, height: CGFloat((self.configuration?.showMoreItems.count ?? 0) * 50)))
  118. }
  119. }
  120. var height: CGFloat = 44.0
  121. if #available(iOS 11.0, *) {
  122. height += self.view.safeAreaInsets.bottom
  123. }
  124. var bottomHeight: CGFloat = 0
  125. if self.pdfListView?.toolModel == .annotation{
  126. self.annotationBar?.frame = CGRect(x: 0, y: self.view.frame.size.height - height, width: self.view.frame.size.width, height: height)
  127. bottomHeight = self.annotationBar?.frame.size.height ?? 0
  128. } else if self.pdfListView?.toolModel == .edit {
  129. self.toolBar?.frame = CGRect(x: 0, y: self.view.frame.size.height - height, width: self.view.frame.size.width, height: height)
  130. bottomHeight = self.toolBar?.frame.size.height ?? 0
  131. } else if self.pdfListView?.toolModel == .form {
  132. self.formBar?.frame = CGRect(x: 0, y: self.view.frame.size.height - height, width: self.view.frame.size.width, height: height)
  133. bottomHeight = self.formBar?.frame.size.height ?? 0
  134. }
  135. if self.digitalSignatureBar?.superview != nil {
  136. height += 14
  137. self.digitalSignatureBar?.frame = CGRect(x: 0, y: self.view.frame.size.height - height, width: self.view.frame.size.width, height: height)
  138. }
  139. height = self.navigationController?.navigationBar.frame.maxY ?? 0
  140. if self.signtureViewController.view.superview != nil {
  141. self.signtureViewController.view.frame = CGRect(x: 0, y: height, width: self.view.frame.size.width, height: 44.0)
  142. }
  143. if self.pdfListView?.toolModel == .annotation {
  144. if !self.navigationController!.isNavigationBarHidden {
  145. UIView.animate(withDuration: 0.3) {
  146. if(self.annotationBar != nil) {
  147. var frame = self.annotationBar?.frame ?? CGRect.zero
  148. frame.origin.y = self.view.bounds.size.height - frame.size.height
  149. self.annotationBar?.frame = frame
  150. }
  151. }
  152. } else {
  153. UIView.animate(withDuration: 0.3) {
  154. if(self.annotationBar != nil) {
  155. var frame = self.annotationBar?.frame ?? CGRect.zero
  156. frame.origin.y = self.view.bounds.size.height
  157. self.annotationBar?.frame = frame
  158. }
  159. }
  160. }
  161. } else {
  162. }
  163. if CPDFKitConfig.sharedInstance().displayDirection() == .vertical {
  164. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  165. inset.bottom = 10 + bottomHeight
  166. self.pdfListView?.documentView().contentInset = inset
  167. } else {
  168. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  169. inset.bottom = 0
  170. self.pdfListView?.documentView().contentInset = inset
  171. }
  172. }
  173. // MARK: - Public Methods
  174. override func selectDocumentRefresh() {
  175. if self.functionTypeState == .annotation {
  176. self.pdfListView?.setAnnotationMode(.CPDFViewAnnotationModenone)
  177. self.annotationBar?.updatePropertiesButtonState()
  178. self.annotationBar?.reloadData()
  179. self.annotationBar?.updateUndoRedoState()
  180. } else if self.functionTypeState == .form {
  181. self.formBar?.initUndoRedo()
  182. }
  183. }
  184. override func reloadDocument(withFilePath filePath: String, password: String?, completion: @escaping (Bool) -> Void) {
  185. navigationController?.view.isUserInteractionEnabled = false
  186. if loadingView.superview == nil {
  187. view.addSubview(self.loadingView)
  188. }
  189. self.loadingView.startAnimating()
  190. DispatchQueue.global(qos: .default).async {
  191. let url = URL(fileURLWithPath: filePath)
  192. let document = CPDFDocument(url: url)
  193. if document?.isLocked == true {
  194. document?.unlock(withPassword: password)
  195. }
  196. DispatchQueue.main.async {
  197. self.navigationController?.view.isUserInteractionEnabled = true
  198. self.loadingView.stopAnimating()
  199. self.loadingView.removeFromSuperview()
  200. if let error = document?.error, error._code != CPDFDocumentPasswordError {
  201. let okAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { _ in
  202. self.navigationController?.popViewController(animated: true)
  203. }
  204. let alert = UIAlertController(title: "", message: NSLocalizedString("Sorry PDF Reader Can't open this pdf file!", comment: ""), preferredStyle: .alert)
  205. alert.addAction(okAction)
  206. self.present(alert, animated: true, completion: nil)
  207. completion(false)
  208. } else {
  209. self.pdfListView?.document = document
  210. completion(true)
  211. }
  212. }
  213. }
  214. }
  215. // MARK: - Private Methods
  216. func enterEditMode() {
  217. self.functionTypeState = .edit
  218. self.selectDocumentRefresh()
  219. self.toolBar?.isHidden = false
  220. self.annotationBar?.isHidden = true
  221. self.formBar?.isHidden = true
  222. self.digitalSignatureBar?.isHidden = true
  223. self.pdfListView?.setToolModel(.edit)
  224. if CPDFEditMode.text == self.toolBar?.editToolBarSelectType {
  225. self.pdfListView?.beginEditingLoadType(.text)
  226. self.pdfListView?.setShouAddEdit(.text)
  227. } else if CPDFEditMode.image == self.toolBar?.editToolBarSelectType {
  228. self.pdfListView?.beginEditingLoadType(.image)
  229. self.pdfListView?.setShouAddEdit(.image)
  230. } else {
  231. self.pdfListView?.beginEditingLoadType([.text, .image])
  232. self.pdfListView?.setShouAddEdit([])
  233. }
  234. self.navigationTitle = NSLocalizedString("Content Editor", comment: "")
  235. self.titleButton?.setTitle(self.navigationTitle as String?, for: .normal)
  236. self.toolBar?.updateButtonState()
  237. var frame = self.toolBar?.frame ?? CGRect.zero
  238. frame.origin.y = self.view.bounds.size.height - frame.size.height
  239. self.toolBar?.frame = frame
  240. if digitalSignatureBar?.superview != nil {
  241. digitalSignatureBar?.removeFromSuperview()
  242. }
  243. self.viewDidLayoutSubviews()
  244. }
  245. func enterAnnotationMode() {
  246. self.functionTypeState = .annotation
  247. self.toolBar?.isHidden = true
  248. self.annotationBar?.isHidden = false
  249. self.formBar?.isHidden = true
  250. self.digitalSignatureBar?.isHidden = true
  251. if self.pdfListView?.isEdited() == true {
  252. DispatchQueue.global(qos: .default).async {
  253. self.pdfListView?.commitEditing()
  254. DispatchQueue.main.async {
  255. self.pdfListView?.endOfEditing()
  256. }
  257. }
  258. } else {
  259. self.pdfListView?.endOfEditing()
  260. }
  261. self.pdfListView?.setToolModel(.annotation)
  262. self.navigationTitle = NSLocalizedString("Annotation", comment: "")
  263. self.titleButton?.setTitle(self.navigationTitle as String?, for: .normal)
  264. var frame = self.annotationBar?.frame ?? CGRect.zero
  265. frame.origin.y = self.view.bounds.size.height - frame.size.height
  266. self.annotationBar?.frame = frame
  267. if CPDFKitConfig.sharedInstance().displayDirection() == .vertical {
  268. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  269. inset.bottom = 10 + (self.annotationBar?.frame.size.height ?? 0)
  270. self.pdfListView?.documentView().contentInset = inset
  271. } else {
  272. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  273. inset.bottom = 0
  274. self.pdfListView?.documentView().contentInset = inset
  275. }
  276. if digitalSignatureBar?.superview != nil {
  277. digitalSignatureBar?.removeFromSuperview()
  278. }
  279. self.viewDidLayoutSubviews()
  280. }
  281. func enterViewerMode() {
  282. self.functionTypeState = .viewer
  283. self.toolBar?.isHidden = true
  284. self.formBar?.isHidden = true
  285. self.annotationBar?.isHidden = true
  286. self.digitalSignatureBar?.isHidden = true
  287. if self.pdfListView?.isEdited() == true {
  288. DispatchQueue.global(qos: .default).async {
  289. self.pdfListView?.commitEditing()
  290. DispatchQueue.main.async {
  291. self.pdfListView?.endOfEditing()
  292. }
  293. }
  294. } else {
  295. self.pdfListView?.endOfEditing()
  296. }
  297. self.pdfListView?.setToolModel(.viewer)
  298. self.navigationTitle = NSLocalizedString("Viewer", comment: "")
  299. self.titleButton?.setTitle(self.navigationTitle as String?, for: .normal)
  300. var frame = self.annotationBar?.frame ?? CGRect.zero
  301. frame.origin.y = self.view.bounds.size.height
  302. self.annotationBar?.frame = frame
  303. if CPDFKitConfig.sharedInstance().displayDirection() == .vertical {
  304. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  305. inset.bottom = 0
  306. self.pdfListView?.documentView().contentInset = inset
  307. } else {
  308. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  309. inset.bottom = 0
  310. self.pdfListView?.documentView().contentInset = inset
  311. }
  312. if digitalSignatureBar?.superview != nil {
  313. digitalSignatureBar?.removeFromSuperview()
  314. }
  315. self.viewDidLayoutSubviews()
  316. }
  317. func enterFormMode() {
  318. self.functionTypeState = .form
  319. self.toolBar?.isHidden = true
  320. self.annotationBar?.isHidden = true
  321. self.formBar?.isHidden = false
  322. self.digitalSignatureBar?.isHidden = true
  323. if pdfListView?.isEdited() == true {
  324. DispatchQueue.global(qos: .default).async {
  325. self.pdfListView?.commitEditing()
  326. DispatchQueue.main.async {
  327. self.pdfListView?.endOfEditing()
  328. }
  329. }
  330. } else {
  331. self.pdfListView?.endOfEditing()
  332. }
  333. pdfListView?.setToolModel(.form)
  334. self.navigationTitle = NSLocalizedString("Form", comment: "")
  335. titleButton?.setTitle(navigationTitle as String?, for: .normal)
  336. var frame = self.formBar?.frame ?? CGRect.zero
  337. frame.origin.y = view.bounds.size.height - frame.size.height
  338. self.formBar?.frame = frame
  339. if CPDFKitConfig.sharedInstance().displayDirection() == .vertical {
  340. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  341. inset.bottom = 10 + (self.formBar?.frame.size.height ?? 0)
  342. self.pdfListView?.documentView().contentInset = inset
  343. } else {
  344. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  345. inset.bottom = 0
  346. self.pdfListView?.documentView().contentInset = inset
  347. }
  348. if digitalSignatureBar?.superview != nil {
  349. digitalSignatureBar?.removeFromSuperview()
  350. }
  351. self.viewDidLayoutSubviews()
  352. }
  353. func enterSignatureMode() {
  354. self.functionTypeState = .signature
  355. self.toolBar?.isHidden = true
  356. self.formBar?.isHidden = true
  357. self.annotationBar?.isHidden = true
  358. self.digitalSignatureBar?.isHidden = false
  359. if pdfListView?.isEdited() == true {
  360. DispatchQueue.global(qos: .default).async {
  361. self.pdfListView?.commitEditing()
  362. DispatchQueue.main.async {
  363. self.pdfListView?.endOfEditing()
  364. }
  365. }
  366. } else {
  367. self.pdfListView?.endOfEditing()
  368. }
  369. if isSelctSignature {
  370. pdfListView?.setToolModel(.form)
  371. } else {
  372. pdfListView?.setToolModel(.viewer)
  373. }
  374. if digitalSignatureBar != nil {
  375. view.addSubview(digitalSignatureBar!)
  376. }
  377. if self.digitalSignatureBar?.superview != nil {
  378. self.digitalSignatureBar?.updateStatusWith(signatures: self.signatures)
  379. }
  380. navigationTitle = NSLocalizedString("Signatures", comment: "")
  381. titleButton?.setTitle(navigationTitle as String?, for: .normal)
  382. var tPosY: CGFloat = 0
  383. var tBottomY: CGFloat = 0
  384. var frame = digitalSignatureBar?.frame ?? .zero
  385. frame.origin.y = view.bounds.size.height - frame.size.height
  386. digitalSignatureBar?.frame = frame
  387. tBottomY = digitalSignatureBar?.frame.size.height ?? 0
  388. if let signtureViewControllerView = signtureViewController.view {
  389. if signtureViewControllerView.frame.height > 44.0 {
  390. tPosY = 0
  391. } else {
  392. tPosY = signtureViewControllerView.frame.height
  393. }
  394. }
  395. if CPDFKitConfig.sharedInstance().displayDirection() == .vertical {
  396. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  397. inset.bottom = 10 + tBottomY
  398. inset.top = tPosY
  399. self.pdfListView?.documentView().contentInset = inset
  400. } else {
  401. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  402. inset.bottom = 0
  403. self.pdfListView?.documentView().contentInset = inset
  404. }
  405. }
  406. override func setTitleRefresh() {
  407. if self.functionTypeState == .edit {
  408. self.enterEditMode()
  409. } else if self.functionTypeState == .viewer {
  410. self.enterViewerMode()
  411. } else if self.functionTypeState == .annotation {
  412. self.enterAnnotationMode()
  413. } else if self.functionTypeState == .form {
  414. self.enterFormMode()
  415. } else if self.functionTypeState == .signature {
  416. self.enterSignatureMode()
  417. }
  418. }
  419. func tagString() -> String {
  420. let formatter = DateFormatter()
  421. formatter.dateFormat = "YYYY-MM-ddHH:mm:ssSS"
  422. let dateString = formatter.string(from: Date())
  423. return dateString
  424. }
  425. func writeSignatureToWidget(_ widget: CPDFSignatureWidgetAnnotation, PKCS12Cert path: String, password: String, config: CPDFSignatureConfig, lockDocument isLock: Bool) {
  426. let writeDirectoryPath = SIGNATURESFOLDER
  427. if !FileManager.default.fileExists(atPath: writeDirectoryPath) {
  428. try? FileManager.default.createDirectory(at: URL(fileURLWithPath: writeDirectoryPath), withIntermediateDirectories: true)
  429. }
  430. let documentURL = self.pdfListView?.document.documentURL
  431. let documentName = documentURL?.lastPathComponent.components(separatedBy: ".").first ?? ""
  432. let writeFilePath = writeDirectoryPath + "/" + "\(documentName)_Widget_\(tagString()).pdf"
  433. if FileManager.default.fileExists(atPath: writeFilePath) {
  434. try? FileManager.default.removeItem(at: URL(fileURLWithPath: writeFilePath))
  435. }
  436. var locationStr = ""
  437. var reasonStr = "\(NSLocalizedString("none", comment: ""))"
  438. for item in config.contents {
  439. if item.key == "Reason" {
  440. reasonStr = "\(String(describing: item.value))"
  441. } else if item.key == "Location" {
  442. locationStr = item.value
  443. }
  444. }
  445. let permissions: CPDFSignaturePermissions = isLock ? .forbidChange : .none
  446. let isSuccess = self.pdfListView?.document.writeSignature(to: URL(fileURLWithPath: writeFilePath), withWidget: widget, pkcs12Cert: path, password: password, location: locationStr, reason: reasonStr, permissions: permissions) ?? false
  447. if isSuccess {
  448. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  449. if FileManager.default.fileExists(atPath: writeFilePath) {
  450. self.reloadDocument(withFilePath: writeFilePath, password: nil, completion: { result in
  451. // Handle the completion as needed
  452. })
  453. }
  454. }
  455. }
  456. }
  457. // MARK: - Action
  458. @objc override func buttonItemClicked_Bota(_ button: UIButton?) {
  459. let navArrays: [CPDFBOTATypeState] = [.CPDFBOTATypeStateOutline,
  460. .CPDFBOTATypeStateBookmark,
  461. .CPDFBOTATypeStateAnnotation]
  462. if(self.pdfListView != nil) {
  463. let botaViewController = CPDFBOTAViewController(customizeWith: self.pdfListView!, navArrays: navArrays)
  464. botaViewController.delegate = self
  465. let presentationController = AAPLCustomPresentationController.init(presentedViewController: botaViewController, presenting: self)
  466. botaViewController.transitioningDelegate = presentationController
  467. self.present(botaViewController, animated: true, completion: nil)
  468. }
  469. }
  470. @objc override func titleButtonClickd(_ button :UIButton) {
  471. let toolsArray: [NSNumber] = [NSNumber(value: CPDFToolFunctionTypeState.viewer.rawValue),
  472. NSNumber(value: CPDFToolFunctionTypeState.annotation.rawValue),
  473. NSNumber(value: CPDFToolFunctionTypeState.form.rawValue),
  474. NSNumber(value: CPDFToolFunctionTypeState.edit.rawValue),
  475. NSNumber(value: CPDFToolFunctionTypeState.signature.rawValue)]
  476. let toolsVc = CPDFToolsViewController(customizeWithToolArrays: toolsArray)
  477. toolsVc.delegate = self;
  478. let presentationController = AAPLCustomPresentationController.init(presentedViewController: toolsVc, presenting: self)
  479. toolsVc.transitioningDelegate = presentationController
  480. self.present(toolsVc, animated: true)
  481. }
  482. override func buttonItemClicked_thumbnail(_ sender: UIButton) {
  483. if self.pdfListView?.activeAnnotations?.count ?? 0 > 0 {
  484. self.pdfListView?.updateActiveAnnotations([])
  485. self.pdfListView?.setNeedsDisplayForVisiblePages()
  486. }
  487. if (self.pdfListView?.isEditing() == true && self.pdfListView?.isEdited() == true) {
  488. DispatchQueue.global(qos: .default).async {
  489. self.pdfListView?.commitEditing()
  490. DispatchQueue.main.async {
  491. self.enterThumbnail()
  492. }
  493. }
  494. } else {
  495. enterThumbnail()
  496. }
  497. }
  498. override func enterThumbnail() {
  499. if(self.pdfListView != nil) {
  500. let pageEditViewController = CPDFPageEditViewController(pdfView: self.pdfListView!)
  501. pageEditViewController.pageEditDelegate = self
  502. let navController = CNavigationController(rootViewController: pageEditViewController)
  503. navController.modalPresentationStyle = .fullScreen
  504. self.navigationController?.present(navController, animated: true, completion: nil)
  505. }
  506. }
  507. func showMenuList() {
  508. if self.pdfListView != nil {
  509. let baseVC = CPDFEditViewController(pdfView: self.pdfListView!)
  510. baseVC.editMode = self.editMode
  511. if (self.editMode == .text || self.editMode == .image) &&
  512. self.pdfListView?.editStatus() != CEditingSelectState(rawValue: 0) {
  513. let presentationController = AAPLCustomPresentationController.init(presentedViewController: baseVC, presenting: self)
  514. baseVC.transitioningDelegate = presentationController
  515. self.present(baseVC, animated: true, completion: nil)
  516. } else if .text == self.pdfListView?.shouAddEditAreaType() {
  517. baseVC.editMode = CPDFEditMode.text
  518. let presentationController = AAPLCustomPresentationController.init(presentedViewController: baseVC, presenting: self)
  519. baseVC.transitioningDelegate = presentationController
  520. self.present(baseVC, animated: true, completion: nil)
  521. }
  522. }
  523. }
  524. // MARK: - CPDFViewDelegate
  525. override func pdfViewPerformURL(_ pdfView: CPDFView, withContent content: String!) {
  526. let url = URL.init(string: content)
  527. if(url != nil) {
  528. UIApplication.shared.open(url!)
  529. }
  530. }
  531. override func pdfViewEditingSelectStateDidChanged(_ pdfView: CPDFView) {
  532. if pdfView.editingArea() is CPDFEditImageArea {
  533. self.editMode = .image
  534. } else if pdfView.editingArea() is CPDFEditTextArea {
  535. self.editMode = .text
  536. }
  537. self.toolBar?.updateButtonState()
  538. }
  539. override func pdfViewShouldBeginEditing(_ pdfView: CPDFView, textView: UITextView, for annotation: CPDFFreeTextAnnotation) {
  540. let keyboardToolbar = CPDFKeyboardToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 50))
  541. keyboardToolbar.delegate = self
  542. keyboardToolbar.bindToTextView(textView)
  543. }
  544. override func pdfViewEditingAddTextArea(_ pdfView: CPDFView, add page: CPDFPage, add rect: CGRect) {
  545. var fontColor = CPDFTextProperty.shared.fontColor
  546. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  547. fontColor?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  548. fontColor = fontColor?.withAlphaComponent(CPDFTextProperty.shared.textOpacity)
  549. var font = UIFont(name: (CPDFTextProperty.shared.fontName ?? "Helvetica-Oblique") as String, size: CPDFTextProperty.shared.fontSize)
  550. if font == nil {
  551. font = UIFont(name: "Helvetica-Oblique", size: 10)
  552. }
  553. let style = NSMutableParagraphStyle()
  554. style.alignment = CPDFTextProperty.shared.textAlignment
  555. var dic = [NSAttributedString.Key: Any]()
  556. dic[.paragraphStyle] = style
  557. dic[.foregroundColor] = fontColor
  558. dic[.font] = font
  559. self.pdfListView?.createEmptyStringBounds(rect,withAttributes: dic, page: page)
  560. }
  561. override func pdfViewEditingAddImageArea(_ pdfView: CPDFView, add page: CPDFPage, add rect: CGRect) {
  562. self.addImageRect = rect
  563. self.addImagePage = page
  564. let imagePicker = UIImagePickerController()
  565. imagePicker.sourceType = .photoLibrary
  566. imagePicker.delegate = self
  567. self.present(imagePicker, animated: true, completion: nil)
  568. }
  569. override func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView?) {
  570. let editingArea = pdfView?.editingArea()
  571. if editingArea is CPDFEditImageArea {
  572. self.editMode = .image
  573. } else if editingArea is CPDFEditTextArea {
  574. self.editMode = .text
  575. }
  576. self.toolBar?.updateButtonState()
  577. super.pdfViewCurrentPageDidChanged(self.pdfListView)
  578. }
  579. override func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  580. super.pdfViewDocumentDidLoaded(pdfView)
  581. if self.digitalSignatureBar?.superview != nil {
  582. self.digitalSignatureBar?.updateStatusWith(signatures: self.signatures)
  583. }
  584. if self.signtureViewController.view.superview != nil {
  585. verifySignature()
  586. }
  587. }
  588. // MARK: - CPDFListViewDelegate
  589. override func PDFListViewPerformTouchEnded(_ pdfListView: CPDFListView) {
  590. if CToolModel.annotation == self.pdfListView?.toolModel {
  591. if self.navigationController?.isNavigationBarHidden ?? false {
  592. self.navigationController?.setNavigationBarHidden(false, animated: true)
  593. UIView.animate(withDuration: 0.3) {
  594. var frame = self.annotationBar?.frame ?? CGRect.zero
  595. frame.origin.y = self.view.bounds.size.height - frame.size.height
  596. self.annotationBar?.frame = frame
  597. self.pdfListView?.pageSliderView?.alpha = 1.0
  598. self.annotationBar?.topToolBar?.alpha = 1.0
  599. self.annotationBar?.drawPencilFuncView?.alpha = 1.0
  600. }
  601. } else {
  602. self.navigationController?.setNavigationBarHidden(true, animated: true)
  603. UIView.animate(withDuration: 0.3) {
  604. var frame = self.annotationBar?.frame ?? CGRect.zero
  605. frame.origin.y = self.view.bounds.size.height
  606. self.annotationBar?.frame = frame
  607. self.pdfListView?.pageSliderView?.alpha = 0.0
  608. self.annotationBar?.topToolBar?.alpha = 0.0
  609. self.annotationBar?.drawPencilFuncView?.alpha = 0.0
  610. }
  611. }
  612. } else if self.digitalSignatureBar?.superview != nil {
  613. // var tTopY:CGFloat = 0.0
  614. if self.navigationController?.isNavigationBarHidden ?? false {
  615. self.navigationController?.setNavigationBarHidden(false, animated: true)
  616. UIView.animate(withDuration: 0.3) {
  617. var frame = self.digitalSignatureBar?.frame ?? CGRect.zero
  618. frame.origin.y = self.view.bounds.size.height - frame.size.height
  619. self.digitalSignatureBar?.frame = frame
  620. self.pdfListView?.pageSliderView?.alpha = 1.0
  621. self.signtureViewController.view?.alpha = 1.0
  622. }
  623. // tPosY = self.navigationController?.navigationBar.frame.maxY ?? 0
  624. if self.digitalSignatureBar?.superview != nil {
  625. // tBottomY = self.digitalSignatureBar?.frame.size.height ?? 0
  626. }
  627. if self.signtureViewController.view.superview != nil {
  628. // tTopY = self.signtureViewController.view.frame.height
  629. }
  630. } else {
  631. self.navigationController?.setNavigationBarHidden(true, animated: true)
  632. UIView.animate(withDuration: 0.3) {
  633. var frame = self.digitalSignatureBar?.frame ?? CGRect.zero
  634. frame.origin.y = self.view.bounds.size.height
  635. self.digitalSignatureBar?.frame = frame
  636. self.pdfListView?.pageSliderView?.alpha = 0.0
  637. self.signtureViewController.view?.alpha = 0.0
  638. }
  639. }
  640. } else {
  641. if self.navigationController?.isNavigationBarHidden ?? false {
  642. self.navigationController?.setNavigationBarHidden(false, animated: true)
  643. UIView.animate(withDuration: 0.3) {
  644. self.pdfListView?.pageSliderView?.alpha = 1.0
  645. }
  646. } else {
  647. self.navigationController?.setNavigationBarHidden(true, animated: true)
  648. UIView.animate(withDuration: 0.3) {
  649. self.pdfListView?.pageSliderView?.alpha = 0.0
  650. }
  651. }
  652. }
  653. if CPDFKitConfig.sharedInstance().displayDirection() == .vertical {
  654. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  655. inset.bottom = 10 + (self.annotationBar?.frame.size.height ?? 0)
  656. self.pdfListView?.documentView().contentInset = inset
  657. } else {
  658. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  659. inset.bottom = 0
  660. self.pdfListView?.documentView().contentInset = inset
  661. }
  662. }
  663. override func PDFListViewEditNote(_ pdfListView: CPDFListView, forAnnotation annotation: CPDFAnnotation) {
  664. if annotation is CPDFLinkAnnotation {
  665. self.annotationBar?.buttonItemClicked_openAnnotation(self.titleButton)
  666. } else if annotation is CPDFWidgetAnnotation {
  667. self.formBar?.buttonItemClicked_openOption(annotation as! CPDFWidgetAnnotation)
  668. } else {
  669. let rect:CGRect = self.pdfListView?.convert(annotation.bounds, from: annotation.page) ?? CGRect.zero
  670. let noteVC = CPDFNoteOpenViewController(annotation: annotation)
  671. noteVC.delegate = self
  672. noteVC.showViewController(self, inRect: rect)
  673. }
  674. }
  675. override func PDFListViewChangedAnnotationType(_ pdfListView: CPDFListView, forAnnotationMode annotationMode: Int) {
  676. if self.pdfListView?.toolModel == .annotation {
  677. self.annotationBar?.reloadData()
  678. } else if self.pdfListView?.toolModel == .form {
  679. self.formBar?.reloadData()
  680. }
  681. }
  682. override func PDFListViewPerformAddStamp(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage) {
  683. self.annotationBar?.addStampAnnotation(withPage: page, point: point)
  684. }
  685. override func PDFListViewPerformAddImage(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage) {
  686. self.annotationBar?.addImageAnnotation(withPage: page, point: point)
  687. }
  688. override func PDFListViewerTouchEndedIsAudioRecordMedia(_ pdfListView: CPDFListView) -> Bool {
  689. if CPDFMediaManager.sharedManager.mediaState == .audioRecord {
  690. self.PDFListViewPerformTouchEnded(pdfListView)
  691. return true
  692. }
  693. return false
  694. }
  695. override func PDFListViewPerformCancelMedia(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage) {
  696. CPDFMediaManager.sharedManager.mediaState = .stop
  697. }
  698. override func PDFListViewPerformRecordMedia(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage) {
  699. if self.soundPlayBar != nil && self.soundPlayBar?.superview != nil {
  700. if self.soundPlayBar?.soundState == .play {
  701. self.soundPlayBar?.stopAudioPlay()
  702. self.soundPlayBar?.removeFromSuperview()
  703. } else if self.soundPlayBar?.soundState == .record {
  704. self.soundPlayBar?.stopRecord()
  705. self.soundPlayBar?.removeFromSuperview()
  706. }
  707. }
  708. #if targetEnvironment(macCatalyst)
  709. if #available(macCatalyst 14.0, *) {
  710. let authStatus = AVCaptureDevice.authorizationStatus(for: .audio)
  711. if authStatus == .notDetermined || authStatus == .denied {
  712. AVCaptureDevice.requestAccess(for: .audio) { granted in
  713. if granted {
  714. } else {
  715. if let url = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(url) {
  716. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  717. }
  718. }
  719. }
  720. }
  721. if authStatus == .authorized {
  722. let pageIndex = self.pdfListView?.document.index(for: page)
  723. CPDFMediaManager.sharedManager.mediaState = .audioRecord
  724. CPDFMediaManager.sharedManager.pageNum = Int(pageIndex ?? 0)
  725. CPDFMediaManager.sharedManager.ptInPdf = point
  726. self.soundPlayBar = CPDFSoundPlayBar(style:self.annotationManage?.annotStyle)
  727. self.soundPlayBar?.delegate = self
  728. if(self.pdfListView != nil) {
  729. self.soundPlayBar?.show(inView: self.pdfListView!, soundState: .record)
  730. }
  731. self.soundPlayBar?.startAudioRecord()
  732. } else {
  733. return
  734. }
  735. } else {
  736. // Fallback on earlier versions
  737. }
  738. #else
  739. let authStatus = AVCaptureDevice.authorizationStatus(for: .audio)
  740. if authStatus == .notDetermined || authStatus == .denied {
  741. AVCaptureDevice.requestAccess(for: .audio) { granted in
  742. if granted {
  743. } else {
  744. if let url = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(url) {
  745. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  746. }
  747. }
  748. }
  749. }
  750. if authStatus == .authorized {
  751. let pageIndex = self.pdfListView?.document.index(for: page)
  752. CPDFMediaManager.sharedManager.mediaState = .audioRecord
  753. CPDFMediaManager.sharedManager.pageNum = Int(pageIndex ?? 0)
  754. CPDFMediaManager.sharedManager.ptInPdf = point
  755. self.soundPlayBar = CPDFSoundPlayBar(style:self.annotationManage?.annotStyle)
  756. self.soundPlayBar?.delegate = self
  757. if(self.pdfListView != nil) {
  758. self.soundPlayBar?.show(inView: self.pdfListView!, soundState: .record)
  759. }
  760. self.soundPlayBar?.startAudioRecord()
  761. } else {
  762. return
  763. }
  764. #endif
  765. }
  766. override func PDFListViewPerformPlay(_ pdfView: CPDFListView, forAnnotation annotation: CPDFSoundAnnotation) {
  767. if let filePath = annotation.mediaPath() {
  768. let URL = URL(fileURLWithPath: filePath)
  769. self.soundPlayBar = CPDFSoundPlayBar(style:self.annotationManage?.annotStyle)
  770. self.soundPlayBar?.delegate = self
  771. if(self.pdfListView != nil) {
  772. self.soundPlayBar?.show(inView: self.pdfListView!, soundState: .play)
  773. }
  774. self.soundPlayBar?.setURL(URL)
  775. self.soundPlayBar?.startAudioPlay()
  776. CPDFMediaManager.sharedManager.mediaState = .videoPlaying
  777. }
  778. }
  779. override func PDFListViewPerformSignatureWidget(_ pdfView: CPDFListView, forAnnotation annotation: CPDFSignatureWidgetAnnotation) {
  780. if self.pdfListView?.toolModel == .annotation {
  781. self.annotationBar?.openSignatureAnnotation(annotation)
  782. } else if self.pdfListView?.toolModel == .viewer {
  783. self.signatureAnnotation = annotation
  784. if let annotationSignature = annotation.signature(), let signers = annotationSignature.signers, signers.count > 0 {
  785. let vc = CPDFSigntureVerifyDetailsViewController()
  786. let nav = CNavigationController(rootViewController: vc)
  787. vc.signature = annotationSignature
  788. present(nav, animated: true, completion: nil)
  789. } else {
  790. if (digitalSignatureBar?.superview) != nil {
  791. signatureAnnotation = annotation
  792. let signatureTypeSelectView = CSignatureTypeSelectView(frame: view.frame, height: 216.0)
  793. signatureTypeSelectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  794. signatureTypeSelectView.delegate = self
  795. signatureTypeSelectView.showinView(view)
  796. } else {
  797. signatureAnnotation = annotation
  798. let signatureVC = CPDFSignatureViewController(style:nil)
  799. let presentationController = AAPLCustomPresentationController(presentedViewController: signatureVC, presenting: self)
  800. signatureVC.delegate = self
  801. signatureVC.transitioningDelegate = presentationController
  802. self.present(signatureVC, animated: true, completion: nil)
  803. }
  804. }
  805. }
  806. }
  807. override func PDFListViewEditProperties(_ pdfListView: CPDFListView, forAnnotation annotation: CPDFAnnotation) {
  808. if self.pdfListView?.toolModel == .annotation {
  809. self.annotationBar?.buttonItemClicked_openAnnotation(self.titleButton)
  810. } else if self.pdfListView?.toolModel == .form {
  811. self.formBar?.buttonItemClicked_open(nil)
  812. }
  813. }
  814. override func PDFListViewContentEditProperty(_ pdfListView: CPDFListView, point: CGPoint) {
  815. if pdfListView.editingArea() is CPDFEditImageArea {
  816. self.editMode = .image
  817. } else if pdfListView.editingArea() is CPDFEditTextArea {
  818. self.editMode = .text
  819. }
  820. self.showMenuList()
  821. self.toolBar?.updateButtonState()
  822. }
  823. // MARK: - CPDFEditToolBarDelegate
  824. func editClick(in toolBar: CPDFEditToolBar, editMode mode: Int) {
  825. self.editMode = CPDFEditMode(rawValue: UInt(mode)) ?? .text
  826. }
  827. func undoDidClick(in toolBar: CPDFEditToolBar) {
  828. self.pdfListView?.editTextUndo()
  829. }
  830. func redoDidClick(in toolBar: CPDFEditToolBar) {
  831. self.pdfListView?.editTextRedo()
  832. }
  833. func propertyEditDidClick(in toolBar: CPDFEditToolBar) {
  834. self.showMenuList()
  835. }
  836. // MARK: - CPDFPageEditViewControllerDelegate
  837. override func pageEditViewControllerDone(_ pageEditViewController: CPDFPageEditViewController) {
  838. pageEditViewController.dismiss(animated: true) {
  839. if(pageEditViewController.isPageEdit) {
  840. self.reloadDocument(withFilePath: (self.filePath)!, password: self.pdfListView?.document.password) { [weak self] result in
  841. self?.pdfListView?.reloadInputViews()
  842. self?.selectDocumentRefresh()
  843. }
  844. self.pdfListView?.reloadInputViews()
  845. }
  846. }
  847. }
  848. override func pageEditViewController(_ pageEditViewController: CPDFPageEditViewController, pageIndex: Int, isPageEdit: Bool) {
  849. pageEditViewController.dismiss(animated: true) {
  850. if isPageEdit {
  851. self.reloadDocument(withFilePath: self.filePath!, password: self.pdfListView?.document.password) { [weak self] result in
  852. self?.pdfListView?.reloadInputViews()
  853. self?.pdfListView?.go(toPageIndex: pageIndex, animated: false)
  854. }
  855. } else {
  856. self.pdfListView?.go(toPageIndex: pageIndex, animated: false)
  857. }
  858. }
  859. }
  860. // MARK: - CPDFKeyboardToolbarDelegate
  861. func keyboardShouldDissmiss(_ toolbar: CPDFKeyboardToolbar) {
  862. self.pdfListView?.commitEditAnnotationFreeText()
  863. self.pdfListView?.setAnnotationMode(.CPDFViewAnnotationModenone)
  864. self.annotationBar?.reloadData()
  865. }
  866. // MARK: - CPDFAnnotationBarDelegate
  867. func annotationBarClick(_ annotationBar: CPDFAnnotationToolBar, clickAnnotationMode annotationMode: Int, forSelected isSelected: Bool, forButton button: UIButton) {
  868. if CPDFViewAnnotationMode(rawValue: annotationMode) == .ink || CPDFViewAnnotationMode(rawValue: annotationMode) == .pencilDrawing {
  869. if isSelected {
  870. self.navigationController?.setNavigationBarHidden(true, animated: true)
  871. UIView.animate(withDuration: 0.3) {
  872. var frame = self.annotationBar?.frame ?? CGRect.zero
  873. frame.origin.y = self.view.bounds.size.height
  874. self.annotationBar?.frame = frame
  875. self.pdfListView?.pageSliderView?.alpha = 0.0
  876. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  877. inset.bottom = 0
  878. self.pdfListView?.documentView().contentInset = inset
  879. }
  880. } else {
  881. self.navigationController?.setNavigationBarHidden(false, animated: true)
  882. UIView.animate(withDuration: 0.3) {
  883. var frame = self.annotationBar?.frame ?? CGRect.zero
  884. frame.origin.y = self.view.bounds.size.height - frame.size.height
  885. self.annotationBar?.frame = frame
  886. self.pdfListView?.pageSliderView?.alpha = 1.0
  887. }
  888. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  889. inset.bottom = self.annotationBar?.frame.size.height ?? 0
  890. self.pdfListView?.documentView().contentInset = inset
  891. }
  892. } else if CPDFViewAnnotationMode(rawValue: annotationMode) == .sound && !isSelected {
  893. if self.soundPlayBar?.soundState == .record {
  894. self.soundPlayBar?.stopRecord()
  895. } else if self.soundPlayBar?.soundState == .play {
  896. self.soundPlayBar?.stopAudioPlay()
  897. }
  898. }
  899. }
  900. // MARK: - CPDFNoteOpenViewControllerDelegate
  901. func getNoteOpenViewController(_ noteOpenVC: CPDFNoteOpenViewController, content: String, isDelete: Bool) {
  902. if isDelete {
  903. if noteOpenVC.annotation is CPDFTextAnnotation {
  904. noteOpenVC.annotation?.page.removeAnnotation(noteOpenVC.annotation)
  905. self.pdfListView?.setNeedsDisplayFor(noteOpenVC.annotation?.page)
  906. if self.pdfListView?.activeAnnotations?.contains(noteOpenVC.annotation ?? CPDFAnnotation()) == true {
  907. var activeAnnotations:[CPDFAnnotation] = self.pdfListView?.activeAnnotations ?? []
  908. if let index = activeAnnotations.firstIndex(of: noteOpenVC.annotation ?? CPDFAnnotation()) {
  909. activeAnnotations.remove(at: index)
  910. self.pdfListView?.updateActiveAnnotations(activeAnnotations)
  911. }
  912. }
  913. }
  914. } else {
  915. if let markupAnnotation = noteOpenVC.annotation as? CPDFMarkupAnnotation {
  916. markupAnnotation.contents = content
  917. } else if let textAnnotation = noteOpenVC.annotation as? CPDFTextAnnotation {
  918. if !content.isEmpty {
  919. textAnnotation.contents = content
  920. } else {
  921. if self.pdfListView?.activeAnnotations?.contains(noteOpenVC.annotation ?? CPDFAnnotation()) == true {
  922. self.pdfListView?.updateActiveAnnotations([])
  923. }
  924. noteOpenVC.annotation?.page.removeAnnotation(noteOpenVC.annotation ?? CPDFAnnotation())
  925. self.pdfListView?.setNeedsDisplayFor(noteOpenVC.annotation?.page)
  926. }
  927. } else {
  928. noteOpenVC.annotation?.contents = content
  929. }
  930. }
  931. }
  932. // MARK: - CPDFNoteOpenViewControllerDelegate
  933. func soundPlayBarRecordFinished(_ soundPlayBar: CPDFSoundPlayBar, withFile filePath: String) {
  934. guard let page = self.pdfListView?.document.page(at: UInt(CPDFMediaManager.sharedManager.pageNum)) else {
  935. return
  936. }
  937. let annotation:CPDFSoundAnnotation = CPDFSoundAnnotation(document: self.pdfListView?.document)
  938. if ((annotation.setMediaPath(filePath))) {
  939. var bounds:CGRect = annotation.bounds
  940. let ptinPoint:CGPoint = CPDFMediaManager.sharedManager.ptInPdf
  941. bounds.origin.x = ptinPoint.x - bounds.size.width / 2.0
  942. bounds.origin.y = ptinPoint.y - bounds.size.height / 2.0
  943. annotation.bounds = bounds
  944. self.pdfListView?.addAnnotation(annotation, for: page)
  945. }
  946. CPDFMediaManager.sharedManager.mediaState = .stop
  947. self.pdfListView?.stopRecord()
  948. }
  949. func soundPlayBarRecordCancel(_ soundPlayBar: CPDFSoundPlayBar) {
  950. if self.soundPlayBar?.soundState == .record {
  951. self.pdfListView?.stopRecord()
  952. }
  953. CPDFMediaManager.sharedManager.mediaState = .stop
  954. }
  955. func soundPlayBarPlayClose(_ soundPlayBar: CPDFSoundPlayBar) {
  956. CPDFMediaManager.sharedManager.mediaState = .stop
  957. }
  958. // MARK: - Notification
  959. @objc func signatureHaveChangeDidChangeNotification(_ notification: Notification) {
  960. if pdfListView === notification.object as? CPDFListView {
  961. if let signatures = pdfListView?.document.signatures() {
  962. var mSignatures = [CPDFSignature]()
  963. for sign in signatures {
  964. if sign.signers.count > 0 {
  965. mSignatures.append(sign)
  966. }
  967. }
  968. self.signatures = mSignatures
  969. }
  970. }
  971. if self.digitalSignatureBar?.superview != nil {
  972. self.digitalSignatureBar?.updateStatusWith(signatures: self.signatures)
  973. }
  974. if self.signtureViewController.view.superview != nil {
  975. verifySignature()
  976. }
  977. }
  978. @objc func signatureTrustCerDidChangeNotification(_ notification: Notification) {
  979. let signatures = pdfListView?.document.signatures()
  980. if let signatures = pdfListView?.document.signatures() {
  981. var mSignatures = [CPDFSignature]()
  982. for sign in signatures {
  983. if sign.signers.count > 0 {
  984. mSignatures.append(sign)
  985. }
  986. }
  987. self.signatures = mSignatures
  988. }
  989. if self.digitalSignatureBar?.superview != nil {
  990. self.digitalSignatureBar?.updateStatusWith(signatures: self.signatures)
  991. }
  992. if self.signtureViewController.view.superview != nil {
  993. verifySignature()
  994. }
  995. if let signtureVerifyViewController = self.signtureVerifyViewController {
  996. signtureVerifyViewController.signatures = signatures ?? []
  997. signtureVerifyViewController.PDFListView = pdfListView
  998. signtureVerifyViewController.reloadData()
  999. }
  1000. }
  1001. @objc func PDFPageDidRemoveAnnotationNotification(notification :NSNotification) {
  1002. if let annotation = notification.object as? CPDFAnnotation, annotation is CPDFSoundAnnotation { self.soundPlayBar?.stopAudioPlay()
  1003. if ((self.soundPlayBar?.isDescendant(of: self.view)) != nil) {
  1004. self.soundPlayBar?.removeFromSuperview()
  1005. }
  1006. } else if let annotation = notification.object as? CPDFAnnotation,
  1007. let signatureWidgetAnnotation = annotation as? CPDFSignatureWidgetAnnotation,
  1008. let signature = signatureWidgetAnnotation.signature(),
  1009. let page = annotation.page {
  1010. pdfListView?.document.removeSignature(signature)
  1011. annotation.updateAppearanceStream()
  1012. pdfListView?.setNeedsDisplayFor(page)
  1013. NotificationCenter.default.post(name: Notification.Name(CSignatureHaveChangeDidChangeNotification.rawValue), object: pdfListView)
  1014. }
  1015. }
  1016. // MARK: - CPDFToolsViewControllerDelegate
  1017. func CPDFToolsViewControllerDismiss(_ viewController: CPDFToolsViewController, selectItemAtIndex selectIndex: CPDFToolFunctionTypeState) {
  1018. if selectIndex == .viewer {
  1019. self.enterViewerMode()
  1020. } else if selectIndex == .edit {
  1021. self.enterEditMode()
  1022. } else if selectIndex == .annotation {
  1023. self.enterAnnotationMode()
  1024. } else if selectIndex == .form {
  1025. self.formBar?.updateStatus()
  1026. self.enterFormMode()
  1027. } else if selectIndex == .signature {
  1028. self.enterSignatureMode()
  1029. }
  1030. }
  1031. // MARK: - CPDFBOTAViewControllerDelegate
  1032. override func botaViewControllerDismiss(_ botaViewController: CPDFBOTAViewController) {
  1033. self.navigationController?.dismiss(animated: true, completion: nil)
  1034. }
  1035. // MARK: - CPDFSignatureViewControllerDelegate
  1036. func signatureViewControllerDismiss(_ signatureViewController: CPDFSignatureViewController) {
  1037. self.signatureAnnotation = nil;
  1038. }
  1039. func signatureViewController(_ signatureViewController: CPDFSignatureViewController, image: UIImage) {
  1040. if (self.signatureAnnotation != nil) {
  1041. self.signatureAnnotation?.sign(with: image)
  1042. self.pdfListView?.setNeedsDisplayFor(self.signatureAnnotation?.page)
  1043. self.signatureAnnotation = nil;
  1044. }
  1045. }
  1046. // MARK: - imagePickerDelegate
  1047. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  1048. var url: URL? = nil
  1049. if #available(iOS 11.0, *) {
  1050. url = info[UIImagePickerController.InfoKey.imageURL] as? URL
  1051. } else {
  1052. url = info[UIImagePickerController.InfoKey.mediaURL] as? URL
  1053. }
  1054. var image: UIImage? = nil
  1055. if let urlPath = url?.path {
  1056. image = UIImage(contentsOfFile: urlPath)
  1057. }
  1058. var img_width: CGFloat = 0
  1059. var img_height: CGFloat = 0
  1060. var scaled_width: CGFloat = 149
  1061. var scaled_height: CGFloat = 210
  1062. if let orientation = image?.imageOrientation, orientation != .up, let originalImage = image {
  1063. UIGraphicsBeginImageContext(originalImage.size)
  1064. originalImage.draw(in: CGRect(x: 0, y: 0, width: originalImage.size.width, height: originalImage.size.height))
  1065. image = UIGraphicsGetImageFromCurrentImageContext()
  1066. UIGraphicsEndImageContext()
  1067. img_width = image?.size.height ?? 0
  1068. img_height = image?.size.width ?? 0
  1069. } else {
  1070. img_width = image?.size.width ?? 0
  1071. img_height = image?.size.height ?? 0
  1072. }
  1073. let scaled = min(scaled_width / img_width, scaled_height / img_height)
  1074. scaled_height = img_height * scaled
  1075. scaled_width = img_width * scaled
  1076. let rect = CGRect(x: self.addImageRect.origin.x, y: self.addImageRect.origin.y - scaled_height, width: scaled_width, height: scaled_height)
  1077. self.pdfListView?.createEmptyImage(rect, page: self.addImagePage, path: url?.path)
  1078. picker.dismiss(animated: true, completion: nil)
  1079. }
  1080. // MARK: - UIDocumentPickerDelegate
  1081. override func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  1082. if pkcs12DocumentPickerViewController == controller {
  1083. if let fileUrl = urls.first, fileUrl.startAccessingSecurityScopedResource() {
  1084. let fileCoordinator = NSFileCoordinator()
  1085. var error: NSError?
  1086. fileCoordinator.coordinate(readingItemAt: fileUrl, options: [], error: &error) { newURL in
  1087. let documentFolder = NSHomeDirectory() + "/Documents/Files"
  1088. if !FileManager.default.fileExists(atPath: documentFolder) {
  1089. try? FileManager.default.createDirectory(at: URL(fileURLWithPath: documentFolder), withIntermediateDirectories: true, attributes: nil)
  1090. }
  1091. let documentPath = documentFolder + "/" + newURL.lastPathComponent
  1092. if !FileManager.default.fileExists(atPath: documentPath) {
  1093. try? FileManager.default.copyItem(at: newURL, to: URL(fileURLWithPath: documentPath))
  1094. }
  1095. let url = URL(fileURLWithPath: documentPath)
  1096. let certificateViewController = CImportCertificateViewController(p12FilePath: url, annotation: self.signatureAnnotation ?? CPDFSignatureWidgetAnnotation())
  1097. certificateViewController.delegate = self
  1098. present(certificateViewController, animated: true, completion: nil)
  1099. }
  1100. fileUrl.stopAccessingSecurityScopedResource()
  1101. }
  1102. } else if documentPickerViewController == controller {
  1103. let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
  1104. if fileUrlAuthozied {
  1105. if self.pdfListView!.isEditing() == true {
  1106. DispatchQueue.global(qos: .default).async {
  1107. if self.pdfListView?.isEdited() == true {
  1108. self.pdfListView?.commitEditing()
  1109. }
  1110. DispatchQueue.main.async {
  1111. self.pdfListView?.endOfEditing()
  1112. DispatchQueue.global(qos: .default).async {
  1113. if self.pdfListView?.document.isModified() == true {
  1114. self.pdfListView?.document.write(to: self.pdfListView!.document.documentURL)
  1115. }
  1116. DispatchQueue.main.async {
  1117. self.openFile(with: urls)
  1118. if self.signtureViewController.view.superview != nil {
  1119. self.signtureViewController.view.removeFromSuperview()
  1120. }
  1121. }
  1122. }
  1123. }
  1124. }
  1125. } else {
  1126. DispatchQueue.global(qos: .default).async {
  1127. if self.pdfListView?.document.isModified() == true {
  1128. self.pdfListView?.document.write(to: self.pdfListView!.document.documentURL)
  1129. }
  1130. DispatchQueue.main.async {
  1131. self.openFile(with: urls)
  1132. if self.signtureViewController.view.superview != nil {
  1133. self.signtureViewController.view.removeFromSuperview()
  1134. }
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. }
  1141. // MARK: - CDigitalTypeSelectViewDelegate
  1142. @objc func CDigitalTypeSelectViewUse(_ digitalTypeSelectView: CDigitalTypeSelectView) {
  1143. DispatchQueue.global(qos: .default).async {
  1144. DispatchQueue.main.async {
  1145. let documentTypes = [kUTTypePKCS12 as String]
  1146. self.pkcs12DocumentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
  1147. self.pkcs12DocumentPickerViewController?.delegate = self
  1148. if self.pkcs12DocumentPickerViewController != nil {
  1149. self.present(self.pkcs12DocumentPickerViewController!, animated: true, completion: nil)
  1150. }
  1151. }
  1152. }
  1153. }
  1154. @objc func CDigitalTypeSelectViewCreate(_ digitalTypeSelectView: CDigitalTypeSelectView) {
  1155. let createCertificateViewController = CCreateCertificateInfoViewController(annotation: signatureAnnotation ?? CPDFSignatureWidgetAnnotation())
  1156. createCertificateViewController.delegate = self
  1157. self.present(createCertificateViewController, animated: true)
  1158. }
  1159. // MARK: - CImportCertificateViewControllerDelegate
  1160. func importCertificateViewControllerSave(_ importCertificateViewController: CImportCertificateViewController, PKCS12Cert path: String, password: String, config: CPDFSignatureConfig) {
  1161. self.dismiss(animated: false)
  1162. writeSignatureToWidget(self.signatureAnnotation ?? CPDFSignatureWidgetAnnotation(), PKCS12Cert: path, password: password, config: config, lockDocument: true)
  1163. }
  1164. func importCertificateViewControllerCancel(_ importCertificateViewController: CImportCertificateViewController) {
  1165. self.signatureAnnotation?.reset()
  1166. self.signatureAnnotation?.updateAppearanceStream()
  1167. self.dismiss(animated: false)
  1168. pdfListView?.setNeedsDisplayFor(self.signatureAnnotation?.page)
  1169. let signatureTypeSelectView = CSignatureTypeSelectView(frame: view.frame, height: 216.0)
  1170. signatureTypeSelectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  1171. signatureTypeSelectView.delegate = self
  1172. signatureTypeSelectView.showinView(view)
  1173. }
  1174. // MARK: - CCreateCertificateInfoViewControllerDelegate
  1175. func createCertificateInfoViewControllerCancel(_ createCertificateInfoViewController: CCreateCertificateInfoViewController) {
  1176. self.signatureAnnotation?.reset()
  1177. self.signatureAnnotation?.updateAppearanceStream()
  1178. self.dismiss(animated: false)
  1179. pdfListView?.setNeedsDisplayFor(self.signatureAnnotation?.page)
  1180. let signatureTypeSelectView = CSignatureTypeSelectView(frame: view.frame, height: 216.0)
  1181. signatureTypeSelectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  1182. signatureTypeSelectView.delegate = self
  1183. signatureTypeSelectView.showinView(view)
  1184. }
  1185. func createCertificateInfoViewControllerSave(_ createCertificateInfoViewController: CCreateCertificateInfoViewController, PKCS12Cert path: String, password: String, config: CPDFSignatureConfig) {
  1186. pdfListView?.setNeedsDisplayFor(self.signatureAnnotation?.page)
  1187. self.dismiss(animated: false)
  1188. writeSignatureToWidget(self.signatureAnnotation ?? CPDFSignatureWidgetAnnotation(), PKCS12Cert: path, password: password, config: config, lockDocument: true)
  1189. }
  1190. func verifySignature() {
  1191. let signatures = self.signatures ?? []
  1192. if signatures.count > 0 {
  1193. self.navigationController?.view.isUserInteractionEnabled = false
  1194. DispatchQueue.global(qos: .default).async {
  1195. for sign in signatures {
  1196. sign.verifySignature(with: self.pdfListView?.document)
  1197. }
  1198. DispatchQueue.main.async {
  1199. self.navigationController?.view.isUserInteractionEnabled = true
  1200. let blockSelf = self
  1201. self.signtureViewController.updateCertState(signatures)
  1202. self.signtureViewController.callback = {
  1203. let signtureVerifyViewController = CPDFSigntureVerifyViewController()
  1204. signtureVerifyViewController.delegate = blockSelf
  1205. signtureVerifyViewController.signatures = signatures
  1206. signtureVerifyViewController.PDFListView = blockSelf.pdfListView
  1207. let nav = CNavigationController(rootViewController: signtureVerifyViewController)
  1208. blockSelf.present(nav, animated: true, completion: nil)
  1209. }
  1210. if self.signtureViewController.view.superview == nil {
  1211. self.view.addSubview(self.signtureViewController.view ?? UIView())
  1212. self.view.bringSubviewToFront(self.signtureViewController.view ?? UIView())
  1213. }
  1214. let height = self.navigationController?.navigationBar.frame.maxY ?? 0
  1215. self.signtureViewController.view.frame = CGRect(x: 0, y: height, width: self.view.frame.size.width, height: 44.0)
  1216. var tPosY: CGFloat = 0
  1217. if self.signtureViewController.view.superview != nil {
  1218. tPosY = self.signtureViewController.view.frame.size.height
  1219. }
  1220. if .vertical == CPDFKitConfig.sharedInstance().displayDirection() {
  1221. var inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  1222. inset.top = tPosY
  1223. self.pdfListView?.documentView().contentInset = inset
  1224. } else {
  1225. let inset:UIEdgeInsets = self.pdfListView?.documentView().contentInset ?? UIEdgeInsets.zero
  1226. self.pdfListView?.documentView().contentInset = inset
  1227. }
  1228. }
  1229. }
  1230. } else {
  1231. if self.signtureViewController.view.superview != nil {
  1232. self.signtureViewController.view.removeFromSuperview()
  1233. }
  1234. }
  1235. }
  1236. // MARK: - CPDFSignatureViewControllerDelegate
  1237. func verifySignatureBar(_ pdfSignatureBar: CPDFDigitalSignatureToolBar, sourceButton: UIButton) {
  1238. verifySignature()
  1239. }
  1240. func addSignatureBar(_ pdfSignatureBar: CPDFDigitalSignatureToolBar, sourceButton: UIButton) {
  1241. self.isSelctSignature = sourceButton.isSelected
  1242. if self.isSelctSignature {
  1243. pdfListView?.setToolModel(.form)
  1244. pdfListView?.setAnnotationMode(.formModeSign)
  1245. } else {
  1246. pdfListView?.setToolModel(.viewer)
  1247. }
  1248. }
  1249. // MARK: - CSignatureTypeSelectViewDelegate
  1250. func signatureTypeSelectViewElectronic(_ signatureTypeSelectView: CSignatureTypeSelectView) {
  1251. let signatureVC = CPDFSignatureViewController(style:nil)
  1252. let presentationController = AAPLCustomPresentationController(presentedViewController: signatureVC, presenting: self)
  1253. signatureVC.delegate = self
  1254. signatureVC.transitioningDelegate = presentationController
  1255. self.present(signatureVC, animated: true, completion: nil)
  1256. }
  1257. func signatureTypeSelectViewDigital(_ signatureTypeSelectView: CSignatureTypeSelectView) {
  1258. let digitalTypeSelect = CDigitalTypeSelectView.loadFromNib()
  1259. digitalTypeSelect.delegate = self
  1260. digitalTypeSelect.frame = view.frame
  1261. digitalTypeSelect.show(in: view)
  1262. }
  1263. }