// // PDFNativeViewFactory.swift // Runner // // 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 Flutter import UIKit import ComPDFKit class PDFLNativeViewFactory: NSObject, FlutterPlatformViewFactory { private var messenger: FlutterBinaryMessenger init(messenger: FlutterBinaryMessenger) { self.messenger = messenger super.init() } func create( withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any? ) -> FlutterPlatformView { return FLNativeView( frame: frame, viewIdentifier: viewId, arguments: args, binaryMessenger: messenger) } func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol { return FlutterStandardMessageCodec.sharedInstance() } } class FLNativeView: NSObject, FlutterPlatformView { private var _view: UIView // private var _pdfView:CPDFView private var pdfListView:CPDFListView // private var _pdfDocument:CPDFDocument private var pdfDocument:CPDFDocument func convertIntTocolor(_ color: Int) -> UIColor { let c: UInt32 = UInt32(color) let alpha = CGFloat(c >> 24) / 255.0 let r = CGFloat((c & 0xff0000) >> 16) / 255.0 let g = CGFloat((c & 0xff00) >> 8) / 255.0 let b = CGFloat(c & 0xff) / 255.0 return UIColor.init(red: r, green: g, blue: b, alpha: alpha) } init( frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?, binaryMessenger messenger: FlutterBinaryMessenger? ) { var arguments = args as! [String : AnyObject]; let document = arguments["document"]; let configuration = arguments["configuration"] as! [String: AnyObject]; _view = UIView() self.pdfListView = CPDFListView(frame: CGRectMake(0, 0, UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)) let encodeString = document?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) let fileURL = URL(string:encodeString ?? "") self.pdfDocument = CPDFDocument(url: fileURL!) super.init() // iOS views can be created here createNativeView(view: _view) let channel = FlutterMethodChannel(name: "com.compdfkit.pdf.flutter.pdfview.settings", binaryMessenger: messenger!) channel.setMethodCallHandler{(call : FlutterMethodCall, result : @escaping FlutterResult) in if (call.method == "setAnnotAttribute"){ let arguments = call.arguments as! [String:AnyObject]; let annotType = arguments["annotType"] as! String print(annotType) if(annotType == "highlight"){ let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject] let color = annotAttribute["color"] as! String let alpha = annotAttribute["alpha"] as! Int let mColor = UIColor.init(hex: color) CPDFKitConfig.sharedInstance().setHighlightAnnotationColor(mColor) CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0)) self.pdfListView.annotationMode = .highlight }else if(annotType == "underline"){ let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject] let color = annotAttribute["color"] as! String let alpha = annotAttribute["alpha"] as! Int let mColor = UIColor.init(hex: color) CPDFKitConfig.sharedInstance().setUnderlineAnnotationColor(mColor) CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0)) self.pdfListView.annotationMode = .underline }else if(annotType == "strikeout"){ let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject] let color = annotAttribute["color"] as! String let alpha = annotAttribute["alpha"] as! Int let mColor = UIColor.init(hex: color) CPDFKitConfig.sharedInstance().setStrikeoutAnnotationColor(mColor) CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0)) self.pdfListView.annotationMode = .strikeout }else if(annotType == "squiggly"){ let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject] let color = annotAttribute["color"] as! String let alpha = annotAttribute["alpha"] as! Int let mColor = UIColor.init(hex: color) CPDFKitConfig.sharedInstance().setSquigglyAnnotationColor(mColor) CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0)) self.pdfListView.annotationMode = .squiggly }else if(annotType == "ink"){ // CPDFKitShareConfig.freehandAnnotationColor = vc.color; // CPDFKitShareConfig.freehandAnnotationOpacity = vc.opacity; // CPDFKitShareConfig.freehandAnnotationBorderWidth = vc.borderWidth; //0 PEN_STYLE_PENCIL || 1 PEN_STYLE_MIC // CPDFKitShareConfig.freehandPenType = vc.style; self.pdfListView.annotationMode = .ink }else if(annotType == "shape"){ // circle square arrow line self.pdfListView.annotationMode = .arrow }else if(annotType == "freetext"){ self.pdfListView.annotationMode = .freeText }else if(annotType == "signature"){ let annotation = CPDFSignatureAnnotation.init(document: self.pdfListView.document) //get from flutterview let image = UIImage.init() annotation?.setImage(image) self.pdfListView.add(annotation) }else if(annotType == "stamp"){ //translate to swift // if (self.isCreateFromToolbar) { // CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document image:image] autorelease]; // self.pdfView.addAnnotation = annotation; // } else { // CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document image:image] autorelease]; // CGRect bounds = annotation.bounds; // bounds.origin.x = self.pdfView.menuPoint.x-bounds.size.width/2.0; // bounds.origin.y = self.pdfView.menuPoint.y-bounds.size.height/2.0; // annotation.bounds = bounds; // [self.pdfView addAnnotation:annotation forPage:self.pdfView.menuPage]; // } //image form flutter let image = UIImage.init() let annotation = CPDFStampAnnotation.init(document: self.pdfListView.document, image: image) self.pdfListView.add(annotation) //before or after self.pdfListView.annotationMode = .stamp }else if(annotType == "link"){ self.pdfListView.annotationMode = .link } }else if(call.method == "setCurrentFocusedType"){ var arguments = call.arguments as! [String:AnyObject]; //add_annot let focusedType = arguments["focusedType"] as! String let touchMode = arguments["touchMode"] if(touchMode as! String == "add_annot"){ self.pdfListView.textSelectionMode = true }else if(touchMode as! String == "browse"){ self.pdfListView.annotationMode = .none } } } } func view() -> UIView { return _view } func createNativeView(view _view: UIView){ _view.addSubview(self.pdfListView) self.pdfListView.document = self.pdfDocument } } extension UIColor{ static var randomColor:UIColor{ get{ let red = CGFloat(arc4random()%256)/255.0 let green = CGFloat(arc4random()%256)/255.0 let blue = CGFloat(arc4random()%256)/255.0 return UIColor(red: red, green: green, blue: blue, alpha: 1.0) } } public convenience init(hex: String) { var red: CGFloat = 0.0 var green: CGFloat = 0.0 var blue: CGFloat = 0.0 var alpha: CGFloat = 1.0 var hex: String = hex if hex.hasPrefix("#") { let index = hex.index(hex.startIndex, offsetBy: 1) hex = String(hex[index...]) } let scanner = Scanner(string: hex) var hexValue: CUnsignedLongLong = 0 if scanner.scanHexInt64(&hexValue) { switch (hex.count) { case 3: red = CGFloat((hexValue & 0xF00) >> 8) / 15.0 green = CGFloat((hexValue & 0x0F0) >> 4) / 15.0 blue = CGFloat(hexValue & 0x00F) / 15.0 case 4: red = CGFloat((hexValue & 0xF000) >> 12) / 15.0 green = CGFloat((hexValue & 0x0F00) >> 8) / 15.0 blue = CGFloat((hexValue & 0x00F0) >> 4) / 15.0 alpha = CGFloat(hexValue & 0x000F) / 15.0 case 6: red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0 green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0 blue = CGFloat(hexValue & 0x0000FF) / 255.0 case 8: red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0 green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0 blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0 alpha = CGFloat(hexValue & 0x000000FF) / 255.0 default: print("Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8", terminator: "") } } else { print("Scan hex error") } print(red) print(green) print(blue) print(alpha) self.init(red:red, green:green, blue:blue, alpha:alpha) } }