RCTCPDFPageUtil.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // RCTCPDFPageUtil.swift
  3. // react-native-compdfkit-pdf
  4. //
  5. // Copyright © 2014-2025 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 RCTCPDFPageUtil: NSObject {
  15. private var page: CPDFPage?
  16. public var pageIndex: Int = 0
  17. init(page: CPDFPage? = nil) {
  18. self.page = page
  19. }
  20. //MARK: - Public Methods
  21. func getAnnotations() -> [Dictionary<String, Any>] {
  22. var annotaionDicts:[Dictionary<String, Any>] = []
  23. let annoations = page?.annotations ?? []
  24. for annoation in annoations {
  25. var annotaionDict: [String : Any] = [:]
  26. let type: String = annoation.type
  27. if annoation.type == "Widget" {
  28. continue
  29. }
  30. switch type {
  31. case "Highlight", "Squiggly", "Underline", "Strikeout":
  32. if let markupAnnotation = annoation as? CPDFMarkupAnnotation {
  33. let lowertype = lowercaseFirstLetter(of: type)
  34. annotaionDict["type"] = lowertype
  35. annotaionDict["title"] = markupAnnotation.userName()
  36. annotaionDict["page"] = pageIndex
  37. annotaionDict["content"] = markupAnnotation.contents
  38. }
  39. case "Circle":
  40. if let circleAnnotation = annoation as? CPDFCircleAnnotation {
  41. let lowertype = lowercaseFirstLetter(of: type)
  42. annotaionDict["type"] = lowertype
  43. annotaionDict["title"] = circleAnnotation.userName()
  44. annotaionDict["page"] = pageIndex
  45. annotaionDict["content"] = circleAnnotation.contents
  46. }
  47. case "Square":
  48. if let squareAnnotation = annoation as? CPDFSquareAnnotation {
  49. let lowertype = lowercaseFirstLetter(of: type)
  50. annotaionDict["type"] = lowertype
  51. annotaionDict["title"] = squareAnnotation.userName()
  52. annotaionDict["page"] = pageIndex
  53. annotaionDict["content"] = squareAnnotation.contents
  54. }
  55. case "Line", "Arrow":
  56. if let lineAnnotation = annoation as? CPDFLineAnnotation {
  57. let lowertype = lowercaseFirstLetter(of: type)
  58. annotaionDict["type"] = lowertype
  59. annotaionDict["title"] = lineAnnotation.userName()
  60. annotaionDict["page"] = pageIndex
  61. annotaionDict["content"] = lineAnnotation.contents
  62. }
  63. case "Freehand":
  64. if let inkAnnotation = annoation as? CPDFInkAnnotation {
  65. let lowertype = lowercaseFirstLetter(of: type)
  66. annotaionDict["type"] = lowertype
  67. annotaionDict["title"] = inkAnnotation.userName()
  68. annotaionDict["page"] = pageIndex
  69. annotaionDict["content"] = inkAnnotation.contents
  70. }
  71. case "Note":
  72. if let noteAnnotation = annoation as? CPDFTextAnnotation {
  73. let lowertype = lowercaseFirstLetter(of: type)
  74. annotaionDict["type"] = lowertype
  75. annotaionDict["title"] = noteAnnotation.userName()
  76. annotaionDict["page"] = pageIndex
  77. annotaionDict["content"] = noteAnnotation.contents
  78. }
  79. case "FreeText":
  80. if let freeTextAnnotation = annoation as? CPDFFreeTextAnnotation {
  81. let lowertype = lowercaseFirstLetter(of: type)
  82. annotaionDict["type"] = lowertype
  83. annotaionDict["title"] = freeTextAnnotation.userName()
  84. annotaionDict["page"] = pageIndex
  85. annotaionDict["content"] = freeTextAnnotation.contents
  86. }
  87. case "Stamp", "Image":
  88. if let stampAnnotation = annoation as? CPDFStampAnnotation {
  89. let lowertype = lowercaseFirstLetter(of: type)
  90. annotaionDict["type"] = lowertype
  91. annotaionDict["title"] = stampAnnotation.userName()
  92. annotaionDict["page"] = pageIndex
  93. annotaionDict["content"] = stampAnnotation.contents
  94. }
  95. case "Link":
  96. if let linkAnnotation = annoation as? CPDFLinkAnnotation {
  97. let lowertype = lowercaseFirstLetter(of: type)
  98. annotaionDict["type"] = lowertype
  99. annotaionDict["title"] = linkAnnotation.userName()
  100. annotaionDict["page"] = pageIndex
  101. annotaionDict["content"] = linkAnnotation.contents
  102. }
  103. case "Media":
  104. if let mediaAnnotation = annoation as? CPDFSoundAnnotation {
  105. let lowertype = lowercaseFirstLetter(of: type)
  106. annotaionDict["type"] = lowertype
  107. annotaionDict["title"] = mediaAnnotation.userName()
  108. annotaionDict["page"] = pageIndex
  109. annotaionDict["content"] = mediaAnnotation.contents
  110. }
  111. default:
  112. print("Unhandled type: \(type)")
  113. }
  114. annotaionDicts.append(annotaionDict)
  115. }
  116. return annotaionDicts
  117. }
  118. func getForms() -> [Dictionary<String, Any>] {
  119. var formDicts:[Dictionary<String, Any>] = []
  120. let forms = page?.annotations ?? []
  121. for form in forms {
  122. var formDict: [String : Any] = [:]
  123. let type: String = form.type
  124. if form.type == "Widget" {
  125. if let widgetAnnotation = form as? CPDFWidgetAnnotation {
  126. let widgetType: String = widgetAnnotation.widgetType
  127. switch widgetType {
  128. case "CheckBox", "RadioButton", "PushButton":
  129. if let buttonWidget = form as? CPDFButtonWidgetAnnotation {
  130. let lowertype = lowercaseFirstLetter(of: widgetType)
  131. formDict["type"] = lowertype
  132. formDict["title"] = buttonWidget.fieldName()
  133. formDict["page"] = pageIndex
  134. if widgetType != "PushButton" {
  135. let isOn = buttonWidget.state()
  136. if (isOn != 0) {
  137. formDict["isChecked"] = true
  138. } else {
  139. formDict["isChecked"] = false
  140. }
  141. }
  142. }
  143. case "TextField":
  144. if let textFieldWidget = form as? CPDFTextWidgetAnnotation {
  145. let lowertype = lowercaseFirstLetter(of: widgetType)
  146. formDict["type"] = lowertype
  147. formDict["title"] = textFieldWidget.fieldName()
  148. formDict["page"] = pageIndex
  149. formDict["text"] = textFieldWidget.stringValue
  150. }
  151. case "ListBox", "ComboBox":
  152. if let choiceWidget = form as? CPDFChoiceWidgetAnnotation {
  153. let lowertype = lowercaseFirstLetter(of: widgetType)
  154. formDict["type"] = lowertype
  155. formDict["title"] = choiceWidget.fieldName()
  156. formDict["page"] = pageIndex
  157. }
  158. case "SignatureFields":
  159. if let signatureWidget = form as? CPDFSignatureWidgetAnnotation {
  160. let lowertype = lowercaseFirstLetter(of: widgetType)
  161. formDict["type"] = lowertype
  162. formDict["title"] = signatureWidget.fieldName()
  163. formDict["page"] = pageIndex
  164. }
  165. default:
  166. print("Unhandled type: \(type)")
  167. }
  168. formDicts.append(formDict)
  169. }
  170. }
  171. }
  172. return formDicts
  173. }
  174. //MARK: - Private Methods
  175. func lowercaseFirstLetter(of string: String) -> String {
  176. guard !string.isEmpty else { return string }
  177. let lowercaseString = string.prefix(1).lowercased() + string.dropFirst()
  178. return lowercaseString
  179. }
  180. }