CPDFViewCtrlFactory.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // CPDFViewCtrlFactory.swift
  3. //
  4. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. //
  6. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. // This notice may not be removed from this file.
  10. import Flutter
  11. import UIKit
  12. import ComPDFKit
  13. import ComPDFKit_Tools
  14. class CPDFViewCtrlFactory: NSObject, FlutterPlatformViewFactory {
  15. private let messenger: FlutterBinaryMessenger
  16. init(messenger: FlutterBinaryMessenger) {
  17. self.messenger = messenger
  18. super.init()
  19. }
  20. func create(
  21. withFrame frame: CGRect,
  22. viewIdentifier viewId: Int64,
  23. arguments args: Any?
  24. ) -> FlutterPlatformView {
  25. return CPDFViewCtrlFlutter(
  26. frame: frame,
  27. viewIdentifier: viewId,
  28. arguments: args,
  29. binaryMessenger: messenger)
  30. }
  31. func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
  32. return FlutterStandardMessageCodec.sharedInstance()
  33. }
  34. }
  35. class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView, CPDFViewBaseControllerDelete {
  36. private var pdfViewController : CPDFViewController
  37. private var navigationController : CNavigationController
  38. init(
  39. frame: CGRect,
  40. viewIdentifier viewId: Int64,
  41. arguments args: Any?,
  42. binaryMessenger messenger: FlutterBinaryMessenger?
  43. ) {
  44. // Parses the document path, password, and configuration information
  45. let initInfo = args as? [String: Any]
  46. let jsonString = initInfo?["configuration"] ?? ""
  47. let password = initInfo?["password"] ?? ""
  48. let path = initInfo?["document"] as? String ?? ""
  49. let document = NSURL(fileURLWithPath: path)
  50. let fileManager = FileManager.default
  51. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  52. let fileName = document.lastPathComponent ?? ""
  53. let docsFilePath = samplesFilePath + "/" + fileName
  54. if !fileManager.fileExists(atPath: samplesFilePath) {
  55. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  56. }
  57. try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
  58. let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  59. let configuration = jsonDataParse.configuration
  60. // Create the pdfview controller view
  61. pdfViewController = CPDFViewController(filePath: docsFilePath, password: password as? String, configuration: configuration!)
  62. navigationController = CNavigationController(rootViewController: pdfViewController)
  63. navigationController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  64. navigationController.view.frame = frame
  65. var plugin = CPDFViewCtrlPlugin(viewId: viewId, binaryMessenger: messenger!, controller: pdfViewController)
  66. super.init()
  67. // Proxy set, but not used
  68. pdfViewController.delegate = self
  69. navigationController.setViewControllers([pdfViewController], animated: true)
  70. }
  71. func view() -> UIView {
  72. return navigationController.view
  73. }
  74. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  75. }
  76. }