|
@@ -0,0 +1,228 @@
|
|
|
+//
|
|
|
+// RCTCPDFPageUtil.swift
|
|
|
+// react-native-compdfkit-pdf
|
|
|
+//
|
|
|
+// Copyright © 2014-2025 PDF Technologies, Inc. All Rights Reserved.
|
|
|
+//
|
|
|
+// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
|
|
|
+// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
|
|
|
+// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
|
|
|
+// This notice may not be removed from this file.
|
|
|
+//
|
|
|
+
|
|
|
+import UIKit
|
|
|
+import ComPDFKit
|
|
|
+
|
|
|
+class RCTCPDFPageUtil: NSObject {
|
|
|
+
|
|
|
+ private var page: CPDFPage?
|
|
|
+
|
|
|
+ public var pageIndex: Int = 0
|
|
|
+
|
|
|
+ init(page: CPDFPage? = nil) {
|
|
|
+ self.page = page
|
|
|
+ }
|
|
|
+
|
|
|
+ //MARK: - Public Methods
|
|
|
+
|
|
|
+ func getAnnotations() -> [Dictionary<String, Any>] {
|
|
|
+ var annotaionDicts:[Dictionary<String, Any>] = []
|
|
|
+
|
|
|
+ let annoations = page?.annotations ?? []
|
|
|
+
|
|
|
+ for annoation in annoations {
|
|
|
+ var annotaionDict: [String : Any] = [:]
|
|
|
+
|
|
|
+ let type: String = annoation.type
|
|
|
+ if annoation.type == "Widget" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ switch type {
|
|
|
+ case "Highlight", "Squiggly", "Underline", "Strikeout":
|
|
|
+ if let markupAnnotation = annoation as? CPDFMarkupAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = markupAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = markupAnnotation.contents
|
|
|
+ }
|
|
|
+ case "Circle":
|
|
|
+ if let circleAnnotation = annoation as? CPDFCircleAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = circleAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = circleAnnotation.contents
|
|
|
+ }
|
|
|
+ case "Square":
|
|
|
+ if let squareAnnotation = annoation as? CPDFSquareAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = squareAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = squareAnnotation.contents
|
|
|
+ }
|
|
|
+ case "Line", "Arrow":
|
|
|
+ if let lineAnnotation = annoation as? CPDFLineAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = lineAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = lineAnnotation.contents
|
|
|
+ }
|
|
|
+ case "Freehand":
|
|
|
+ if let inkAnnotation = annoation as? CPDFInkAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = inkAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = inkAnnotation.contents
|
|
|
+ }
|
|
|
+
|
|
|
+ case "Note":
|
|
|
+ if let noteAnnotation = annoation as? CPDFTextAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = noteAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = noteAnnotation.contents
|
|
|
+ }
|
|
|
+
|
|
|
+ case "FreeText":
|
|
|
+ if let freeTextAnnotation = annoation as? CPDFFreeTextAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = freeTextAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = freeTextAnnotation.contents
|
|
|
+ }
|
|
|
+
|
|
|
+ case "Stamp", "Image":
|
|
|
+ if let stampAnnotation = annoation as? CPDFStampAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = stampAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = stampAnnotation.contents
|
|
|
+ }
|
|
|
+
|
|
|
+ case "Link":
|
|
|
+ if let linkAnnotation = annoation as? CPDFLinkAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = linkAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = linkAnnotation.contents
|
|
|
+ }
|
|
|
+
|
|
|
+ case "Media":
|
|
|
+ if let mediaAnnotation = annoation as? CPDFSoundAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: type)
|
|
|
+
|
|
|
+ annotaionDict["type"] = lowertype
|
|
|
+ annotaionDict["title"] = mediaAnnotation.userName()
|
|
|
+ annotaionDict["page"] = pageIndex
|
|
|
+ annotaionDict["content"] = mediaAnnotation.contents
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ print("Unhandled type: \(type)")
|
|
|
+ }
|
|
|
+
|
|
|
+ annotaionDicts.append(annotaionDict)
|
|
|
+ }
|
|
|
+
|
|
|
+ return annotaionDicts
|
|
|
+ }
|
|
|
+
|
|
|
+ func getForms() -> [Dictionary<String, Any>] {
|
|
|
+ var formDicts:[Dictionary<String, Any>] = []
|
|
|
+
|
|
|
+ let forms = page?.annotations ?? []
|
|
|
+
|
|
|
+ for form in forms {
|
|
|
+ var formDict: [String : Any] = [:]
|
|
|
+
|
|
|
+ let type: String = form.type
|
|
|
+ if form.type == "Widget" {
|
|
|
+ if let widgetAnnotation = form as? CPDFWidgetAnnotation {
|
|
|
+ let widgetType: String = widgetAnnotation.widgetType
|
|
|
+
|
|
|
+ switch widgetType {
|
|
|
+ case "CheckBox", "RadioButton", "PushButton":
|
|
|
+ if let buttonWidget = form as? CPDFButtonWidgetAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: widgetType)
|
|
|
+
|
|
|
+ formDict["type"] = lowertype
|
|
|
+ formDict["title"] = buttonWidget.fieldName()
|
|
|
+ formDict["page"] = pageIndex
|
|
|
+ if widgetType != "PushButton" {
|
|
|
+ let isOn = buttonWidget.state()
|
|
|
+ if (isOn != 0) {
|
|
|
+ formDict["isChecked"] = true
|
|
|
+ } else {
|
|
|
+ formDict["isChecked"] = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ case "TextField":
|
|
|
+ if let textFieldWidget = form as? CPDFTextWidgetAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: widgetType)
|
|
|
+
|
|
|
+ formDict["type"] = lowertype
|
|
|
+ formDict["title"] = textFieldWidget.fieldName()
|
|
|
+ formDict["page"] = pageIndex
|
|
|
+ formDict["text"] = textFieldWidget.stringValue
|
|
|
+ }
|
|
|
+
|
|
|
+ case "ListBox", "ComboBox":
|
|
|
+ if let choiceWidget = form as? CPDFChoiceWidgetAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: widgetType)
|
|
|
+
|
|
|
+ formDict["type"] = lowertype
|
|
|
+ formDict["title"] = choiceWidget.fieldName()
|
|
|
+ formDict["page"] = pageIndex
|
|
|
+ }
|
|
|
+
|
|
|
+ case "SignatureFields":
|
|
|
+ if let signatureWidget = form as? CPDFSignatureWidgetAnnotation {
|
|
|
+ let lowertype = lowercaseFirstLetter(of: widgetType)
|
|
|
+
|
|
|
+ formDict["type"] = lowertype
|
|
|
+ formDict["title"] = signatureWidget.fieldName()
|
|
|
+ formDict["page"] = pageIndex
|
|
|
+ }
|
|
|
+
|
|
|
+ default:
|
|
|
+ print("Unhandled type: \(type)")
|
|
|
+ }
|
|
|
+
|
|
|
+ formDicts.append(formDict)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return formDicts
|
|
|
+ }
|
|
|
+
|
|
|
+ //MARK: - Private Methods
|
|
|
+
|
|
|
+ func lowercaseFirstLetter(of string: String) -> String {
|
|
|
+ guard !string.isEmpty else { return string }
|
|
|
+
|
|
|
+ let lowercaseString = string.prefix(1).lowercased() + string.dropFirst()
|
|
|
+
|
|
|
+ return lowercaseString
|
|
|
+ }
|
|
|
+
|
|
|
+}
|