CPDFViewController.swift 67 KB

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