123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- //
- // CPDFDocumentPlugin.swift
- // compdfkit_flutter
- // Copyright © 2014-2023 PDF Technologies, Inc. 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 ComPDFKit 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 ComPDFKit
- import Flutter
- import ComPDFKit_Tools
- public class CPDFDocumentPlugin {
-
- private var document : CPDFDocument?
-
- public 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 ?? "")
- }
- self.pdfViewController?.pdfListView?.document = self.document
- self.pdfViewController?.pdfListView?.setNeedsDisplay()
- 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:未加密
- let isEncrypted = self.document?.isEncrypted ?? false
-
- result(isEncrypted)
- case "is_image_doc":
- // TODO: 是否为图片文档, true:是图片文档, false:非图片文档
- let isImageDoc = self.document?.isImageDocument() ?? false
-
- result(isImageDoc)
- case "get_permissions":
- // TODO: 返回文档当前的权限,返回int类型。none:0, user:1, owner:2
- let permissions = self.document?.permissionsStatus ?? .none
- switch permissions {
- case .none:
- result(0)
- case .user:
- result(1)
- case .owner:
- result(2)
-
- default:
- result(0)
- }
-
- case "check_owner_unlocked":
- // TODO: 检查所有者权限是否已经解锁. 已解锁:true, 未解锁:false
- let owner = self.document?.isCheckOwnerUnlocked() ?? false
-
- result(owner)
- case "check_password":
- // TODO: 检查密码是否正确
- let info = call.arguments as? [String: Any]
- // flutter 层传过来的密码
- let password = info?["password"] as? String ?? ""
-
- let isOwnerPassword = self.document?.checkOwnerPassword(password) ?? false
- result(isOwnerPassword)
- case "has_change":
- // TODO: 返回文档是否有修改:true:有修改, false:未修改
- let isModified = self.document?.isModified() ?? false
-
- result(isModified)
- case "import_annotations":
- // TODO: 导入注释
- // 返回值:导入成功:true, 导入失败:false
- let importPath = call.arguments as? String ?? ""
- let success = self.document?.importAnnotation(fromXFDFPath: importPath) ?? false
- if success {
- self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
- }
- result(success)
- case "export_annotations":
- // TODO: 导出注释
- // 返回值:导出成功:导出的xfdf文件路径, 导出失败:空字符串
- let fileNameWithExtension = self.document?.documentURL.lastPathComponent ?? ""
- let fileName = (fileNameWithExtension as NSString).deletingPathExtension
- let documentFolder = NSHomeDirectory().appending("/Documents/\(fileName)_xfdf.xfdf")
- let succes = self.document?.exportAnnotation(toXFDFPath: documentFolder) ?? false
- if succes {
- result(documentFolder)
- } else {
- result("")
- }
-
- case "remove_all_annotations":
- // TODO: 删除所有注释
- // 返回值:删除成功:true, 删除失败:false
- let pageCount = self.document?.pageCount ?? 0
- for i in 0..<pageCount {
- let page = self.document?.page(at: i)
- page?.removeAllAnnotations()
- }
- self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
- self.pdfViewController?.pdfListView?.updateActiveAnnotations([])
- result(true)
- case "get_page_count":
- // TODO: 返回当前文档总页数
- let count = self.document?.pageCount ?? 1
- result(count)
- default:
- result(FlutterMethodNotImplemented)
- }
- });
-
- }
- }
|