forked from PSPDFKit/pspdfkit-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPspdfkitPlatformViewImpl.swift
More file actions
383 lines (337 loc) · 18.9 KB
/
PspdfkitPlatformViewImpl.swift
File metadata and controls
383 lines (337 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
// Copyright © 2024-2025 PSPDFKit GmbH. 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 PSPDFKIT LICENSE AGREEMENT.
// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
// This notice may not be removed from this file.
//
import Foundation
import PSPDFKit
@objc(PspdfkitPlatformViewImpl)
public class PspdfkitPlatformViewImpl: NSObject, PspdfkitWidgetControllerApi, PDFViewControllerDelegate {
private var pdfViewController: PDFViewController? = nil;
private var pspdfkitWidgetCallbacks: PspdfkitWidgetCallbacks? = nil;
private var viewId: String? = nil;
private var eventsHelper: FlutterEventsHelper? = nil;
@objc public func setViewController(controller: PDFViewController){
self.pdfViewController = controller
self.pdfViewController?.delegate = self
// Set the host view for the annotation toolbar controller
controller.annotationToolbarController?.updateHostView(nil, container: nil, viewController: controller)
}
public func pdfViewController(_ pdfController: PDFViewController, didChange document: Document?) {
if document != nil {
pspdfkitWidgetCallbacks?.onDocumentLoaded(documentId: document!.uid){ _ in }
} else {
pspdfkitWidgetCallbacks?.onDocumentError(documentId: "", error: "Loading Document failed") {_ in }
}
}
public func pdfViewController(_ pdfController: PDFViewController, didSelect annotations: [Annotation], on pageView: PDFPageView) {
// Call the event helper to notify the listeners.
eventsHelper?.annotationSelected(annotations: annotations)
}
public func pdfViewController(_ sender: PDFViewController, menuForText glyphs: GlyphSequence, onPageView pageView: PDFPageView, appearance: EditMenuAppearance, suggestedMenu: UIMenu) -> UIMenu {
return UIMenu(
title: "Custom Menu",
image: nil,
identifier: nil,
options: .displayInline,
children: [
UIAction(title: "Custom Action", image: nil, identifier: nil, discoverabilityTitle: nil, attributes: [], state: .off, handler: { _ in
print("Custom Action")
})
]
)
}
public func pdfViewController(_ pdfController: PDFViewController, didDeselect annotations: [Annotation], on pageView: PDFPageView) {
// Call the event helper to notify the listeners.
eventsHelper?.annotationDeselected(annotations: annotations)
}
public func pdfViewController(_ pdfController: PDFViewController, didSelectText text: String, with glyphs: [Glyph], at rect: CGRect, on pageView: PDFPageView) {
// Call the event helper to notify the listeners.
eventsHelper?.textSelected(text: text, glyphs: glyphs, rect: rect)
}
public func pdfViewController(_ pdfController: PDFViewController, didSave document: Document, error: (any Error)?) {
if let error = error {
pspdfkitWidgetCallbacks?.onDocumentError(documentId: document.uid, error: error.localizedDescription){_ in }
} else {
pspdfkitWidgetCallbacks?.onDocumentSaved(documentId: document.uid, path: document.fileURL?.absoluteString){_ in }
}
}
public func pdfViewController(_ pdfController: PDFViewController, didConfigurePageView pageView: PDFPageView, forPageAt pageIndex: Int) {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handlePageTap(_:)))
pageView.addGestureRecognizer(tapGesture)
}
@objc func handlePageTap(_ gesture: UITapGestureRecognizer) {
if let pageView = gesture.view as? PDFPageView {
let location = gesture.location(in: pageView)
let point: PointF = PointF(x: Double(Float(location.x)), y: Double(Float(location.y)))
let pageIndex = Int64(pageView.pageIndex)
pspdfkitWidgetCallbacks?.onPageClick(documentId: pdfViewController?.document?.uid ?? "", pageIndex: pageIndex, point: point, annotation: nil){_ in }
}
}
func setFormFieldValue(value: String, fullyQualifiedName: String, completion: @escaping (Result<Bool?, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let success = try PspdfkitFlutterHelper.setFormFieldValue(value, forFieldWithFullyQualifiedName: fullyQualifiedName, for: document)
completion(.success(success))
} catch {
completion(.failure(error))
}
}
func getFormFieldValue(fullyQualifiedName: String, completion: @escaping (Result<String?, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let value = try PspdfkitFlutterHelper.getFormFieldValue(forFieldWithFullyQualifiedName: fullyQualifiedName, for: document)
completion(.success(value as? String))
} catch {
completion(.failure(error))
}
}
func applyInstantJson(annotationsJson: String, completion: @escaping (Result<Bool?, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let success = try PspdfkitFlutterHelper.applyInstantJson(annotationsJson: annotationsJson, document: document)
pdfViewController!.reloadData()
completion(.success(success))
} catch {
completion(.failure(error))
}
}
func exportInstantJson(completion: @escaping (Result<String?, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let json = try PspdfkitFlutterHelper.exportInstantJson(document: document)
completion(.success(json))
} catch {
completion(.failure(error))
}
}
func addAnnotation(annotation jsonAnnotation: String, completion: @escaping (Result<Bool?, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let success = try PspdfkitFlutterHelper.addAnnotation(jsonAnnotation, for: document)
completion(.success(success))
} catch {
completion(.failure(error))
}
}
func removeAnnotation(annotation jsonAnnotation: String, completion: @escaping (Result<Bool?, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let success = try PspdfkitFlutterHelper.removeAnnotation(jsonAnnotation, for: document)
completion(.success(success))
} catch {
completion(.failure(error))
}
}
func getAnnotations(pageIndex: Int64, type: String, completion: @escaping (Result<Any, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let annotations = try PspdfkitFlutterHelper.getAnnotations(forPageIndex: PageIndex(pageIndex), andType: type, for: document)
completion(.success(annotations))
} catch {
completion(.failure(error))
}
}
func getAllUnsavedAnnotations(completion: @escaping (Result<Any, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let annotations = try PspdfkitFlutterHelper.getAllUnsavedAnnotations(for: document)
completion(.success(annotations))
} catch {
completion(.failure(error))
}
}
func processAnnotations(type: AnnotationType, processingMode: AnnotationProcessingMode, destinationPath: String, completion: @escaping (Result<Bool, any Error>) -> Void) {
do {
let success = try PspdfkitFlutterHelper.processAnnotations(ofType: "\(type)", withProcessingMode: "\(processingMode)", andDestinationPath: destinationPath, for: pdfViewController!)
completion(.success(success))
} catch {
completion(.failure(error))
}
}
func importXfdf(xfdfString: String, completion: @escaping (Result<Bool, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let success = try PspdfkitFlutterHelper.importXFDF(fromString: xfdfString, for: document)
completion(.success(success))
} catch {
completion(.failure(error))
}
}
func exportXfdf(xfdfPath: String, completion: @escaping (Result<Bool, any Error>) -> Void) {
do {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "PDF document not found or is invalid.", details: nil)))
return
}
let success = try PspdfkitFlutterHelper.exportXFDF(toPath: xfdfPath, for: document)
completion(.success(success))
} catch {
completion(.failure(error))
}
}
func save(completion: @escaping (Result<Bool, any Error>) -> Void) {
guard let document = pdfViewController?.document, document.isValid else {
completion(.failure(PspdfkitApiError(code: "", message: "Invalid PDF document.", details: nil )))
return
}
document.save() { Result in
if case .success = Result {
completion(.success(true))
} else {
let error = PspdfkitApiError(code: "", message: "Failed to save PDF document.", details: nil )
completion(.failure(error))
}
}
}
func setAnnotationConfigurations(configurations: [String : [String : Any]], completion: @escaping (Result<Bool?, any Error>) -> Void) {
AnnotationsPresetConfigurations.setConfigurations(annotationPreset: configurations)
}
func getVisibleRect(pageIndex: Int64, completion: @escaping (Result<PdfRect, any Error>) -> Void) {
guard let pdfViewController = pdfViewController else {
completion(.failure(NSError(domain: "PspdfkitPlatformViewImpl", code: -1, userInfo: [NSLocalizedDescriptionKey: "PDFViewController is not set."])))
return
}
let visibleRect = pdfViewController.viewState?.viewPort
if visibleRect == nil {
completion(.failure(NSError(domain: "PspdfkitPlatformViewImpl", code: -1, userInfo: [NSLocalizedDescriptionKey: "Visible rect is not set."])))
return
}
let result: PdfRect = PdfRect(x: visibleRect!.origin.x, y: visibleRect!.origin.y, width: visibleRect!.size.width, height: visibleRect!.size.height)
completion(.success(result))
}
func zoomToRect(pageIndex: Int64, rect: PdfRect, animated: Bool?, duration: Double?, completion: @escaping (Result<Bool, any Error>) -> Void) {
guard let pdfViewController = pdfViewController else {
completion(.failure(NSError(domain: "PspdfkitPlatformViewImpl", code: -1, userInfo: [NSLocalizedDescriptionKey: "PDFViewController is not set."])))
return
}
let rectToZoom = CGRect(x: rect.x, y: rect.y, width: rect.width, height: rect.height)
pdfViewController.documentViewController?.zoom(toPDFRect: rectToZoom, forPageAt: Int(pageIndex), animated: animated ?? true)
completion(.success(true))
}
func getZoomScale(pageIndex: Int64, completion: @escaping (Result<Double, any Error>) -> Void) {
// Not implemented for iOS.
let errormessage: String = "Not implemented for iOS."
completion(.failure(PspdfkitApiError(code: "", message: errormessage, details: nil)))
}
@objc public func onDocumentLoaded(documentId: String){
pspdfkitWidgetCallbacks?.onDocumentLoaded(documentId: documentId){_ in }
}
func addEventListener(event: NutrientEvent) throws {
eventsHelper?.setEventListener(event: event)
}
func removeEventListener(event: NutrientEvent) throws {
eventsHelper?.removeEventListener(event: event)
}
func enterAnnotationCreationMode(annotationTool: AnnotationTool?, completion: @escaping (Result<Bool?, Error>) -> Void) {
guard let pdfViewController = pdfViewController else {
completion(.failure(PspdfkitApiError(code: "error", message: "PDF view controller is null", details: nil)))
return
}
do {
if let annotationTool = annotationTool {
// Get the Flutter tool name
// Use AnnotationHelper to map the Flutter tool to iOS tool
if let toolWithVariant = AnnotationHelper.getIOSAnnotationToolWithVariantFromFlutterName(annotationTool) {
// Set the annotation tool
if pdfViewController.annotationToolbarController?.isToolbarVisible == false {
pdfViewController.annotationToolbarController?.showToolbar(animated: true)
}
pdfViewController.annotationStateManager.toggleState(toolWithVariant.annotationTool, variant: toolWithVariant.variant)
// Ensure the annotation toolbar is visible
completion(.success(true))
} else {
// Default to ink pen if the tool is not supported
let defaultTool = AnnotationToolWithVariant(annotationTool: .ink, variant: nil)
if pdfViewController.annotationToolbarController?.isToolbarVisible == false {
pdfViewController.annotationToolbarController?.showToolbar(animated: true)
}
pdfViewController.annotationStateManager.toggleState(defaultTool.annotationTool, variant: defaultTool.variant)
// Ensure the annotation toolbar is visible
completion(.success(true))
}
} else {
// Enter annotation creation mode with default tool (ink pen)
let defaultTool = AnnotationToolWithVariant(annotationTool: .ink, variant: nil)
if pdfViewController.annotationToolbarController?.isToolbarVisible == false {
pdfViewController.annotationToolbarController?.showToolbar(animated: true)
}
pdfViewController.annotationStateManager.toggleState(defaultTool.annotationTool, variant: defaultTool.variant)
completion(.success(true))
}
} catch {
completion(.failure(PspdfkitApiError(code: "error", message: "Error entering annotation creation mode: \(error.localizedDescription)", details: nil)))
}
}
func exitAnnotationCreationMode(completion: @escaping (Result<Bool?, Error>) -> Void) {
guard let pdfViewController = pdfViewController else {
completion(.failure(PspdfkitApiError(code: "error", message: "PDF view controller is null", details: nil)))
return
}
do {
// Exit annotation creation mode
if pdfViewController.annotationToolbarController?.isToolbarVisible == true {
pdfViewController.annotationToolbarController?.hideToolbar(animated: true)
}
pdfViewController.annotationStateManager.setState(nil, variant: nil)
completion(.success(true))
} catch {
completion(.failure(PspdfkitApiError(code: "error", message: "Error exiting annotation creation mode: \(error.localizedDescription)", details: nil)))
}
}
@objc func spreadIndexDidChange(_ notification: Notification) {
if let newSpreadIndex = notification.userInfo?["PSPDFDocumentViewControllerSpreadIndexKey"] as? Int,
let newPageIndex = self.pdfViewController?.documentViewController?.layout.pageRangeForSpread(at: newSpreadIndex).location {
pspdfkitWidgetCallbacks?.onPageChanged(documentId: self.pdfViewController?.document?.uid ?? "" , pageIndex:Int64(newPageIndex), completion: { _ in })
}
}
@objc public func register( binaryMessenger: FlutterBinaryMessenger, viewId: String){
self.viewId = viewId
pspdfkitWidgetCallbacks = PspdfkitWidgetCallbacks(binaryMessenger: binaryMessenger, messageChannelSuffix: "widget.callbacks.\(viewId)")
PspdfkitWidgetControllerApiSetup.setUp(binaryMessenger: binaryMessenger, api: self, messageChannelSuffix:viewId)
let nutreintEventCallback: NutrientEventsCallbacks = NutrientEventsCallbacks(binaryMessenger: binaryMessenger, messageChannelSuffix: "events.callbacks.\(viewId)")
eventsHelper = FlutterEventsHelper(nutrientCallback: nutreintEventCallback)
NotificationCenter.default.addObserver(self,
selector: #selector(spreadIndexDidChange(_:)),
name: .PSPDFDocumentViewControllerSpreadIndexDidChange,
object: nil)
}
@objc public func unRegister(binaryMessenger: FlutterBinaryMessenger){
NotificationCenter.default.removeObserver(self)
pspdfkitWidgetCallbacks = nil
PspdfkitWidgetControllerApiSetup.setUp(binaryMessenger: binaryMessenger, api: nil, messageChannelSuffix: viewId ?? "")
if eventsHelper != nil {
eventsHelper = nil
}
}
}