CPDFEditViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. //
  2. // CPDFEditViewController.swift
  3. // PDFViewer-Swift
  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 CPDFEditViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIColorPickerViewControllerDelegate, CPDFColorPickerViewDelegate, CPDFEditFontNameSelectViewDelegate, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
  15. var titleLabel:UILabel?
  16. weak var pdfView:CPDFView?
  17. var splitView:UIView?
  18. var tableView:UITableView?
  19. var colorPickerView:CPDFColorPickerView?
  20. var fontSelectView:CPDFEditFontNameSelectView?
  21. var fontStyleSelectView:CPDFEditFontNameSelectView?
  22. var fontName: String = "Helvetica"
  23. var fontStyle: String = ""
  24. var backBtn:UIButton?
  25. var textSampleView:CPDFEditTextSampleView?
  26. var imageSampleView:CPDFEditImageSampleView?
  27. public var editMode: CPDFEditMode = .text {
  28. didSet {
  29. updatePreferredContentSize(with: traitCollection)
  30. }
  31. }
  32. deinit {
  33. #if DEBUG
  34. print("====CPDFEditViewController==deinit")
  35. #endif
  36. }
  37. public init(pdfView: CPDFView) {
  38. super.init(nibName: nil, bundle: nil)
  39. self.pdfView = pdfView
  40. let editArea = self.pdfView?.editingArea()
  41. if editArea != nil {
  42. if editArea?.isTextArea() == true {
  43. let cPDFFont = self.pdfView?.editingSelectionCFont(with: editArea as? CPDFEditTextArea)
  44. fontName = cPDFFont?.familyName ?? "Helvetica"
  45. fontStyle = cPDFFont?.styleName ?? ""
  46. }
  47. } else {
  48. fontName = CPDFTextProperty.shared.fontNewFamilyName
  49. fontStyle = CPDFTextProperty.shared.fontNewStyle
  50. }
  51. }
  52. required init?(coder: NSCoder) {
  53. fatalError("init(coder:) has not been implemented")
  54. }
  55. public override func viewDidLoad() {
  56. super.viewDidLoad()
  57. var bottomPadding: CGFloat = 0
  58. var leftPadding: CGFloat = 0
  59. var rightPadding: CGFloat = 0
  60. if #available(iOS 11.0, *) {
  61. let window = UIApplication.shared.windows.first
  62. bottomPadding = window?.safeAreaInsets.bottom ?? 0
  63. leftPadding = window?.safeAreaInsets.left ?? 0
  64. rightPadding = window?.safeAreaInsets.right ?? 0
  65. }
  66. self.view.frame = CGRect(
  67. x: leftPadding,
  68. y: UIScreen.main.bounds.size.height - bottomPadding,
  69. width: UIScreen.main.bounds.size.width - leftPadding - rightPadding,
  70. height: self.view.frame.size.height
  71. )
  72. self.titleLabel = UILabel()
  73. self.titleLabel?.autoresizingMask = .flexibleRightMargin
  74. self.titleLabel?.text = (self.editMode == .text) ? NSLocalizedString("Text Properties", comment: "") : NSLocalizedString("Image Properties", comment: "")
  75. self.titleLabel?.font = UIFont.systemFont(ofSize: 20)
  76. self.titleLabel?.adjustsFontSizeToFitWidth = true
  77. if(titleLabel != nil) {
  78. self.view.addSubview(self.titleLabel!)
  79. }
  80. self.backBtn = UIButton()
  81. self.backBtn?.autoresizingMask = .flexibleLeftMargin
  82. self.backBtn?.setImage(UIImage(named: "CPDFEditClose", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  83. self.backBtn?.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
  84. if(backBtn != nil) {
  85. self.view.addSubview(self.backBtn!)
  86. }
  87. self.splitView = UIView()
  88. self.splitView?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
  89. if(splitView != nil) {
  90. self.view.addSubview(self.splitView!)
  91. }
  92. self.view.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  93. self.updatePreferredContentSize(with: self.traitCollection)
  94. self.tableView = UITableView(frame: .zero, style: .plain)
  95. self.tableView?.delegate = self
  96. self.tableView?.dataSource = self
  97. if #available(iOS 15.0, *) {
  98. self.tableView?.sectionHeaderTopPadding = 0
  99. }
  100. else {
  101. // Fallback on earlier versions
  102. }
  103. self.tableView?.reloadData()
  104. if(tableView != nil) {
  105. self.view.addSubview(self.tableView!)
  106. }
  107. self.tableView?.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  108. }
  109. public override func viewWillLayoutSubviews() {
  110. super.viewWillLayoutSubviews()
  111. if #available(iOS 11.0, *) {
  112. self.titleLabel?.frame = CGRect(x: (self.view.frame.size.width - 120)/2, y: 5, width: 120, height: 50)
  113. self.splitView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 51, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 1)
  114. self.tableView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 52, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: self.view.frame.size.height - 52)
  115. self.backBtn?.frame = CGRect(x: self.view.frame.size.width - 60, y: 5, width: 50, height: 50)
  116. } else {
  117. self.titleLabel?.frame = CGRect(x: (self.view.frame.size.width - 120)/2, y: 5, width: 120, height: 50)
  118. self.splitView?.frame = CGRect(x: 0, y: 51, width: self.view.frame.size.width, height: 1)
  119. self.tableView?.frame = CGRect(x: 0, y: 52, width: self.view.frame.size.width, height: self.view.frame.size.height - 52)
  120. self.backBtn?.frame = CGRect(x: self.view.frame.size.width - 60, y: 5, width: 50, height: 50)
  121. }
  122. }
  123. public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  124. super.willTransition(to: newCollection, with: coordinator)
  125. updatePreferredContentSize(with: newCollection)
  126. }
  127. func updatePreferredContentSize(with traitCollection: UITraitCollection) {
  128. if self.editMode == .text {
  129. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 300 : 600)
  130. } else {
  131. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 300 : 600)
  132. }
  133. }
  134. public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  135. if self.editMode == .text {
  136. let view = UIView(frame: CGRect(x: 20, y: 0, width: self.view.bounds.size.width-40, height: 120))
  137. view.backgroundColor = UIColor.white
  138. view.layer.borderWidth = 1.0
  139. view.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  140. view.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  141. self.textSampleView = CPDFEditTextSampleView()
  142. self.textSampleView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  143. self.textSampleView?.layer.borderWidth = 1.0
  144. self.textSampleView?.autoresizingMask = .flexibleRightMargin
  145. self.textSampleView?.frame = CGRect(x: (self.view.frame.size.width - 300)/2, y: 15, width: 300, height: view.bounds.size.height - 30)
  146. let editArea = self.pdfView?.editingArea()
  147. if editArea != nil {
  148. if editArea?.isTextArea() == true {
  149. self.textSampleView?.textAlignmnet = self.pdfView?.editingSelectionAlignment(with: editArea as? CPDFEditTextArea) ?? .left
  150. self.textSampleView?.textColor = self.pdfView?.editingSelectionFontColor(with: editArea as? CPDFEditTextArea) ?? UIColor.clear
  151. self.textSampleView?.textOpacity = self.pdfView?.getCurrentOpacity() ?? 1.0
  152. self.textSampleView?.fontSize = self.pdfView?.editingSelectionFontSizes(with: editArea as? CPDFEditTextArea) ?? 0
  153. }
  154. } else {
  155. self.textSampleView?.textAlignmnet = CPDFTextProperty.shared.textAlignment
  156. var color = CPDFTextProperty.shared.fontColor
  157. var red: CGFloat = 0
  158. var green: CGFloat = 0
  159. var blue: CGFloat = 0
  160. var alpha: CGFloat = 0
  161. color?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  162. color = UIColor(red: red, green: green, blue: blue, alpha: CPDFTextProperty.shared.textOpacity)
  163. self.textSampleView?.textColor = color ?? .black
  164. self.textSampleView?.textOpacity = CPDFTextProperty.shared.textOpacity
  165. self.textSampleView?.fontSize = CPDFTextProperty.shared.fontSize
  166. }
  167. let familyName = self.fontName
  168. let styleName = self.fontStyle
  169. let cFont = CPDFFont(familyName: familyName, fontStyle: styleName )
  170. var font = UIFont.init(name: CPDFFont.convertAppleFont(cFont) ?? "Helvetica", size: 16.0)
  171. if font == nil {
  172. font = UIFont(name: "Helvetica-Oblique", size: 16.0)
  173. }
  174. self.textSampleView?.fontName = font?.fontName
  175. if self.textSampleView != nil {
  176. view.addSubview(self.textSampleView!)
  177. }
  178. return view
  179. } else {
  180. let view = UIView(frame: CGRect(x: 20, y: 0, width: self.view.bounds.size.width-40, height: 120))
  181. view.backgroundColor = UIColor.white
  182. view.layer.borderWidth = 1.0
  183. view.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  184. view.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  185. self.imageSampleView = CPDFEditImageSampleView()
  186. self.imageSampleView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  187. self.imageSampleView?.layer.borderWidth = 1.0
  188. self.imageSampleView?.autoresizingMask = .flexibleRightMargin
  189. self.imageSampleView?.frame = CGRect(x: (self.view.frame.size.width - 300)/2, y: 15, width: 300, height: view.bounds.size.height - 30)
  190. var image: UIImage? = nil
  191. let editArea = self.pdfView?.editingArea()
  192. if let editImageArea = editArea as? CPDFEditImageArea {
  193. image = editImageArea.thumbnailImage(with: editImageArea.bounds.size)
  194. }
  195. if image != nil {
  196. self.imageSampleView?.imageView?.image = image
  197. } else {
  198. if(editArea?.isImageArea() == true) {
  199. let imageArea:CPDFEditImageArea = editArea as! CPDFEditImageArea
  200. let rotation:CGFloat = self.pdfView?.getRotationEdit(imageArea) ?? 0
  201. if rotation > 0 {
  202. if rotation > 90 {
  203. self.imageSampleView?.imageView?.transform = self.imageSampleView?.imageView?.transform.rotated(by: CGFloat(Double.pi)) ?? CGAffineTransform()
  204. } else {
  205. self.imageSampleView?.imageView?.transform = self.imageSampleView?.imageView?.transform.rotated(by: CGFloat(Double.pi/2)) ?? CGAffineTransform()
  206. }
  207. } else if (rotation < 0) {
  208. self.imageSampleView?.imageView?.transform = self.imageSampleView?.imageView?.transform.rotated(by: CGFloat(-Double.pi/2)) ?? CGAffineTransform()
  209. }
  210. self.imageSampleView?.imageView?.alpha = self.pdfView?.getCurrentOpacity() ?? 0
  211. }
  212. }
  213. if(self.imageSampleView != nil) {
  214. view.addSubview(self.imageSampleView!)
  215. }
  216. return view
  217. }
  218. return nil
  219. }
  220. // MARK: - UITableViewDataSource
  221. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  222. return 1
  223. }
  224. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  225. if self.editMode == .text {
  226. var cell = tableView.dequeueReusableCell(withIdentifier: "Textcell") as? CPDFTextPropertyCell
  227. if cell == nil {
  228. cell = CPDFTextPropertyCell(style: .default, reuseIdentifier: "Textcell")
  229. }
  230. cell?.backgroundColor = UIColor(red: 250/255, green: 252/255, blue: 255/255, alpha: 1)
  231. let cFont = CPDFFont(familyName: fontName, fontStyle: fontStyle)
  232. cell?.currentSelectCFont = cFont
  233. if(self.pdfView != nil) {
  234. cell?.setPdfView(self.pdfView!)
  235. }
  236. cell?.actionBlock = { [weak self] actionType in
  237. guard let weakSelf = self else { return }
  238. if actionType == .colorSelect {
  239. if #available(iOS 14.0, *) {
  240. let picker = UIColorPickerViewController()
  241. picker.delegate = weakSelf
  242. weakSelf.present(picker, animated: true, completion: nil)
  243. } else {
  244. weakSelf.colorPickerView = CPDFColorPickerView(frame: weakSelf.view.frame)
  245. weakSelf.colorPickerView?.delegate = weakSelf
  246. weakSelf.colorPickerView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  247. weakSelf.colorPickerView?.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  248. if(weakSelf.colorPickerView != nil) {
  249. weakSelf.view.addSubview(weakSelf.colorPickerView!)
  250. }
  251. }
  252. } else if actionType == .fontNameSelect {
  253. weakSelf.fontSelectView = CPDFEditFontNameSelectView(frame: weakSelf.view.bounds, familyNames: weakSelf.fontName, styleName: weakSelf.fontStyle, isFontStyle: false)
  254. weakSelf.fontSelectView?.delegate = weakSelf
  255. weakSelf.fontSelectView?.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  256. if(weakSelf.fontSelectView != nil) {
  257. weakSelf.view.addSubview(weakSelf.fontSelectView!)
  258. }
  259. } else if actionType == .fontStyleSelect {
  260. weakSelf.fontStyleSelectView = CPDFEditFontNameSelectView(frame: weakSelf.view.bounds, familyNames: weakSelf.fontName, styleName: weakSelf.fontStyle, isFontStyle: true)
  261. weakSelf.fontStyleSelectView?.delegate = weakSelf
  262. weakSelf.fontStyleSelectView?.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  263. if(weakSelf.fontStyleSelectView != nil) {
  264. weakSelf.view.addSubview(weakSelf.fontStyleSelectView!)
  265. }
  266. }
  267. }
  268. cell?.colorBlock = { [weak self] selectColor in
  269. guard let weakSelf = self else { return }
  270. weakSelf.textSampleView?.textColor = selectColor
  271. let editingArea = weakSelf.pdfView?.editingArea()
  272. if editingArea != nil {
  273. weakSelf.pdfView?.setEditingSelectionFontColor(selectColor, with: editingArea as? CPDFEditTextArea)
  274. } else {
  275. CPDFTextProperty.shared.fontColor = selectColor
  276. }
  277. }
  278. cell?.alignmentBlock = {[weak self] alignment in
  279. guard let weakSelf = self else { return }
  280. let row:CPDFTextAlignment = alignment
  281. var textAlignmnet:NSTextAlignment = .left
  282. if (row == .left) {
  283. textAlignmnet = .left
  284. } else if (row == .center) {
  285. textAlignmnet = .center
  286. } else if row == .right {
  287. textAlignmnet = .right
  288. } else if row == .justified {
  289. textAlignmnet = .justified
  290. } else if row == .natural {
  291. textAlignmnet = .natural
  292. }
  293. weakSelf.textSampleView?.textAlignmnet = textAlignmnet
  294. let editingArea = weakSelf.pdfView?.editingArea()
  295. if editingArea != nil {
  296. weakSelf.pdfView?.setCurrentSelectionAlignment(textAlignmnet, with: editingArea as? CPDFEditTextArea)
  297. } else {
  298. CPDFTextProperty.shared.textAlignment = textAlignmnet
  299. }
  300. }
  301. cell?.fontSizeBlock = {[weak self] fontSize in
  302. guard let weakSelf = self else { return }
  303. weakSelf.textSampleView?.fontSize = fontSize * 10
  304. let editingArea = weakSelf.pdfView?.editingArea()
  305. if editingArea != nil {
  306. weakSelf.pdfView?.setEditingSelectionFontSize(fontSize * 10, with: editingArea as? CPDFEditTextArea, isAutoSize: true)
  307. } else {
  308. CPDFTextProperty.shared.fontSize = fontSize * 10
  309. }
  310. }
  311. cell?.opacityBlock = {[weak self] opacity in
  312. guard let weakSelf = self else { return }
  313. weakSelf.textSampleView?.textOpacity = opacity
  314. let editingArea = weakSelf.pdfView?.editingArea()
  315. if editingArea != nil {
  316. weakSelf.pdfView?.setCharsFontTransparency(Float(opacity), with: editingArea as? CPDFEditTextArea)
  317. } else {
  318. CPDFTextProperty.shared.textOpacity = opacity
  319. }
  320. }
  321. cell?.selectionStyle = .none
  322. tableView.separatorStyle = .none
  323. return cell!
  324. } else {
  325. var cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as? CPDFImagePropertyCell
  326. if cell == nil {
  327. cell = CPDFImagePropertyCell(style: .default, reuseIdentifier: "CPDFImagePropertyCell")
  328. }
  329. cell?.backgroundColor = UIColor(red: 250/255, green: 252/255, blue: 255/255, alpha: 1)
  330. if(self.pdfView != nil) {
  331. cell?.setPdfView(self.pdfView!)
  332. }
  333. cell?.rotateBlock = {[weak self] rotateType, isRotated in
  334. guard let weakSelf = self else { return }
  335. if rotateType == .left {
  336. let editingArea = weakSelf.pdfView?.editingArea()
  337. if(editingArea != nil) {
  338. weakSelf.pdfView?.rotateEdit(editingArea as? CPDFEditImageArea, rotateAngle: -90)
  339. weakSelf.imageSampleView?.imageView?.transform = weakSelf.imageSampleView?.imageView?.transform.rotated(by: -CGFloat.pi/2) ?? CGAffineTransform()
  340. weakSelf.imageSampleView?.setNeedsLayout()
  341. }
  342. } else if rotateType == .right {
  343. let editingArea = weakSelf.pdfView?.editingArea()
  344. if(editingArea != nil) {
  345. weakSelf.pdfView?.rotateEdit(editingArea as? CPDFEditImageArea, rotateAngle: 90)
  346. weakSelf.imageSampleView?.imageView?.transform = weakSelf.imageSampleView?.imageView?.transform.rotated(by: CGFloat.pi/2) ?? CGAffineTransform()
  347. weakSelf.imageSampleView?.setNeedsLayout()
  348. }
  349. }
  350. }
  351. cell?.transFormBlock = {[weak self] transformType, isTransformed in
  352. guard let weakSelf = self else { return }
  353. if transformType == .vertical {
  354. let editingArea = weakSelf.pdfView?.editingArea()
  355. if(editingArea != nil) {
  356. weakSelf.pdfView?.verticalMirrorEdit(editingArea as? CPDFEditImageArea)
  357. weakSelf.imageSampleView?.imageView?.transform = weakSelf.imageSampleView?.imageView?.transform.scaledBy(x: 1.0, y: -1.0) ?? CGAffineTransform()
  358. weakSelf.imageSampleView?.setNeedsLayout()
  359. }
  360. } else if transformType == .horizontal {
  361. let editingArea = weakSelf.pdfView?.editingArea()
  362. if(editingArea != nil) {
  363. weakSelf.pdfView?.horizontalMirrorEdit(editingArea as? CPDFEditImageArea)
  364. weakSelf.imageSampleView?.imageView?.transform = weakSelf.imageSampleView?.imageView?.transform.scaledBy(x: -1.0, y: 1.0) ?? CGAffineTransform()
  365. weakSelf.imageSampleView?.setNeedsLayout()
  366. }
  367. }
  368. }
  369. cell?.transparencyBlock = {[weak self] transparency in
  370. guard let weakSelf = self else { return }
  371. let editingArea = weakSelf.pdfView?.editingArea()
  372. if(editingArea != nil) {
  373. weakSelf.pdfView?.setImageTransparencyEdit(editingArea as? CPDFEditImageArea, transparency: Float(transparency))
  374. weakSelf.imageSampleView?.imageView?.alpha = transparency
  375. weakSelf.imageSampleView?.setNeedsLayout()
  376. }
  377. }
  378. cell?.replaceImageBlock = {[weak self] in
  379. guard let weakSelf = self else { return }
  380. let imagePicker = UIImagePickerController()
  381. imagePicker.sourceType = .photoLibrary
  382. imagePicker.delegate = weakSelf
  383. weakSelf.present(imagePicker, animated: true, completion: nil)
  384. }
  385. cell?.cropImageBlock = {[weak self] in
  386. guard let weakSelf = self else { return }
  387. let editingArea = weakSelf.pdfView?.editingArea()
  388. if(editingArea != nil) {
  389. weakSelf.pdfView?.beginCropEdit(editingArea as? CPDFEditImageArea)
  390. weakSelf.controllerDismiss()
  391. }
  392. }
  393. cell?.exportImageBlock = {[weak self] in
  394. guard let weakSelf = self else { return }
  395. let editingArea = weakSelf.pdfView?.editingArea()
  396. if(editingArea != nil) {
  397. let saved = weakSelf.pdfView?.extractImage(withEditImageArea: editingArea)
  398. if saved ?? false {
  399. let alertController = UIAlertController(title: "", message: NSLocalizedString("Export Successfully!", comment: ""), preferredStyle: .alert)
  400. alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: { action in
  401. weakSelf.controllerDismiss()
  402. }))
  403. weakSelf.present(alertController, animated: true, completion: nil)
  404. } else {
  405. let alertController = UIAlertController(title: "", message: NSLocalizedString("Export Failed!", comment: ""), preferredStyle: .alert)
  406. alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: { action in
  407. weakSelf.controllerDismiss()
  408. }))
  409. weakSelf.present(alertController, animated: true, completion: nil)
  410. }
  411. }
  412. }
  413. cell?.selectionStyle = .none
  414. tableView.separatorStyle = .none
  415. return cell!
  416. }
  417. }
  418. // MARK: - UITableViewDelegate
  419. public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  420. return 120
  421. }
  422. public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  423. if self.editMode == .text {
  424. return 400
  425. } else {
  426. return 380
  427. }
  428. }
  429. // MARK: - ColorPickerDelegate
  430. func pickerView(_ colorPickerView: CPDFColorPickerView, color: UIColor) {
  431. self.textSampleView?.textColor = color
  432. var red: CGFloat = 0
  433. var green: CGFloat = 0
  434. var blue: CGFloat = 0
  435. var alpha: CGFloat = 0
  436. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  437. let editingArea = self.pdfView?.editingArea()
  438. if editingArea != nil {
  439. self.pdfView?.setEditingSelectionFontColor(color, with: editingArea as? CPDFEditTextArea)
  440. } else {
  441. CPDFTextProperty.shared.fontColor = color
  442. }
  443. }
  444. // MARK: - CPDFEditFontNameSelectViewDelegate
  445. func pickerView(_ colorPickerView: CPDFEditFontNameSelectView, fontName: String) {
  446. var styleName = fontStyle
  447. var familyName = self.fontName
  448. if(colorPickerView == self.fontSelectView) {
  449. familyName = fontName
  450. let datasArray:[String] = CPDFFont.fontNames(forFamilyName: familyName)
  451. styleName = datasArray.first ?? ""
  452. } else {
  453. styleName = fontName
  454. }
  455. self.fontName = familyName;
  456. self.fontStyle = styleName
  457. let cfont = CPDFFont(familyName: self.fontName, fontStyle: self.fontStyle)
  458. var font = UIFont.init(name: CPDFFont.convertAppleFont(cfont) ?? "Helvetica", size: 16.0)
  459. if font == nil {
  460. font = UIFont(name: "Helvetica-Oblique", size: 16.0)
  461. }
  462. textSampleView?.fontName = font?.fontName
  463. let editingArea = self.pdfView?.editingArea()
  464. if editingArea != nil {
  465. pdfView?.setEditSelectionCFont(cfont, with: editingArea as? CPDFEditTextArea)
  466. } else {
  467. CPDFTextProperty.shared.fontNewFamilyName = self.fontName
  468. CPDFTextProperty.shared.fontNewStyle = self.fontStyle
  469. }
  470. tableView?.reloadData()
  471. }
  472. // MARK: - UIColorPickerViewControllerDelegate
  473. @available(iOS 14.0, *)
  474. public func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
  475. textSampleView?.textColor = viewController.selectedColor
  476. var red: CGFloat = 0
  477. var green: CGFloat = 0
  478. var blue: CGFloat = 0
  479. var alpha: CGFloat = 0
  480. viewController.selectedColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  481. let editingArea = self.pdfView?.editingArea()
  482. if editingArea != nil {
  483. pdfView?.setEditingSelectionFontColor(viewController.selectedColor, with: editingArea as? CPDFEditTextArea)
  484. pdfView?.setCharsFontTransparency(Float(alpha), with: editingArea as? CPDFEditTextArea)
  485. } else {
  486. CPDFTextProperty.shared.fontColor = viewController.selectedColor
  487. CPDFTextProperty.shared.textOpacity = CGFloat(Float(alpha))
  488. }
  489. textSampleView?.textOpacity = CGFloat(Float(alpha))
  490. tableView?.reloadData()
  491. }
  492. // MARK: - UIImagePickerControllerDelegate
  493. public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  494. let editingArea = self.pdfView?.editingArea()
  495. if(editingArea != nil) {
  496. var url:URL?
  497. if #available(iOS 11.0, *) {
  498. url = info[UIImagePickerController.InfoKey.imageURL] as? URL
  499. } else {
  500. url = info[UIImagePickerController.InfoKey.mediaURL] as? URL
  501. }
  502. if(url != nil) {
  503. if var image = UIImage(contentsOfFile: url!.path) {
  504. var imgWidth: CGFloat = 0
  505. var imgHeight: CGFloat = 0
  506. var scaledWidth: CGFloat = 149
  507. var scaledHeight: CGFloat = 210
  508. if image.imageOrientation != .up {
  509. UIGraphicsBeginImageContext(image.size)
  510. image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
  511. if let rotatedImage = UIGraphicsGetImageFromCurrentImageContext() {
  512. image = rotatedImage
  513. }
  514. UIGraphicsEndImageContext()
  515. imgWidth = image.size.height
  516. imgHeight = image.size.width
  517. } else {
  518. imgWidth = image.size.width
  519. imgHeight = image.size.height
  520. }
  521. scaledHeight = scaledWidth * imgHeight / imgWidth
  522. let rect = CGRect(x: editingArea?.bounds.origin.x ?? 0, y: editingArea?.bounds.origin.y ?? 0, width: scaledWidth, height: scaledHeight)
  523. pdfView?.replace(editingArea as? CPDFEditImageArea, imagePath: url!.path,rect:rect)
  524. tableView?.reloadData()
  525. }
  526. }
  527. }
  528. controllerDismiss()
  529. }
  530. // MARK: - Action
  531. @objc func buttonItemClicked_back(_ sender: UIButton) {
  532. controllerDismiss()
  533. }
  534. @objc func controllerDismiss() {
  535. self.dismiss(animated: true)
  536. }
  537. }