123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- //
- // CPDFDocumentPlugin.swift
- // compdfkit_flutter
- //
- // Created by Xiaolong Liu on 2024/7/19.
- //
- import Foundation
- import ComPDFKit
- import Flutter
- import ComPDFKit_Tools
- public class CPDFDocumentPlugin {
-
- private var document : CPDFDocument?
-
- private var _methodChannel : FlutterMethodChannel
-
- private var pdfViewController : CPDFViewController?
-
-
- init(uid : String, binaryMessager : FlutterBinaryMessenger) {
- _methodChannel = FlutterMethodChannel(name: "com.compdfkit.flutter.document_\(uid)", binaryMessenger: binaryMessager)
- registeryMethodChannel()
- }
-
- init(pdfViewController : CPDFViewController, uid : String, binaryMessager : FlutterBinaryMessenger){
- self.pdfViewController = pdfViewController
- _methodChannel = FlutterMethodChannel(name: "com.compdfkit.flutter.document_\(uid)", binaryMessenger: binaryMessager)
- registeryMethodChannel()
- }
-
-
- private func registeryMethodChannel(){
- _methodChannel.setMethodCallHandler({
- (call: FlutterMethodCall, result: FlutterResult) -> Void in
- print("ComPDFKit-Flutter: iOS-MethodChannel: CPDFDocumentPlugin [method:\(call.method)]")
- if(self.document == nil && self.pdfViewController != nil){
- guard let pdfListView = self.pdfViewController?.pdfListView else {
- print("pdfViewController error")
- return
- }
- self.document = pdfListView.document
- }
- switch call.method {
- case "open_document":
- // TODO: 这里要再打开文档
- let initInfo = call.arguments as? [String: Any]
- let path = initInfo?["filePath"] as? String ?? ""
- let password = initInfo?["password"] ?? ""
-
- self.document = CPDFDocument(url: URL(fileURLWithPath: path))
- if(self.document?.isLocked == true){
- self.document?.unlock(withPassword: password as? String ?? "")
- }
- result(2)
- case "get_file_name":
-
- if(self.document == nil){
- print("self.document is nil")
- result("is nil")
- return
- }
- // TODO: 这里返回文档名称
- result("文档名称")
- case "is_encrypted":
- // TODO: 返回文档是否加密了,true:加密, false:未加密
- result(false)
- case "is_image_doc":
- // TODO: 是否为图片文档, true:是图片文档, false:非图片文档
- result(false)
- case "get_permissions":
- // TODO: 返回文档当前的权限,返回int类型。none:0, user:1, owner:2
- result(0)
- case "check_owner_unlocked":
- // TODO: 检查所有者权限是否已经解锁. 已解锁:true, 未解锁:false
- result(false)
- case "check_password":
- // TODO: 检查密码是否正确
- let info = call.arguments as? [String: Any]
- // flutter 层传过来的密码
- let password = initInfo?["password"] as? String ?? ""
- // 是否是权限密码
- let isOwnerPassword = initInfo?["isOwnerPassword"] as Bool ?? false
- // 返回密码是否正确
- result(true)
- case "close":
- // TODO: 关闭文档,释放资源
- result(true)
- case "has_change":
- // TODO: 返回文档是否有修改:true:有修改, false:未修改
- result(false)
- case "import_annotations":
- // TODO: 导入注释
- // 返回值:导入成功:true, 导入失败:false
- result(true)
- case "export_annotations":
- // TODO: 导出注释
- // 返回值:导出成功:导出的xfdf文件路径, 导出失败:空字符串
- result("")
- case "remove_all_annotations":
- // TODO: 删除所有注释
- // 返回值:删除成功:true, 删除失败:false
- result(true)
- case "get_page_count":
- // TODO: 返回当前文档总页数
- result(1)
- default:
- result(FlutterMethodNotImplemented)
- }
- });
-
- }
- }
|