CPDFInfoViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. // CPDFInfoViewController.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. class CPDFInfoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  15. var currentPath: String?
  16. weak var pdfView: CPDFView?
  17. // MARK: - Accessors
  18. private lazy var curTableView: UITableView! = {
  19. let curTableView = UITableView.init(frame: CGRect.zero, style: .grouped)
  20. curTableView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  21. curTableView.delegate = self
  22. curTableView.dataSource = self
  23. curTableView.separatorStyle = .singleLine
  24. curTableView.backgroundColor = UIColor.clear
  25. return curTableView
  26. } ()
  27. private var curTableArray = [[[String: Any]]]()
  28. private var titleLabel: UILabel!
  29. private var doneBtn: UIButton!
  30. // MARK: - Initializers
  31. init(pdfView: CPDFView) {
  32. doneBtn = UIButton(type: .system)
  33. super.init(nibName: nil, bundle: nil)
  34. self.pdfView = pdfView
  35. }
  36. required init?(coder aDecoder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. if #available(iOS 12.0, *) {
  42. let isDark = (self.traitCollection.userInterfaceStyle == .dark)
  43. if isDark {
  44. self.view.backgroundColor = .black
  45. } else {
  46. self.view.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
  47. }
  48. } else {
  49. self.view.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
  50. }
  51. self.titleLabel = UILabel()
  52. self.titleLabel.text = NSLocalizedString("Document Info", comment: "")
  53. self.titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
  54. self.titleLabel.adjustsFontSizeToFitWidth = true
  55. self.titleLabel.textAlignment = .center
  56. self.view.addSubview(self.titleLabel)
  57. self.loadDocumentInfo()
  58. self.view.addSubview(self.curTableView)
  59. self.view.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  60. updatePreferredContentSize(with: self.traitCollection)
  61. self.doneBtn = UIButton(type: .system)
  62. self.doneBtn.autoresizingMask = .flexibleLeftMargin
  63. self.doneBtn.setTitle(NSLocalizedString("Done", comment: ""), for: .normal)
  64. self.doneBtn.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
  65. self.view.addSubview(self.doneBtn)
  66. }
  67. override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  68. super.willTransition(to: newCollection, with: coordinator)
  69. updatePreferredContentSize(with: newCollection)
  70. }
  71. override func viewWillLayoutSubviews() {
  72. super.viewWillLayoutSubviews()
  73. doneBtn.frame = CGRect(x: view.frame.size.width - 60, y: 5, width: 50, height: 50)
  74. titleLabel.frame = CGRect(x: (view.frame.size.width - 120)/2, y: 5, width: 120, height: 50)
  75. curTableView.frame = CGRect(x: 0, y: 70, width: view.frame.size.width, height: view.frame.size.height - 50)
  76. }
  77. func updatePreferredContentSize(with traitCollection: UITraitCollection) {
  78. let width = UIScreen.main.bounds.size.width
  79. let height = UIScreen.main.bounds.size.height
  80. let mWidth = min(width, height)
  81. let mHeight = max(width, height)
  82. let currentDevice = UIDevice.current
  83. if currentDevice.userInterfaceIdiom == .pad {
  84. // This is an iPad
  85. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.7 : mHeight * 0.7)
  86. } else {
  87. // This is an iPhone or iPod touch
  88. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.9 : mHeight * 0.9)
  89. }
  90. }
  91. @objc func buttonItemClicked_back(_ button: UIButton) {
  92. dismiss(animated: true)
  93. }
  94. // MARK: - private method
  95. func fileSizeStr() -> String? {
  96. let defaultManager = FileManager.default
  97. if !defaultManager.fileExists(atPath: self.currentPath!) {
  98. return ""
  99. }
  100. guard let attrib = try? defaultManager.attributesOfItem(atPath: self.currentPath!),
  101. let fileSize = attrib[FileAttributeKey.size] as? Float else {
  102. return ""
  103. }
  104. var size = fileSize / 1024
  105. var unit: String
  106. if size >= 1024 {
  107. if size < 1048576 {
  108. size /= 1024.0
  109. unit = "M"
  110. } else {
  111. size /= 1048576.0
  112. unit = "G"
  113. }
  114. } else {
  115. unit = "K"
  116. }
  117. return String(format: "%0.1f%@", size, unit)
  118. }
  119. func loadDocumentInfo() -> Void {
  120. let documentAttributes = self.pdfView?.document.documentAttributes
  121. self.currentPath = self.pdfView?.document.documentURL?.path
  122. var tableArray = [[[String: Any]]]()
  123. guard let documentAttributes = documentAttributes else {
  124. return
  125. }
  126. // 1.abstract
  127. var mArray = [[String: Any]]()
  128. if let currentPath = self.currentPath {
  129. mArray.append([kDocumentInfoTitle: NSLocalizedString("File Name:", comment: ""), kDocumentInfoValue: (currentPath as NSString).lastPathComponent ])
  130. mArray.append([kDocumentInfoTitle: NSLocalizedString("Size:", comment: ""), kDocumentInfoValue: self.fileSizeStr() ?? ""])
  131. }
  132. if let title = documentAttributes()![CPDFDocumentAttribute.titleAttribute] {
  133. mArray.append([kDocumentInfoTitle: NSLocalizedString("Title:", comment: ""), kDocumentInfoValue: title])
  134. }
  135. if let author = documentAttributes()![CPDFDocumentAttribute.authorAttribute] {
  136. mArray.append([kDocumentInfoTitle: NSLocalizedString("Author:", comment: ""), kDocumentInfoValue: author])
  137. }
  138. if let subject = documentAttributes()![CPDFDocumentAttribute.subjectAttribute] {
  139. mArray.append([kDocumentInfoTitle: NSLocalizedString("Subject:", comment: ""), kDocumentInfoValue: subject])
  140. }
  141. if let keywords = documentAttributes()![CPDFDocumentAttribute.keywordsAttribute] {
  142. mArray.append([kDocumentInfoTitle: NSLocalizedString("Keywords:", comment: ""), kDocumentInfoValue: keywords])
  143. }
  144. tableArray.append(mArray)
  145. mArray = [[String: Any]]()
  146. // 2. create
  147. mArray = [[String: Any]]()
  148. let versionString = "\(self.pdfView?.document.majorVersion ?? 0).\(self.pdfView?.document.minorVersion ?? 0)"
  149. mArray.append([kDocumentInfoTitle: NSLocalizedString("Version:", comment: ""), kDocumentInfoValue: versionString])
  150. mArray.append([kDocumentInfoTitle: NSLocalizedString("Pages:", comment: ""), kDocumentInfoValue: "\(self.pdfView?.document.pageCount ?? 0)"])
  151. if let creator = documentAttributes()![CPDFDocumentAttribute.creatorAttribute] {
  152. mArray.append([kDocumentInfoTitle: NSLocalizedString("Creator:", comment: ""), kDocumentInfoValue: creator])
  153. }
  154. if let creationDate = documentAttributes()![CPDFDocumentAttribute.creationDateAttribute] as? String {
  155. var mString = ""
  156. if creationDate.count >= 16 {
  157. let start = creationDate.index(creationDate.startIndex, offsetBy: 2)
  158. let end = creationDate.index(creationDate.startIndex, offsetBy: 4)
  159. mString.append(String(creationDate[start..<end]))
  160. let range = creationDate.index(start, offsetBy: 2) ..< creationDate.index(start, offsetBy: 4)
  161. mString.append("\(creationDate[range])")
  162. let range1 = creationDate.index(start, offsetBy: 4) ..< creationDate.index(start, offsetBy: 6)
  163. mString.append("-\(creationDate[range1])")
  164. let range2 = creationDate.index(start, offsetBy: 6) ..< creationDate.index(start, offsetBy: 8)
  165. mString.append("-\(creationDate[range2])")
  166. let range3 = creationDate.index(start, offsetBy: 8) ..< creationDate.index(start, offsetBy: 10)
  167. mString.append(" \(creationDate[range3])")
  168. let range4 = creationDate.index(start, offsetBy: 10) ..< creationDate.index(start, offsetBy: 12)
  169. mString.append(":\(creationDate[range4])")
  170. let range5 = creationDate.index(start, offsetBy: 12) ..< creationDate.index(start, offsetBy: 14)
  171. mString.append(":\(creationDate[range5])")
  172. mArray.append([
  173. kDocumentInfoTitle: NSLocalizedString("Creation Date:", comment: ""),
  174. kDocumentInfoValue: mString
  175. ])
  176. }
  177. }
  178. if let creationDate = documentAttributes()![CPDFDocumentAttribute.modificationDateAttribute] as? String {
  179. var mString = ""
  180. if creationDate.count >= 16 {
  181. let start = creationDate.index(creationDate.startIndex, offsetBy: 2)
  182. let end = creationDate.index(creationDate.startIndex, offsetBy: 4)
  183. mString.append(String(creationDate[start..<end]))
  184. let range = creationDate.index(start, offsetBy: 2) ..< creationDate.index(start, offsetBy: 4)
  185. mString.append("\(creationDate[range])")
  186. let range1 = creationDate.index(start, offsetBy: 4) ..< creationDate.index(start, offsetBy: 6)
  187. mString.append("-\(creationDate[range1])")
  188. let range2 = creationDate.index(start, offsetBy: 6) ..< creationDate.index(start, offsetBy: 8)
  189. mString.append("-\(creationDate[range2])")
  190. let range3 = creationDate.index(start, offsetBy: 8) ..< creationDate.index(start, offsetBy: 10)
  191. mString.append(" \(creationDate[range3])")
  192. let range4 = creationDate.index(start, offsetBy: 10) ..< creationDate.index(start, offsetBy: 12)
  193. mString.append(":\(creationDate[range4])")
  194. let range5 = creationDate.index(start, offsetBy: 12) ..< creationDate.index(start, offsetBy: 14)
  195. mString.append(":\(creationDate[range5])")
  196. mArray.append([
  197. kDocumentInfoTitle: NSLocalizedString("Modification Date:", comment: ""),
  198. kDocumentInfoValue: mString
  199. ])
  200. }
  201. }
  202. tableArray.append(mArray)
  203. // 3. execute
  204. mArray = [[String: Any]]()
  205. mArray.append([kDocumentInfoTitle: NSLocalizedString("Printing:", comment: ""), kDocumentInfoValue: ((self.pdfView?.document.allowsPrinting ?? false) ? NSLocalizedString("Allowed", comment: "") : NSLocalizedString("Not Allowed", comment: ""))])
  206. mArray.append([kDocumentInfoTitle: NSLocalizedString("Content Copying:", comment: ""), kDocumentInfoValue: ((self.pdfView?.document.allowsCopying ?? false) ? NSLocalizedString("Allowed", comment: "") : NSLocalizedString("Not Allowed", comment: ""))])
  207. mArray.append([kDocumentInfoTitle: NSLocalizedString("Document Change:", comment: ""), kDocumentInfoValue: ((self.pdfView?.document.allowsDocumentChanges ?? false) ? NSLocalizedString("Allowed", comment: "") : NSLocalizedString("Not Allowed", comment: ""))])
  208. mArray.append([kDocumentInfoTitle: NSLocalizedString("Document Assembly:", comment: ""), kDocumentInfoValue: ((self.pdfView?.document.allowsDocumentAssembly ?? false) ? NSLocalizedString("Allowed", comment: "") : NSLocalizedString("Not Allowed", comment: ""))])
  209. mArray.append([kDocumentInfoTitle: NSLocalizedString("Commenting:", comment: ""), kDocumentInfoValue: ((self.pdfView?.document.allowsCommenting ?? false) ? NSLocalizedString("Allowed", comment: "") : NSLocalizedString("Not Allowed", comment: ""))])
  210. mArray.append([kDocumentInfoTitle: NSLocalizedString("Filling of Form Field:", comment: ""), kDocumentInfoValue: ((self.pdfView?.document.allowsFormFieldEntry ?? false) ? NSLocalizedString("Allowed", comment: "") : NSLocalizedString("Not Allowed", comment: ""))])
  211. tableArray.append(mArray)
  212. self.curTableArray = tableArray
  213. }
  214. // MARK: - tableview delegate & datasource
  215. func numberOfSections(in tableView: UITableView) -> Int {
  216. return self.curTableArray.count
  217. }
  218. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  219. var title: String = ""
  220. switch section {
  221. case 0:
  222. title = NSLocalizedString("Abstract", comment: "")
  223. case 1:
  224. title = NSLocalizedString("Create Information:", comment: "")
  225. case 2:
  226. title = NSLocalizedString("Access Permissions:", comment: "")
  227. default:
  228. title = ""
  229. }
  230. return title
  231. }
  232. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  233. let headerLabel = UILabel.init(frame: CGRect(x: 20, y: 0, width: tableView.bounds.size.width, height: 20))
  234. var title: String = ""
  235. switch section {
  236. case 0:
  237. title = NSLocalizedString("Abstract:", comment: "")
  238. case 1:
  239. title = NSLocalizedString("Create Information:", comment: "")
  240. case 2:
  241. title = NSLocalizedString("Access Permissions:", comment: "")
  242. default:
  243. title = ""
  244. }
  245. title = " \(title)"
  246. headerLabel.text = title
  247. headerLabel.font = UIFont.boldSystemFont(ofSize: 22)
  248. return headerLabel
  249. }
  250. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  251. let array = self.curTableArray[section]
  252. return (array as AnyObject).count ?? 0
  253. }
  254. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  255. return 44
  256. }
  257. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  258. var cell = tableView.dequeueReusableCell(withIdentifier: "MyIdentifier") as? CPDFInfoTableCell
  259. if (cell == nil) {
  260. cell = CPDFInfoTableCell(style: .subtitle, reuseIdentifier: "MyIdentifier")
  261. }
  262. let array = curTableArray[indexPath.section]
  263. let dic = array[indexPath.row]
  264. cell?.setDataDictionary(dic)
  265. cell!.selectionStyle = .none
  266. return cell!
  267. }
  268. }