RCTCPDFPageUtil.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. annotaionDict["type"] = "ink"
  66. annotaionDict["title"] = inkAnnotation.userName()
  67. annotaionDict["page"] = pageIndex
  68. annotaionDict["content"] = inkAnnotation.contents
  69. }
  70. case "Note":
  71. if let noteAnnotation = annoation as? CPDFTextAnnotation {
  72. let lowertype = lowercaseFirstLetter(of: type)
  73. annotaionDict["type"] = lowertype
  74. annotaionDict["title"] = noteAnnotation.userName()
  75. annotaionDict["page"] = pageIndex
  76. annotaionDict["content"] = noteAnnotation.contents
  77. }
  78. case "FreeText":
  79. if let freeTextAnnotation = annoation as? CPDFFreeTextAnnotation {
  80. let lowertype = lowercaseFirstLetter(of: type)
  81. annotaionDict["type"] = lowertype
  82. annotaionDict["title"] = freeTextAnnotation.userName()
  83. annotaionDict["page"] = pageIndex
  84. annotaionDict["content"] = freeTextAnnotation.contents
  85. }
  86. case "Stamp", "Image":
  87. if let stampAnnotation = annoation as? CPDFStampAnnotation {
  88. let lowertype = lowercaseFirstLetter(of: type)
  89. annotaionDict["type"] = lowertype
  90. if type == "Image" {
  91. annotaionDict["type"] = "pictures"
  92. }
  93. annotaionDict["title"] = stampAnnotation.userName()
  94. annotaionDict["page"] = pageIndex
  95. annotaionDict["content"] = stampAnnotation.contents
  96. }
  97. case "Link":
  98. if let linkAnnotation = annoation as? CPDFLinkAnnotation {
  99. let lowertype = lowercaseFirstLetter(of: type)
  100. annotaionDict["type"] = lowertype
  101. annotaionDict["title"] = linkAnnotation.userName()
  102. annotaionDict["page"] = pageIndex
  103. annotaionDict["content"] = linkAnnotation.contents
  104. }
  105. case "Media":
  106. if let mediaAnnotation = annoation as? CPDFSoundAnnotation {
  107. let lowertype = lowercaseFirstLetter(of: type)
  108. annotaionDict["type"] = lowertype
  109. annotaionDict["title"] = mediaAnnotation.userName()
  110. annotaionDict["page"] = pageIndex
  111. annotaionDict["content"] = mediaAnnotation.contents
  112. }
  113. default:
  114. print("Unhandled type: \(type)")
  115. }
  116. annotaionDicts.append(annotaionDict)
  117. }
  118. return annotaionDicts
  119. }
  120. func getForms() -> [Dictionary<String, Any>] {
  121. var formDicts:[Dictionary<String, Any>] = []
  122. let forms = page?.annotations ?? []
  123. for form in forms {
  124. var formDict: [String : Any] = [:]
  125. let type: String = form.type
  126. if form.type == "Widget" {
  127. if let widgetAnnotation = form as? CPDFWidgetAnnotation {
  128. let widgetType: String = widgetAnnotation.widgetType
  129. switch widgetType {
  130. case "CheckBox", "RadioButton", "PushButton":
  131. if let buttonWidget = form as? CPDFButtonWidgetAnnotation {
  132. let lowertype = lowercaseFirstLetter(of: widgetType)
  133. formDict["type"] = lowertype
  134. formDict["title"] = buttonWidget.fieldName()
  135. formDict["page"] = pageIndex
  136. if widgetType != "PushButton" {
  137. let isOn = buttonWidget.state()
  138. if (isOn != 0) {
  139. formDict["isChecked"] = true
  140. } else {
  141. formDict["isChecked"] = false
  142. }
  143. }
  144. }
  145. case "TextField":
  146. if let textFieldWidget = form as? CPDFTextWidgetAnnotation {
  147. let lowertype = lowercaseFirstLetter(of: widgetType)
  148. formDict["type"] = lowertype
  149. formDict["title"] = textFieldWidget.fieldName()
  150. formDict["page"] = pageIndex
  151. formDict["text"] = textFieldWidget.stringValue
  152. }
  153. case "ListBox", "ComboBox":
  154. if let choiceWidget = form as? CPDFChoiceWidgetAnnotation {
  155. let lowertype = lowercaseFirstLetter(of: widgetType)
  156. formDict["type"] = lowertype
  157. formDict["title"] = choiceWidget.fieldName()
  158. formDict["page"] = pageIndex
  159. }
  160. case "SignatureFields":
  161. if let signatureWidget = form as? CPDFSignatureWidgetAnnotation {
  162. formDict["type"] = "signaturesFields"
  163. formDict["title"] = signatureWidget.fieldName()
  164. formDict["page"] = pageIndex
  165. }
  166. default:
  167. print("Unhandled type: \(type)")
  168. }
  169. formDicts.append(formDict)
  170. }
  171. }
  172. }
  173. return formDicts
  174. }
  175. //MARK: - Private Methods
  176. func lowercaseFirstLetter(of string: String) -> String {
  177. guard !string.isEmpty else { return string }
  178. let lowercaseString = string.prefix(1).lowercased() + string.dropFirst()
  179. return lowercaseString
  180. }
  181. }