CPDFSecureViewController.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. //
  2. // CPDFSecureViewController.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 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. public class CPDFSecureViewController: UIViewController, UITextFieldDelegate {
  15. private var pdfDocument:CPDFDocument?
  16. private var filePath:String!
  17. private var password:String?
  18. private var shareBtn: UIButton?
  19. private var scrollView: UIScrollView?
  20. private var openPasswordLabel:UILabel?
  21. private var openPasswordSwitch:UISwitch?
  22. private var openPasswordField:UITextField?
  23. private var line1:UIView?
  24. private var owerPasswordLabel:UILabel?
  25. private var owerPasswordSwitch:UISwitch?
  26. private var owerPasswordField:UITextField?
  27. private var line2:UIView?
  28. private var line3:UIView?
  29. private var printBtn:UIButton?
  30. private var copyBtn:UIButton?
  31. private var printViewBtn:UIButton?
  32. private var copyViewBtn:UIButton?
  33. private var printLabel:UILabel?
  34. private var copyLabel:UILabel?
  35. private var levelLabel:UILabel?
  36. private var levelSubLabel:UILabel?
  37. private var levelImage:UIImageView?
  38. private var levelBtn:UIButton?
  39. private var contentView1:UIView?
  40. private var contentView2:UIView?
  41. private var levelType:CPDFDocumentEncryptionLevel = .noEncryptAlgo
  42. private var isHaveOpenPassword = false
  43. public init(filePath: String,password:String?) {
  44. super.init(nibName: nil, bundle: nil)
  45. self.filePath = filePath
  46. self.password = password;
  47. }
  48. required init?(coder: NSCoder) {
  49. fatalError("init(coder:) has not been implemented")
  50. }
  51. public override func viewDidLoad() {
  52. super.viewDidLoad()
  53. self.title = NSLocalizedString("Security Settings", comment: "");
  54. shareBtn = UIButton()
  55. shareBtn?.setTitle(NSLocalizedString("Share", comment: ""), for: .normal)
  56. shareBtn?.addTarget(self, action: #selector(buttonItemClick_Share(_:)), for: .touchUpInside)
  57. shareBtn?.sizeToFit()
  58. if shareBtn != nil {
  59. let shareBarItem = UIBarButtonItem(customView: shareBtn!)
  60. self.navigationItem.rightBarButtonItems = [shareBarItem]
  61. }
  62. let backItem = UIBarButtonItem(image: UIImage(named: "CPDFViewImageBack", in: Bundle(for: self.classForCoder), compatibleWith: nil), style: .plain, target: self, action: #selector(buttonItemClicked_back(_:)))
  63. self.navigationItem.leftBarButtonItems = [backItem]
  64. let document = CPDFDocument(url: NSURL.fileURL(withPath: self.filePath))
  65. // have open PassWord or have open+ower
  66. if(document?.isLocked == true) {
  67. isHaveOpenPassword = true
  68. }
  69. if((self.password?.count ?? 0) > 0) {
  70. if ((document?.unlock(withPassword: self.password)) == false) {
  71. dismiss(animated: true)
  72. return
  73. }
  74. }
  75. pdfDocument = document
  76. configUI()
  77. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  78. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  79. refreshShareButton()
  80. }
  81. public override func viewWillLayoutSubviews() {
  82. super.viewWillLayoutSubviews()
  83. scrollView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
  84. var offstX = 10
  85. if #available(iOS 11.0, *) {
  86. offstX += Int(self.view.safeAreaInsets.left)
  87. }
  88. var offstR = 10
  89. if #available(iOS 11.0, *) {
  90. offstR += Int(self.view.safeAreaInsets.right)
  91. }
  92. var offstY = 0
  93. openPasswordSwitch?.frame = CGRect(x: Int((scrollView?.frame.size.width ?? 0)) - Int(openPasswordSwitch?.width ?? 0) - offstR, y: offstY+7, width: Int(openPasswordSwitch?.width ?? 0), height: 30)
  94. let w = Int(scrollView?.frame.size.width ?? 0) - offstX - Int(openPasswordSwitch?.frame.size.width ?? 0) - offstR - 5
  95. openPasswordLabel?.frame = CGRect(x: offstX, y: offstY, width:w, height: 20)
  96. openPasswordLabel?.centerY = openPasswordSwitch?.centerY ?? 0
  97. offstY = Int((openPasswordSwitch?.frame.maxY ?? 0) + 7)
  98. line1?.frame = CGRect(x: offstX, y: offstY, width: Int(scrollView?.frame.size.width ?? 0) - offstX - offstR, height: 1)
  99. offstY += 1
  100. let w1 = Int(self.scrollView?.frame.width ?? 0) - offstX - offstR
  101. openPasswordField?.frame = CGRect(x: offstX, y: offstY+12, width: w1, height: 25)
  102. offstY = Int(((openPasswordField?.frame.maxY ?? 0) + 12))
  103. contentView1?.frame = CGRect(x: 0, y: offstY, width: Int(scrollView?.frame.size.width ?? 0), height: 20)
  104. offstY += 20
  105. owerPasswordSwitch?.frame = CGRect(x: Int(scrollView?.frame.size.width ?? 0) - Int(owerPasswordSwitch?.width ?? 0) - offstR, y: offstY+7, width: Int(owerPasswordSwitch?.width ?? 0), height: 30)
  106. owerPasswordLabel?.frame = CGRect(x: offstX, y: offstY, width:Int(scrollView?.frame.size.width ?? 0) - offstX - Int(openPasswordSwitch?.frame.size.width ?? 0) - 5 - offstR, height: 20)
  107. owerPasswordLabel?.centerY = owerPasswordSwitch?.centerY ?? 0
  108. offstY = Int(((owerPasswordSwitch?.frame.maxY ?? 0) + 7))
  109. line2?.frame = CGRect(x: offstX, y: offstY, width: Int(scrollView?.frame.size.width ?? 0) - offstX - offstR, height: 1)
  110. offstY += 1
  111. let w2 = Int(self.scrollView?.frame.width ?? 0) - offstX - offstR
  112. owerPasswordField?.frame = CGRect(x: offstX, y: offstY+12, width: w2, height: 20)
  113. offstY = Int(((owerPasswordField?.frame.maxY ?? 0) + 12))
  114. line3?.frame = CGRect(x: offstX, y: offstY, width: Int((scrollView?.frame.size.width ?? 0)) - offstX - offstR, height: 1)
  115. offstY += 1
  116. printBtn?.frame = CGRect(x: offstX, y: offstY+12, width: 20, height: 20)
  117. printLabel?.frame = CGRect(x: (printBtn?.frame.maxX ?? 0) + 5, y: CGFloat(offstY + 12), width: self.view.width - 35, height: 20)
  118. printViewBtn?.frame = CGRect(x:0, y: CGFloat(offstY), width: self.scrollView?.width ?? 0, height:44)
  119. offstY = Int(((printBtn?.frame.maxY ?? 0) + 12))
  120. copyBtn?.frame = CGRect(x: offstX, y: offstY+12, width: 20, height: 20)
  121. copyLabel?.frame = CGRect(x: (copyBtn?.frame.maxX ?? 0) + 5, y: CGFloat(offstY + 12), width: self.view.width - 35, height: 20)
  122. copyViewBtn?.frame = CGRect(x:0, y:CGFloat(offstY), width: self.scrollView?.width ?? 0, height:44)
  123. offstY = Int(((copyBtn?.frame.maxY ?? 0) + 12))
  124. contentView2?.frame = CGRect(x: 0, y: offstY, width: Int(scrollView?.frame.size.width ?? 0), height:20)
  125. offstY += 20
  126. levelImage?.frame = CGRect(x: Int(self.scrollView?.width ?? 0) - 20 - offstR, y: offstY + 12, width: 20, height: 20)
  127. levelSubLabel?.frame = CGRect(x: Int(self.scrollView?.width ?? 0) - Int(levelImage?.frame.width ?? 0) - 5 - offstR - 120, y: offstY + 12, width: 120, height: 20)
  128. levelLabel?.frame = CGRect(x: offstX, y: offstY+12, width: Int((self.scrollView?.width ?? 0) - (levelSubLabel?.frame.width ?? 0) - 5) - offstR - 20, height: 20)
  129. levelBtn?.frame = CGRect(x: 0, y: CGFloat(offstY), width: self.scrollView?.width ?? 0, height: 44.0)
  130. offstY = Int(((levelBtn?.frame.maxY ?? 0) + 10))
  131. scrollView?.contentSize = CGSize(width: scrollView?.frame.width ?? 0, height: CGFloat(offstY))
  132. }
  133. // MARK: - Action
  134. @objc func buttonItemClick_Share(_ button: UIBarButtonItem) {
  135. let openPassWord = self.openPasswordField?.text
  136. let owerPassWord = self.owerPasswordField?.text
  137. if self.owerPasswordSwitch?.isOn == true && self.openPasswordSwitch?.isOn == true && (openPassWord?.count ?? 0) > 0 && (owerPassWord?.count ?? 0) > 0 && openPassWord == owerPassWord {
  138. errMsgTip(NSLocalizedString("The password can't be empty.", comment: ""))
  139. return
  140. }
  141. if self.openPasswordSwitch?.isOn == true && openPassWord?.count == 0 {
  142. errMsgTip(NSLocalizedString("The password can't be empty.", comment: ""))
  143. return
  144. }
  145. if self.owerPasswordSwitch?.isOn == true && owerPassWord?.count == 0 {
  146. errMsgTip(NSLocalizedString("The password can't be empty.", comment: ""))
  147. return
  148. }
  149. if (!(FileManager.default.fileExists(atPath: TEMPOARTFOLDER))) {
  150. try? FileManager.default.createDirectory(atPath: TEMPOARTFOLDER, withIntermediateDirectories: true, attributes: nil)
  151. }
  152. guard let lastPathComponent = self.pdfDocument?.documentURL.deletingPathExtension().lastPathComponent else { return }
  153. var isSuccess = false
  154. var secPath = ""
  155. if(self.openPasswordSwitch?.isOn == false && self.owerPasswordSwitch?.isOn == false) {
  156. secPath = TEMPOARTFOLDER + "/" + lastPathComponent + "_Password_Removed.pdf"
  157. do {
  158. try FileManager.default.removeItem(atPath: secPath)
  159. } catch {
  160. // Handle the error, e.g., print an error message or perform other actions
  161. }
  162. isSuccess = ((pdfDocument?.writeDecrypt(to: NSURL(fileURLWithPath: secPath) as URL)) == true)
  163. } else {
  164. secPath = TEMPOARTFOLDER + "/" + lastPathComponent + "_Encrypted.pdf"
  165. do {
  166. try FileManager.default.removeItem(atPath: secPath)
  167. } catch {
  168. // Handle the error, e.g., print an error message or perform other actions
  169. }
  170. var options:[CPDFDocumentWriteOption: Any] = [:]
  171. if(openPassWord?.count ?? 0 > 0) {
  172. options[CPDFDocumentWriteOption.userPasswordOption] = openPassWord
  173. }
  174. if(owerPassWord?.count ?? 0 > 0) {
  175. options[CPDFDocumentWriteOption.ownerPasswordOption] = owerPassWord
  176. }
  177. if self.printBtn?.isSelected == true {
  178. options[CPDFDocumentWriteOption.allowsPrintingOption] = false
  179. } else {
  180. options[CPDFDocumentWriteOption.allowsPrintingOption] = true
  181. }
  182. if self.copyBtn?.isSelected == true {
  183. options[CPDFDocumentWriteOption.allowsCopyingOption] = false
  184. } else {
  185. options[CPDFDocumentWriteOption.allowsCopyingOption] = true
  186. }
  187. options[CPDFDocumentWriteOption.encryptionLevelOption] = NSNumber(value: self.levelType.rawValue)
  188. isSuccess = (pdfDocument?.write(to: NSURL(fileURLWithPath: secPath) as URL, withOptions: options) == true)
  189. }
  190. if isSuccess == true {
  191. self.shareAction(url: NSURL(fileURLWithPath: secPath) as URL)
  192. }
  193. }
  194. func shareAction(url: URL?) {
  195. if (url != nil) {
  196. let activityVC = UIActivityViewController(activityItems: [url!], applicationActivities: nil)
  197. activityVC.definesPresentationContext = true
  198. if UI_USER_INTERFACE_IDIOM() == .pad {
  199. activityVC.popoverPresentationController?.sourceView = self.navigationController?.navigationBar ?? self.view
  200. activityVC.popoverPresentationController?.sourceRect = CGRect(x:self.navigationController?.navigationBar.width ?? 0, y:0, width: 0, height: 0)
  201. }
  202. self.present(activityVC, animated: true) {
  203. self.navigationController?.popViewController(animated: false)
  204. }
  205. activityVC.completionWithItemsHandler = { (activityType, completed, returnedItems, activityError) in
  206. if completed {
  207. print("Success!")
  208. } else {
  209. print("Failed Or Canceled!")
  210. }
  211. }
  212. }
  213. }
  214. func refreshShareButton() {
  215. let openPassWord = self.openPasswordField?.text ?? ""
  216. let owerPassWord = self.owerPasswordField?.text ?? ""
  217. if self.owerPasswordSwitch?.isOn == true && self.openPasswordSwitch?.isOn == true && openPassWord.count > 0 && owerPassWord.count > 0 && openPassWord == owerPassWord {
  218. shareBtn?.setTitleColor(.gray, for: .normal)
  219. return
  220. } else {
  221. shareBtn?.setTitleColor(UIColor(red: 20.0/255.0, green: 96.0/255.0, blue: 243.0/255.0, alpha: 1.0), for: .normal)
  222. }
  223. if self.openPasswordSwitch?.isOn == true && openPassWord.count == 0 {
  224. shareBtn?.setTitleColor(.gray, for: .normal)
  225. return
  226. } else {
  227. shareBtn?.setTitleColor(UIColor(red: 20.0/255.0, green: 96.0/255.0, blue: 243.0/255.0, alpha: 1.0), for: .normal)
  228. }
  229. if self.owerPasswordSwitch?.isOn == true && owerPassWord.count == 0 {
  230. shareBtn?.setTitleColor(.gray, for: .normal)
  231. return
  232. } else {
  233. shareBtn?.setTitleColor(UIColor(red: 20.0/255.0, green: 96.0/255.0, blue: 243.0/255.0, alpha: 1.0), for: .normal)
  234. }
  235. }
  236. @objc func buttonItemClicked_back(_ button: UIBarButtonItem) {
  237. self.navigationController?.popViewController(animated: true)
  238. }
  239. @objc func buttonItemClicked_openPassword(_ button: UISwitch) {
  240. if (openPasswordSwitch?.isOn == true) {
  241. openPasswordField?.isEnabled = true
  242. } else {
  243. openPasswordField?.text = ""
  244. openPasswordField?.isEnabled = false
  245. }
  246. hiddenLevel()
  247. refreshShareButton()
  248. }
  249. @objc func buttonItemClicked_owerPassword(_ button: UISwitch) {
  250. if (owerPasswordSwitch?.isOn == true) {
  251. printViewBtn?.isEnabled = true
  252. copyViewBtn?.isEnabled = true
  253. owerPasswordField?.isEnabled = true
  254. if #available(iOS 13.0, *) {
  255. printLabel?.textColor = UIColor.label
  256. copyLabel?.textColor = UIColor.label
  257. } else {
  258. printLabel?.textColor = UIColor.black
  259. copyLabel?.textColor = UIColor.black
  260. }
  261. } else {
  262. printViewBtn?.isEnabled = false
  263. copyViewBtn?.isEnabled = false
  264. owerPasswordField?.text = ""
  265. owerPasswordField?.isEnabled = false
  266. if #available(iOS 13.0, *) {
  267. printLabel?.textColor = UIColor.secondaryLabel
  268. copyLabel?.textColor = UIColor.secondaryLabel
  269. } else {
  270. printLabel?.textColor = UIColor.init(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1.0)
  271. copyLabel?.textColor = UIColor.init(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1.0)
  272. }
  273. }
  274. hiddenLevel()
  275. refreshShareButton()
  276. }
  277. @objc func buttonItemClicked_print(_ button: UIButton) {
  278. if printBtn?.isSelected == true {
  279. printBtn?.isSelected = false
  280. } else {
  281. printBtn?.isSelected = true
  282. }
  283. }
  284. @objc func buttonItemClicked_copy(_ button: UIButton) {
  285. if copyBtn?.isSelected == true {
  286. copyBtn?.isSelected = false
  287. } else {
  288. copyBtn?.isSelected = true
  289. }
  290. }
  291. @objc func buttonItemClicked_showOwerPassword(_ button: UIButton) {
  292. if button.isSelected == true {
  293. button.isSelected = false
  294. owerPasswordField?.isSecureTextEntry = true
  295. } else {
  296. button.isSelected = true
  297. owerPasswordField?.isSecureTextEntry = false
  298. }
  299. }
  300. @objc func buttonItemClicked_showOpenPassword(_ button: UIButton) {
  301. if button.isSelected == true {
  302. button.isSelected = false
  303. openPasswordField?.isSecureTextEntry = true
  304. } else {
  305. button.isSelected = true
  306. openPasswordField?.isSecureTextEntry = false
  307. }
  308. }
  309. @objc func buttonItemClicked_level(_ button: UIButton) {
  310. self.view.endEditing(true)
  311. let actionSheetController = UIAlertController(title: nil, message: NSLocalizedString("Encryption Level", comment: ""), preferredStyle: .alert)
  312. // Cancel
  313. let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel) { (action) in
  314. }
  315. // 128-bit RC4
  316. let RC4Action = UIAlertAction(title: NSLocalizedString("128-bit RC4", comment: ""), style: .destructive) { (action) in
  317. self.levelSubLabel?.text = NSLocalizedString("128-bit RC4", comment: "")
  318. self.levelType = .RC4
  319. }
  320. // 128-bit AES
  321. let AES128Action = UIAlertAction(title: NSLocalizedString("128-bit AES" , comment: ""), style: .destructive) { (action) in
  322. self.levelSubLabel?.text = NSLocalizedString("128-bit AES", comment: "")
  323. self.levelType = .AES128
  324. }
  325. // 256-bit AES
  326. let AES256Action = UIAlertAction(title: NSLocalizedString("256-bit AES" , comment: ""), style: .destructive) { (action) in
  327. self.levelSubLabel?.text = NSLocalizedString("256-bit AES", comment: "")
  328. self.levelType = .AES256
  329. }
  330. actionSheetController.addAction(cancelAction)
  331. actionSheetController.addAction(RC4Action)
  332. actionSheetController.addAction(AES128Action)
  333. actionSheetController.addAction(AES256Action)
  334. if UIDevice.current.userInterfaceIdiom == .pad {
  335. actionSheetController.popoverPresentationController?.sourceView = button
  336. actionSheetController.popoverPresentationController?.sourceRect = button.bounds
  337. }
  338. present(actionSheetController, animated: true, completion: nil)
  339. }
  340. func errMsgTip(_ msg: String) {
  341. let alvc = UIAlertController(title: msg, message: "", preferredStyle: .alert)
  342. let okAction = UIAlertAction(title: NSLocalizedString("OK",comment: ""), style: .destructive) { (action) in
  343. // Add your action code here
  344. }
  345. alvc.addAction(okAction)
  346. present(alvc, animated: true, completion: nil)
  347. }
  348. private func configUI() {
  349. scrollView = UIScrollView()
  350. scrollView?.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  351. scrollView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
  352. scrollView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  353. if scrollView != nil {
  354. view.addSubview(scrollView!)
  355. }
  356. openPasswordLabel = UILabel()
  357. openPasswordLabel?.font = UIFont.systemFont(ofSize: 15.0)
  358. openPasswordLabel?.text = NSLocalizedString("Password to Open the Document", comment: "")
  359. openPasswordLabel?.adjustsFontSizeToFitWidth = true
  360. if openPasswordLabel != nil {
  361. scrollView?.addSubview(openPasswordLabel!)
  362. }
  363. openPasswordSwitch = UISwitch()
  364. openPasswordSwitch?.addTarget(self, action: #selector(buttonItemClicked_openPassword(_:)), for: .valueChanged)
  365. if openPasswordSwitch != nil {
  366. scrollView?.addSubview(openPasswordSwitch!)
  367. }
  368. line1 = UIView(frame: CGRect.zero);
  369. line1?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.12)
  370. if line1 != nil {
  371. scrollView?.addSubview(line1!)
  372. }
  373. openPasswordField = UITextField()
  374. openPasswordField?.placeholder = NSLocalizedString("Please Enter the Password", comment: "")
  375. openPasswordField?.addTarget(self, action: #selector(textFieldTextChange(_:)), for: .editingChanged)
  376. openPasswordField?.borderStyle = .none
  377. openPasswordField?.textAlignment = .left
  378. openPasswordField?.isSecureTextEntry = true
  379. openPasswordField?.delegate = self
  380. openPasswordField?.font = UIFont.systemFont(ofSize: 13)
  381. openPasswordField?.returnKeyType = .done
  382. openPasswordField?.rightViewMode = .always
  383. if openPasswordField != nil {
  384. scrollView?.addSubview(openPasswordField!)
  385. }
  386. let openPasswordBtton = UIButton(type: .custom)
  387. openPasswordBtton.addTarget(self, action: #selector(buttonItemClicked_showOpenPassword(_:)), for: .touchUpInside)
  388. openPasswordBtton.setImage(UIImage(named: "CSecureImageInvisible", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  389. openPasswordBtton.setImage(UIImage(named: "CSecureImageVisible", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .selected)
  390. openPasswordBtton.frame = CGRect(x:0, y:0, width: 25, height: 25)
  391. openPasswordField?.rightView = openPasswordBtton
  392. contentView1 = UIView(frame: CGRect.zero);
  393. if contentView1 != nil {
  394. scrollView?.addSubview(contentView1!)
  395. }
  396. contentView1?.backgroundColor = CPDFColorUtils.CContentBackgroundColor()
  397. owerPasswordLabel = UILabel()
  398. openPasswordLabel?.font = UIFont.systemFont(ofSize: 15.0)
  399. owerPasswordLabel?.text = NSLocalizedString("Owner Password", comment: "")
  400. owerPasswordLabel?.adjustsFontSizeToFitWidth = true
  401. if owerPasswordLabel != nil {
  402. scrollView?.addSubview(owerPasswordLabel!)
  403. }
  404. owerPasswordSwitch = UISwitch()
  405. owerPasswordSwitch?.addTarget(self, action: #selector(buttonItemClicked_owerPassword(_:)), for: .valueChanged)
  406. if owerPasswordSwitch != nil {
  407. scrollView?.addSubview(owerPasswordSwitch!)
  408. }
  409. line2 = UIView(frame: CGRect.zero);
  410. line2?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.12)
  411. if line2 != nil {
  412. scrollView?.addSubview(line2!)
  413. }
  414. owerPasswordField = UITextField()
  415. owerPasswordField?.placeholder = NSLocalizedString("Please enter the owner's password", comment: "")
  416. owerPasswordField?.addTarget(self, action: #selector(textFieldTextChange(_:)), for: .editingChanged)
  417. owerPasswordField?.borderStyle = .none
  418. owerPasswordField?.textAlignment = .left
  419. owerPasswordField?.isSecureTextEntry = true
  420. owerPasswordField?.delegate = self
  421. owerPasswordField?.font = UIFont.systemFont(ofSize: 11)
  422. owerPasswordField?.returnKeyType = .done
  423. owerPasswordField?.rightViewMode = .always
  424. if owerPasswordField != nil {
  425. scrollView?.addSubview(owerPasswordField!)
  426. }
  427. let owerPasswordBtton = UIButton(type: .custom)
  428. owerPasswordBtton.addTarget(self, action: #selector(buttonItemClicked_showOwerPassword(_:)), for: .touchUpInside)
  429. owerPasswordBtton.setImage(UIImage(named: "CSecureImageInvisible", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  430. owerPasswordBtton.setImage(UIImage(named: "CSecureImageVisible", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .selected)
  431. owerPasswordBtton.frame = CGRect(x:0, y:0, width:25, height:25)
  432. owerPasswordField?.rightView = owerPasswordBtton
  433. line3 = UIView(frame: CGRect.zero);
  434. line3?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.12)
  435. if line3 != nil {
  436. scrollView?.addSubview(line3!)
  437. }
  438. printBtn = UIButton(type: .custom)
  439. printBtn?.setImage(UIImage(named: "CAddSignatureCellNoSelect", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  440. printBtn?.setImage(UIImage(named: "CAddSignatureCellSelect", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .selected)
  441. if printBtn != nil {
  442. scrollView?.addSubview(printBtn!)
  443. }
  444. printViewBtn = UIButton(type: .custom)
  445. printViewBtn?.addTarget(self, action: #selector(buttonItemClicked_print(_:)), for: .touchUpInside)
  446. if printViewBtn != nil {
  447. scrollView?.addSubview(printViewBtn!)
  448. }
  449. printLabel = UILabel()
  450. printLabel?.font = UIFont.systemFont(ofSize: 13.0)
  451. printLabel?.text = NSLocalizedString("Restrict document printing", comment: "")
  452. printLabel?.adjustsFontSizeToFitWidth = true
  453. if printLabel != nil {
  454. scrollView?.addSubview(printLabel!)
  455. }
  456. copyBtn = UIButton(type: .custom)
  457. copyBtn?.setImage(UIImage(named: "CAddSignatureCellNoSelect", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  458. copyBtn?.setImage(UIImage(named: "CAddSignatureCellSelect", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .selected)
  459. if copyBtn != nil {
  460. scrollView?.addSubview(copyBtn!)
  461. }
  462. copyViewBtn = UIButton(type: .custom)
  463. copyViewBtn?.addTarget(self, action: #selector(buttonItemClicked_copy(_:)), for: .touchUpInside)
  464. if copyViewBtn != nil {
  465. scrollView?.addSubview(copyViewBtn!)
  466. }
  467. copyLabel = UILabel()
  468. copyLabel?.font = UIFont.systemFont(ofSize: 13.0)
  469. copyLabel?.text = NSLocalizedString("Restrict content copying", comment: "")
  470. copyLabel?.adjustsFontSizeToFitWidth = true
  471. if copyLabel != nil {
  472. scrollView?.addSubview(copyLabel!)
  473. }
  474. contentView2 = UIView(frame: CGRect.zero);
  475. if contentView2 != nil {
  476. scrollView?.addSubview(contentView2!)
  477. }
  478. contentView2?.backgroundColor = CPDFColorUtils.CContentBackgroundColor()
  479. levelLabel = UILabel()
  480. levelLabel?.font = UIFont.systemFont(ofSize: 13.0)
  481. levelLabel?.text = NSLocalizedString("Encryption Level", comment: "")
  482. levelLabel?.adjustsFontSizeToFitWidth = true
  483. if levelLabel != nil {
  484. scrollView?.addSubview(levelLabel!)
  485. }
  486. levelSubLabel = UILabel()
  487. levelSubLabel?.font = UIFont.systemFont(ofSize: 13.0)
  488. levelSubLabel?.text = NSLocalizedString("128 - bit RC4", comment: "")
  489. levelSubLabel?.adjustsFontSizeToFitWidth = true
  490. levelSubLabel?.textAlignment = .right
  491. if levelSubLabel != nil {
  492. scrollView?.addSubview(levelSubLabel!)
  493. }
  494. levelImage = UIImageView()
  495. levelImage?.image = UIImage(named: "CSecureImageGo", in: Bundle(for: self.classForCoder), compatibleWith: nil)
  496. if levelImage != nil {
  497. scrollView?.addSubview(levelImage!)
  498. }
  499. levelBtn = UIButton(type: .custom)
  500. levelBtn?.addTarget(self, action: #selector(buttonItemClicked_level(_:)), for: .touchUpInside)
  501. if levelBtn != nil {
  502. scrollView?.addSubview(levelBtn!)
  503. }
  504. if (self.pdfDocument?.password != nil) {
  505. if(self.isHaveOpenPassword) {
  506. openPasswordSwitch?.setOn(true, animated: true)
  507. openPasswordField?.isEnabled = true
  508. } else {
  509. openPasswordSwitch?.setOn(false, animated: true)
  510. openPasswordField?.isEnabled = false
  511. }
  512. if (self.pdfDocument?.checkOwnerPassword(self.pdfDocument?.password!) == true) {
  513. owerPasswordSwitch?.setOn(true, animated: true)
  514. printViewBtn?.isEnabled = true
  515. copyViewBtn?.isEnabled = true
  516. owerPasswordField?.isEnabled = true
  517. if #available(iOS 13.0, *) {
  518. printLabel?.textColor = UIColor.label
  519. copyLabel?.textColor = UIColor.label
  520. } else {
  521. printLabel?.textColor = UIColor.black
  522. copyLabel?.textColor = UIColor.black
  523. }
  524. self.owerPasswordField?.text = self.pdfDocument?.password
  525. } else {
  526. owerPasswordSwitch?.setOn(false, animated: true)
  527. printViewBtn?.isEnabled = false
  528. copyViewBtn?.isEnabled = false
  529. owerPasswordField?.isEnabled = false
  530. if #available(iOS 13.0, *) {
  531. printLabel?.textColor = UIColor.label
  532. copyLabel?.textColor = UIColor.label
  533. } else {
  534. printLabel?.textColor = UIColor.init(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1.0)
  535. copyLabel?.textColor = UIColor.init(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1.0)
  536. }
  537. if(isHaveOpenPassword) {
  538. self.openPasswordField?.text = self.pdfDocument?.password
  539. }
  540. }
  541. } else {
  542. openPasswordSwitch?.setOn(false, animated: true)
  543. owerPasswordSwitch?.setOn(false, animated: true)
  544. printViewBtn?.isEnabled = false
  545. copyViewBtn?.isEnabled = false
  546. openPasswordField?.isEnabled = false
  547. owerPasswordField?.isEnabled = false
  548. if #available(iOS 13.0, *) {
  549. printLabel?.textColor = UIColor.secondaryLabel
  550. copyLabel?.textColor = UIColor.secondaryLabel
  551. } else {
  552. printLabel?.textColor = UIColor.init(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1.0)
  553. copyLabel?.textColor = UIColor.init(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1.0)
  554. }
  555. }
  556. if self.pdfDocument?.allowsPrinting == false {
  557. printBtn?.isSelected = true
  558. } else {
  559. printBtn?.isSelected = false
  560. }
  561. if self.pdfDocument?.allowsCopying == false {
  562. copyBtn?.isSelected = true
  563. } else {
  564. copyBtn?.isSelected = false
  565. }
  566. switch self.pdfDocument?.encryptionLevel {
  567. case .AES128:
  568. self.levelSubLabel?.text = NSLocalizedString("128-bit AES", comment: "")
  569. self.levelType = .AES128
  570. case .AES256:
  571. self.levelSubLabel?.text = NSLocalizedString("256-bit AES", comment: "")
  572. self.levelType = .AES256
  573. case .RC4:
  574. self.levelSubLabel?.text = NSLocalizedString("128-bit RC4", comment: "")
  575. self.levelType = .RC4
  576. default:
  577. self.levelSubLabel?.text = NSLocalizedString("No Encrypt Algo", comment: "")
  578. self.levelType = .noEncryptAlgo
  579. }
  580. hiddenLevel()
  581. }
  582. func hiddenLevel() {
  583. if openPasswordSwitch?.isOn == false && owerPasswordSwitch?.isOn == false {
  584. levelBtn?.isHidden = true
  585. levelLabel?.isHidden = true
  586. levelSubLabel?.isHidden = true
  587. levelImage?.isHidden = true
  588. } else {
  589. levelBtn?.isHidden = false
  590. levelLabel?.isHidden = false
  591. levelSubLabel?.isHidden = false
  592. levelImage?.isHidden = false
  593. }
  594. }
  595. // MARK: - UITextFieldDelegate
  596. public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  597. textField.resignFirstResponder()
  598. return true
  599. }
  600. @objc func textFieldTextChange(_ textField: UITextField) {
  601. refreshShareButton()
  602. }
  603. // MARK: - NSNotification
  604. @objc func keyboardWillChangeFrame(_ notification: Notification) {
  605. if let userInfo = notification.userInfo {
  606. if let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  607. let textFied:UITextField?
  608. if self.openPasswordField?.isFirstResponder == true {
  609. textFied = self.openPasswordField
  610. } else {
  611. textFied = self.owerPasswordField
  612. }
  613. let frame = value.cgRectValue
  614. let rect = textFied?.frame ?? CGRect.zero
  615. if rect.maxY > self.view.frame.size.height - frame.size.height {
  616. var insets = self.scrollView?.contentInset
  617. insets?.bottom = frame.size.height + (self.owerPasswordField?.frame.size.height ?? 0)
  618. self.scrollView?.contentInset = insets ?? UIEdgeInsets.zero
  619. }
  620. }
  621. }
  622. }
  623. @objc func keyboardWillHide(_ notification: Notification) {
  624. var insets = self.scrollView?.contentInset
  625. insets?.bottom = 0
  626. self.scrollView?.contentInset = insets ?? UIEdgeInsets.zero
  627. }
  628. }