CPDFDocumentPlugin.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // CPDFDocumentPlugin.swift
  3. // compdfkit_flutter
  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 Foundation
  11. import ComPDFKit
  12. import Flutter
  13. import ComPDFKit_Tools
  14. public class CPDFDocumentPlugin {
  15. private var document : CPDFDocument?
  16. public var _methodChannel : FlutterMethodChannel
  17. private var pdfViewController : CPDFViewController?
  18. init(uid : String, binaryMessager : FlutterBinaryMessenger) {
  19. _methodChannel = FlutterMethodChannel(name: "com.compdfkit.flutter.document_\(uid)", binaryMessenger: binaryMessager)
  20. registeryMethodChannel()
  21. }
  22. init(pdfViewController : CPDFViewController, uid : String, binaryMessager : FlutterBinaryMessenger){
  23. self.pdfViewController = pdfViewController
  24. _methodChannel = FlutterMethodChannel(name: "com.compdfkit.flutter.document_\(uid)", binaryMessenger: binaryMessager)
  25. registeryMethodChannel()
  26. }
  27. private func registeryMethodChannel(){
  28. _methodChannel.setMethodCallHandler({
  29. (call: FlutterMethodCall, result: FlutterResult) -> Void in
  30. print("ComPDFKit-Flutter: iOS-MethodChannel: CPDFDocumentPlugin [method:\(call.method)]")
  31. if(self.document == nil && self.pdfViewController != nil){
  32. guard let pdfListView = self.pdfViewController?.pdfListView else {
  33. print("pdfViewController error")
  34. return
  35. }
  36. self.document = pdfListView.document
  37. }
  38. switch call.method {
  39. case "open_document":
  40. // TODO: 这里要再打开文档
  41. let initInfo = call.arguments as? [String: Any]
  42. let path = initInfo?["filePath"] as? String ?? ""
  43. let password = initInfo?["password"] ?? ""
  44. self.document = CPDFDocument(url: URL(fileURLWithPath: path))
  45. if(self.document?.isLocked == true){
  46. self.document?.unlock(withPassword: password as? String ?? "")
  47. }
  48. self.pdfViewController?.pdfListView?.document = self.document
  49. self.pdfViewController?.pdfListView?.setNeedsDisplay()
  50. result(2)
  51. case "get_file_name":
  52. if(self.document == nil){
  53. print("self.document is nil")
  54. result("is nil")
  55. return
  56. }
  57. // TODO: 这里返回文档名称
  58. result("文档名称")
  59. case "is_encrypted":
  60. // TODO: 返回文档是否加密了,true:加密, false:未加密
  61. let isEncrypted = self.document?.isEncrypted ?? false
  62. result(isEncrypted)
  63. case "is_image_doc":
  64. // TODO: 是否为图片文档, true:是图片文档, false:非图片文档
  65. let isImageDoc = self.document?.isImageDocument() ?? false
  66. result(isImageDoc)
  67. case "get_permissions":
  68. // TODO: 返回文档当前的权限,返回int类型。none:0, user:1, owner:2
  69. let permissions = self.document?.permissionsStatus ?? .none
  70. switch permissions {
  71. case .none:
  72. result(0)
  73. case .user:
  74. result(1)
  75. case .owner:
  76. result(2)
  77. default:
  78. result(0)
  79. }
  80. case "check_owner_unlocked":
  81. // TODO: 检查所有者权限是否已经解锁. 已解锁:true, 未解锁:false
  82. let owner = self.document?.isCheckOwnerUnlocked() ?? false
  83. result(owner)
  84. case "check_password":
  85. // TODO: 检查密码是否正确
  86. let info = call.arguments as? [String: Any]
  87. // flutter 层传过来的密码
  88. let password = info?["password"] as? String ?? ""
  89. let isOwnerPassword = self.document?.checkOwnerPassword(password) ?? false
  90. result(isOwnerPassword)
  91. case "has_change":
  92. // TODO: 返回文档是否有修改:true:有修改, false:未修改
  93. let isModified = self.document?.isModified() ?? false
  94. result(isModified)
  95. case "import_annotations":
  96. // TODO: 导入注释
  97. // 返回值:导入成功:true, 导入失败:false
  98. let importPath = call.arguments as? String ?? ""
  99. let success = self.document?.importAnnotation(fromXFDFPath: importPath) ?? false
  100. if success {
  101. self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  102. }
  103. result(success)
  104. case "export_annotations":
  105. // TODO: 导出注释
  106. // 返回值:导出成功:导出的xfdf文件路径, 导出失败:空字符串
  107. let fileNameWithExtension = self.document?.documentURL.lastPathComponent ?? ""
  108. let fileName = (fileNameWithExtension as NSString).deletingPathExtension
  109. let documentFolder = NSHomeDirectory().appending("/Documents/\(fileName)_xfdf.xfdf")
  110. let succes = self.document?.exportAnnotation(toXFDFPath: documentFolder) ?? false
  111. if succes {
  112. result(documentFolder)
  113. } else {
  114. result("")
  115. }
  116. case "remove_all_annotations":
  117. // TODO: 删除所有注释
  118. // 返回值:删除成功:true, 删除失败:false
  119. let pageCount = self.document?.pageCount ?? 0
  120. for i in 0..<pageCount {
  121. let page = self.document?.page(at: i)
  122. page?.removeAllAnnotations()
  123. }
  124. self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  125. self.pdfViewController?.pdfListView?.updateActiveAnnotations([])
  126. result(true)
  127. case "get_page_count":
  128. // TODO: 返回当前文档总页数
  129. let count = self.document?.pageCount ?? 1
  130. result(count)
  131. default:
  132. result(FlutterMethodNotImplemented)
  133. }
  134. });
  135. }
  136. }