123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- //
- // 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 {
- annotaionDict["type"] = "ink"
- 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
- if type == "Image" {
- annotaionDict["type"] = "pictures"
- }
- 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 {
- formDict["type"] = "signaturesFields"
- 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
- }
-
- }
|