PDFNativeViewFactory.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // PDFNativeViewFactory.swift
  3. // Runner
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import Flutter
  13. import UIKit
  14. import ComPDFKit
  15. class PDFLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
  16. private var messenger: FlutterBinaryMessenger
  17. init(messenger: FlutterBinaryMessenger) {
  18. self.messenger = messenger
  19. super.init()
  20. }
  21. func create(
  22. withFrame frame: CGRect,
  23. viewIdentifier viewId: Int64,
  24. arguments args: Any?
  25. ) -> FlutterPlatformView {
  26. return FLNativeView(
  27. frame: frame,
  28. viewIdentifier: viewId,
  29. arguments: args,
  30. binaryMessenger: messenger)
  31. }
  32. func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
  33. return FlutterStandardMessageCodec.sharedInstance()
  34. }
  35. }
  36. class FLNativeView: NSObject, FlutterPlatformView {
  37. private var _view: UIView
  38. // private var _pdfView:CPDFView
  39. private var pdfListView:CPDFListView
  40. // private var _pdfDocument:CPDFDocument
  41. private var pdfDocument:CPDFDocument
  42. func convertIntTocolor(_ color: Int) -> UIColor {
  43. let c: UInt32 = UInt32(color)
  44. let alpha = CGFloat(c >> 24) / 255.0
  45. let r = CGFloat((c & 0xff0000) >> 16) / 255.0
  46. let g = CGFloat((c & 0xff00) >> 8) / 255.0
  47. let b = CGFloat(c & 0xff) / 255.0
  48. return UIColor.init(red: r, green: g, blue: b, alpha: alpha)
  49. }
  50. init(
  51. frame: CGRect,
  52. viewIdentifier viewId: Int64,
  53. arguments args: Any?,
  54. binaryMessenger messenger: FlutterBinaryMessenger?
  55. ) {
  56. var arguments = args as! [String : AnyObject];
  57. let document = arguments["document"];
  58. let configuration = arguments["configuration"] as! [String: AnyObject];
  59. _view = UIView()
  60. self.pdfListView = CPDFListView(frame: CGRectMake(0, 0, UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height))
  61. let encodeString = document?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  62. let fileURL = URL(string:encodeString ?? "")
  63. self.pdfDocument = CPDFDocument(url: fileURL!)
  64. super.init()
  65. // iOS views can be created here
  66. createNativeView(view: _view)
  67. let channel = FlutterMethodChannel(name: "com.compdfkit.pdf.flutter.pdfview.settings", binaryMessenger: messenger!)
  68. channel.setMethodCallHandler{(call : FlutterMethodCall, result : @escaping FlutterResult) in
  69. if (call.method == "setAnnotAttribute"){
  70. let arguments = call.arguments as! [String:AnyObject];
  71. let annotType = arguments["annotType"] as! String
  72. print(annotType)
  73. if(annotType == "highlight"){
  74. let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject]
  75. let color = annotAttribute["color"] as! String
  76. let alpha = annotAttribute["alpha"] as! Int
  77. let mColor = UIColor.init(hex: color)
  78. CPDFKitConfig.sharedInstance().setHighlightAnnotationColor(mColor)
  79. CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0))
  80. self.pdfListView.annotationMode = .highlight
  81. }else if(annotType == "underline"){
  82. let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject]
  83. let color = annotAttribute["color"] as! String
  84. let alpha = annotAttribute["alpha"] as! Int
  85. let mColor = UIColor.init(hex: color)
  86. CPDFKitConfig.sharedInstance().setUnderlineAnnotationColor(mColor)
  87. CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0))
  88. self.pdfListView.annotationMode = .underline
  89. }else if(annotType == "strikeout"){
  90. let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject]
  91. let color = annotAttribute["color"] as! String
  92. let alpha = annotAttribute["alpha"] as! Int
  93. let mColor = UIColor.init(hex: color)
  94. CPDFKitConfig.sharedInstance().setStrikeoutAnnotationColor(mColor)
  95. CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0))
  96. self.pdfListView.annotationMode = .strikeout
  97. }else if(annotType == "squiggly"){
  98. let annotAttribute = arguments["annotAttribute"] as! [String:AnyObject]
  99. let color = annotAttribute["color"] as! String
  100. let alpha = annotAttribute["alpha"] as! Int
  101. let mColor = UIColor.init(hex: color)
  102. CPDFKitConfig.sharedInstance().setSquigglyAnnotationColor(mColor)
  103. CPDFKitConfig.sharedInstance().setMarkupAnnotationOpacity(CGFloat(Double(alpha)/255.0))
  104. self.pdfListView.annotationMode = .squiggly
  105. }else if(annotType == "ink"){
  106. // CPDFKitShareConfig.freehandAnnotationColor = vc.color;
  107. // CPDFKitShareConfig.freehandAnnotationOpacity = vc.opacity;
  108. // CPDFKitShareConfig.freehandAnnotationBorderWidth = vc.borderWidth;
  109. //0 PEN_STYLE_PENCIL || 1 PEN_STYLE_MIC
  110. // CPDFKitShareConfig.freehandPenType = vc.style;
  111. self.pdfListView.annotationMode = .ink
  112. }else if(annotType == "shape"){
  113. // circle square arrow line
  114. self.pdfListView.annotationMode = .arrow
  115. }else if(annotType == "freetext"){
  116. self.pdfListView.annotationMode = .freeText
  117. }else if(annotType == "signature"){
  118. let annotation = CPDFSignatureAnnotation.init(document: self.pdfListView.document)
  119. //get from flutterview
  120. let image = UIImage.init()
  121. annotation?.setImage(image)
  122. self.pdfListView.add(annotation)
  123. }else if(annotType == "stamp"){
  124. //translate to swift
  125. // if (self.isCreateFromToolbar) {
  126. // CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document image:image] autorelease];
  127. // self.pdfView.addAnnotation = annotation;
  128. // } else {
  129. // CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document image:image] autorelease];
  130. // CGRect bounds = annotation.bounds;
  131. // bounds.origin.x = self.pdfView.menuPoint.x-bounds.size.width/2.0;
  132. // bounds.origin.y = self.pdfView.menuPoint.y-bounds.size.height/2.0;
  133. // annotation.bounds = bounds;
  134. // [self.pdfView addAnnotation:annotation forPage:self.pdfView.menuPage];
  135. // }
  136. //image form flutter
  137. let image = UIImage.init()
  138. let annotation = CPDFStampAnnotation.init(document: self.pdfListView.document, image: image)
  139. self.pdfListView.add(annotation)
  140. //before or after
  141. self.pdfListView.annotationMode = .stamp
  142. }else if(annotType == "link"){
  143. self.pdfListView.annotationMode = .link
  144. }
  145. }else if(call.method == "setCurrentFocusedType"){
  146. var arguments = call.arguments as! [String:AnyObject];
  147. //add_annot
  148. let focusedType = arguments["focusedType"] as! String
  149. let touchMode = arguments["touchMode"]
  150. if(touchMode as! String == "add_annot"){
  151. self.pdfListView.textSelectionMode = true
  152. }else if(touchMode as! String == "browse"){
  153. self.pdfListView.annotationMode = .none
  154. }
  155. }
  156. }
  157. }
  158. func view() -> UIView {
  159. return _view
  160. }
  161. func createNativeView(view _view: UIView){
  162. _view.addSubview(self.pdfListView)
  163. self.pdfListView.document = self.pdfDocument
  164. }
  165. }
  166. extension UIColor{
  167. static var randomColor:UIColor{
  168. get{
  169. let red = CGFloat(arc4random()%256)/255.0
  170. let green = CGFloat(arc4random()%256)/255.0
  171. let blue = CGFloat(arc4random()%256)/255.0
  172. return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
  173. }
  174. }
  175. public convenience init(hex: String) {
  176. var red: CGFloat = 0.0
  177. var green: CGFloat = 0.0
  178. var blue: CGFloat = 0.0
  179. var alpha: CGFloat = 1.0
  180. var hex: String = hex
  181. if hex.hasPrefix("#") {
  182. let index = hex.index(hex.startIndex, offsetBy: 1)
  183. hex = String(hex[index...])
  184. }
  185. let scanner = Scanner(string: hex)
  186. var hexValue: CUnsignedLongLong = 0
  187. if scanner.scanHexInt64(&hexValue) {
  188. switch (hex.count) {
  189. case 3:
  190. red = CGFloat((hexValue & 0xF00) >> 8) / 15.0
  191. green = CGFloat((hexValue & 0x0F0) >> 4) / 15.0
  192. blue = CGFloat(hexValue & 0x00F) / 15.0
  193. case 4:
  194. red = CGFloat((hexValue & 0xF000) >> 12) / 15.0
  195. green = CGFloat((hexValue & 0x0F00) >> 8) / 15.0
  196. blue = CGFloat((hexValue & 0x00F0) >> 4) / 15.0
  197. alpha = CGFloat(hexValue & 0x000F) / 15.0
  198. case 6:
  199. red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
  200. green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
  201. blue = CGFloat(hexValue & 0x0000FF) / 255.0
  202. case 8:
  203. red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
  204. green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
  205. blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
  206. alpha = CGFloat(hexValue & 0x000000FF) / 255.0
  207. default:
  208. print("Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8", terminator: "")
  209. }
  210. } else {
  211. print("Scan hex error")
  212. }
  213. print(red)
  214. print(green)
  215. print(blue)
  216. print(alpha)
  217. self.init(red:red, green:green, blue:blue, alpha:alpha)
  218. }
  219. }