123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // 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)
- }
- }
|