CPDFDocumentPlugin.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // CPDFDocumentPlugin.swift
  3. // compdfkit_flutter
  4. //
  5. // Created by Xiaolong Liu on 2024/7/19.
  6. //
  7. import Foundation
  8. import ComPDFKit
  9. import Flutter
  10. import ComPDFKit_Tools
  11. public class CPDFDocumentPlugin {
  12. private var document : CPDFDocument?
  13. private var _methodChannel : FlutterMethodChannel
  14. private var pdfViewController : CPDFViewController?
  15. init(uid : String, binaryMessager : FlutterBinaryMessenger) {
  16. _methodChannel = FlutterMethodChannel(name: "com.compdfkit.flutter.document_\(uid)", binaryMessenger: binaryMessager)
  17. registeryMethodChannel()
  18. }
  19. init(pdfViewController : CPDFViewController, uid : String, binaryMessager : FlutterBinaryMessenger){
  20. self.pdfViewController = pdfViewController
  21. _methodChannel = FlutterMethodChannel(name: "com.compdfkit.flutter.document_\(uid)", binaryMessenger: binaryMessager)
  22. registeryMethodChannel()
  23. }
  24. private func registeryMethodChannel(){
  25. _methodChannel.setMethodCallHandler({
  26. (call: FlutterMethodCall, result: FlutterResult) -> Void in
  27. print("ComPDFKit-Flutter: iOS-MethodChannel: CPDFDocumentPlugin [method:\(call.method)]")
  28. if(self.document == nil && self.pdfViewController != nil){
  29. guard let pdfListView = self.pdfViewController?.pdfListView else {
  30. print("pdfViewController error")
  31. return
  32. }
  33. self.document = pdfListView.document
  34. }
  35. switch call.method {
  36. case "open_document":
  37. // TODO: 这里要再打开文档
  38. let initInfo = call.arguments as? [String: Any]
  39. let path = initInfo?["filePath"] as? String ?? ""
  40. let password = initInfo?["password"] ?? ""
  41. self.document = CPDFDocument(url: URL(fileURLWithPath: path))
  42. if(self.document?.isLocked == true){
  43. self.document?.unlock(withPassword: password as? String ?? "")
  44. }
  45. result(2)
  46. case "get_file_name":
  47. if(self.document == nil){
  48. print("self.document is nil")
  49. result("is nil")
  50. return
  51. }
  52. // TODO: 这里返回文档名称
  53. result("文档名称")
  54. case "is_encrypted":
  55. // TODO: 返回文档是否加密了,true:加密, false:未加密
  56. result(false)
  57. case "is_image_doc":
  58. // TODO: 是否为图片文档, true:是图片文档, false:非图片文档
  59. result(false)
  60. case "get_permissions":
  61. // TODO: 返回文档当前的权限,返回int类型。none:0, user:1, owner:2
  62. result(0)
  63. case "check_owner_unlocked":
  64. // TODO: 检查所有者权限是否已经解锁. 已解锁:true, 未解锁:false
  65. result(false)
  66. case "check_password":
  67. // TODO: 检查密码是否正确
  68. let info = call.arguments as? [String: Any]
  69. // flutter 层传过来的密码
  70. let password = initInfo?["password"] as? String ?? ""
  71. // 是否是权限密码
  72. let isOwnerPassword = initInfo?["isOwnerPassword"] as Bool ?? false
  73. // 返回密码是否正确
  74. result(true)
  75. case "close":
  76. // TODO: 关闭文档,释放资源
  77. result(true)
  78. case "has_change":
  79. // TODO: 返回文档是否有修改:true:有修改, false:未修改
  80. result(false)
  81. case "import_annotations":
  82. // TODO: 导入注释
  83. // 返回值:导入成功:true, 导入失败:false
  84. result(true)
  85. case "export_annotations":
  86. // TODO: 导出注释
  87. // 返回值:导出成功:导出的xfdf文件路径, 导出失败:空字符串
  88. result("")
  89. case "remove_all_annotations":
  90. // TODO: 删除所有注释
  91. // 返回值:删除成功:true, 删除失败:false
  92. result(true)
  93. case "get_page_count":
  94. // TODO: 返回当前文档总页数
  95. result(1)
  96. default:
  97. result(FlutterMethodNotImplemented)
  98. }
  99. });
  100. }
  101. }