KMWatermarkPDFView.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. //
  2. // KMWatermarkPDFView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/19.
  6. //
  7. import Cocoa
  8. class KMWatermarkPDFView: CPDFView {
  9. // var watermarkModel: KMWatermarkModel!
  10. var watermark: KMWatermarkModel?
  11. var background: KMBackgroundModel?
  12. var headerFooter: KMHeaderFooterModel?
  13. override func draw(_ dirtyRect: NSRect) {
  14. super.draw(dirtyRect)
  15. }
  16. override func draw(_ page: CPDFPage!, to context: CGContext!) {
  17. KMPrint("...................................")
  18. if let watermark = self.watermark {
  19. if watermark.isFront {
  20. super.draw(page, to: context)
  21. if (self.needDraw(page)) {
  22. drawWatermarkPage(page, to: context)
  23. }
  24. } else {
  25. if (self.needDraw(page)) {
  26. drawWatermarkPage(page, to: context)
  27. }
  28. super.draw(page, to: context)
  29. }
  30. } else if let background = self.background {
  31. // if let context = NSGraphicsContext.current?.cgContext {
  32. drawBackgroundPage(page, to: context)
  33. super.draw(page, to: context)
  34. // }
  35. } else if let headerFooter = self.headerFooter {
  36. super.draw(page, to: context)
  37. // if let context = NSGraphicsContext.current?.cgContext {
  38. drawHeaderFooterPage(page, to: context)
  39. // }
  40. } else {
  41. super.draw(page, to: context)
  42. }
  43. }
  44. func needDraw(_ page: CPDFPage) -> Bool {
  45. guard let watermark = watermark else { return false}
  46. if (watermark.pageRangeType.rawValue == 0) {
  47. return true
  48. }
  49. // else if (self.watermarkModel.pageRangeType == 1) {
  50. // let array = self.watermarkModel.pagesString.components(separatedBy: ",")
  51. // let index: Int = Int(self.document.index(for: page))
  52. // if (!array.contains("\(index+1)")) {
  53. // return false
  54. // }
  55. // }
  56. else if (watermark.pageRangeType == .odd) {
  57. let index: Int = Int(self.document.index(for: page))
  58. return (index % 2 == 0)
  59. } else if (watermark.pageRangeType == .even) {
  60. let index: Int = Int(self.document.index(for: page))
  61. return (index % 2 == 1)
  62. } else if (watermark.pageRangeType == .other) {
  63. if (watermark.pagesString.isEmpty) {
  64. return false
  65. }
  66. let array = watermark.pagesString.components(separatedBy: ",")
  67. let index: Int = Int(self.document.index(for: page))
  68. if (!array.contains("\(index+1)")) {
  69. return false
  70. }
  71. }
  72. return true
  73. }
  74. func drawWatermarkPage(_ page: CPDFPage, to context: CGContext) {
  75. KMPrint("................................... 11")
  76. guard let watermark = watermark else { return}
  77. let pageBounds = page.bounds(for: .cropBox)
  78. let w: CGFloat = NSWidth(pageBounds)
  79. let h: CGFloat = NSHeight(pageBounds)
  80. let width: CGFloat = sqrt(w*w+h*h)
  81. let newRect: CGRect = CGRect(x: -(width-w)*0.5, y: -(width-h)*0.5, width: width, height: width)
  82. let new_w: CGFloat = NSWidth(newRect)
  83. let new_h: CGFloat = NSHeight(newRect)
  84. // NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  85. NSGraphicsContext.saveGraphicsState()
  86. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  87. page.transform(context, for: .cropBox)
  88. if (watermark.pagesString.count > 0) {
  89. let array = watermark.pagesString.components(separatedBy: ",")
  90. let index: Int = Int(self.document.index(for: page))
  91. if (!array.contains("\(index)")) {
  92. return
  93. }
  94. }
  95. if (!watermark.text.isEmpty) {
  96. var font = watermark.getTextFont()
  97. var color = watermark.getTextColor()
  98. var red: CGFloat = 0
  99. var green: CGFloat = 0
  100. var blue: CGFloat = 0
  101. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: nil)
  102. color = NSColor(red: red, green: green, blue: blue, alpha: watermark.opacity)
  103. var size = NSZeroSize
  104. let style = NSMutableParagraphStyle()
  105. style.alignment = watermark.textAligement
  106. style.lineBreakMode = .byCharWrapping
  107. let dict = [
  108. NSAttributedString.Key.paragraphStyle : style,
  109. NSAttributedString.Key.foregroundColor : color,
  110. NSAttributedString.Key.font : font as Any
  111. ] as [NSAttributedString.Key : Any]
  112. size = watermark.text.boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: dict).size
  113. let radian: CGFloat = watermark.rotation*(Double.pi/180.0)
  114. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  115. var rect:CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  116. if (watermark.isTilePage) {
  117. context.translateBy(x: w * 0.5, y: h * 0.5)
  118. context.concatenate(t)
  119. context.translateBy(x: -(w/2), y: -(h/2))
  120. let verticalWidth: CGFloat = size.width + watermark.tileHorizontalSpace
  121. let horizontalHeight: CGFloat = size.height + watermark.tileVerticalSpace
  122. let line: Int = Int(((new_h-watermark.tileVerticalSpace)/horizontalHeight)+1)
  123. let row: Int = Int(((new_w-watermark.tileHorizontalSpace)/verticalWidth)+1)
  124. let point: CGPoint = CGPoint(x: w*0.5-size.width*0.5+watermark.horizontalSpace, y: h*0.5-size.height*0.5+watermark.verticalSpace)
  125. for i in 0 ..< line {
  126. for j in 0 ..< row {
  127. watermark.text.draw(in: NSRect(x: point.x+CGFloat(j)*verticalWidth, y: point.y+CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  128. }
  129. }
  130. for i in 1 ..< line {
  131. for j in 0 ..< row {
  132. watermark.text.draw(in: NSRect(x: point.x+CGFloat(j)*verticalWidth, y: point.y-CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  133. }
  134. }
  135. for i in 0 ..< line {
  136. for j in 1 ..< row {
  137. watermark.text.draw(in: NSRect(x: point.x-CGFloat(j)*verticalWidth, y: point.y+CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  138. }
  139. }
  140. for i in 1 ..< line {
  141. for j in 1 ..< row {
  142. watermark.text.draw(in: NSRect(x: point.x-CGFloat(j)*verticalWidth, y: point.y-CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  143. }
  144. }
  145. } else {
  146. if (watermark.verticalMode == 0) {
  147. rect.origin.y = pageBounds.size.height-rect.size.height
  148. } else if (watermark.verticalMode == 1) {
  149. rect.origin.y = (pageBounds.size.height-rect.size.height) * 0.5
  150. } else {
  151. rect.origin.y = 0
  152. }
  153. if (watermark.horizontalMode == 0) {
  154. rect.origin.x = 0
  155. } else if (watermark.horizontalMode == 1) {
  156. rect.origin.x = (pageBounds.size.width-rect.size.width) * 0.5
  157. } else {
  158. rect.origin.x = pageBounds.size.width-rect.size.width
  159. }
  160. rect.origin.x += watermark.horizontalSpace
  161. rect.origin.y += watermark.verticalSpace
  162. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  163. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  164. context.rotate(by: radian)
  165. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  166. watermark.text.draw(in: rect, withAttributes: dict)
  167. }
  168. } else if (watermark.image != nil) {
  169. let tiffData = watermark.image.tiffRepresentation
  170. let bitmap: NSBitmapImageRep!
  171. bitmap = NSBitmapImageRep(data: tiffData!)
  172. let ciImage = CIImage(bitmapImageRep: bitmap)
  173. var size: NSSize = (ciImage?.extent.size)!
  174. size.width *= watermark.scale
  175. size.height *= watermark.scale
  176. let radian = watermark.rotation * (Double.pi / 180.0)
  177. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  178. var rect = NSMakeRect(0, 0, size.width, size.height)
  179. if (watermark.isTilePage) {
  180. context.translateBy(x: w/2,y: h/2)
  181. context.concatenate(t)
  182. context.translateBy(x: -(w/2), y: -(h/2))
  183. let verticalWidth: CGFloat = size.width + watermark.tileHorizontalSpace
  184. let horizontalHeight: CGFloat = size.height + watermark.tileVerticalSpace
  185. let line: Int = Int((new_h - watermark.tileVerticalSpace)/horizontalHeight + 1)
  186. let row: Int = Int((new_w - watermark.tileHorizontalSpace) / verticalWidth + 1)
  187. let point = NSPoint(x: w/2 - size.width/2, y: h/2 - size.height/2)
  188. for i in 0 ..< (line/2+1) {
  189. for j in 0 ..< row {
  190. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  191. watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: watermark.opacity)
  192. }
  193. }
  194. for i in 1 ..< (line/2+1) {
  195. for j in 0 ..< row {
  196. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  197. watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: watermark.opacity)
  198. }
  199. }
  200. for i in 0 ..< (line/2+1) {
  201. for j in 1 ..< row {
  202. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  203. watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: watermark.opacity)
  204. }
  205. }
  206. for i in 1 ..< (line/2+1) {
  207. for j in 1 ..< row {
  208. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  209. watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: watermark.opacity)
  210. }
  211. }
  212. } else {
  213. if (watermark.verticalMode == 0) {
  214. rect.origin.y = pageBounds.size.height-rect.size.height
  215. } else if (watermark.verticalMode == 1) {
  216. rect.origin.y = (pageBounds.size.height-rect.size.height) * 0.5
  217. } else {
  218. rect.origin.y = 0
  219. }
  220. if (watermark.horizontalMode == 0) {
  221. rect.origin.x = 0
  222. } else if (watermark.horizontalMode == 1) {
  223. rect.origin.x = (pageBounds.size.width-rect.size.width) * 0.5
  224. } else {
  225. rect.origin.x = pageBounds.size.width-rect.size.width
  226. }
  227. rect.origin.x += watermark.horizontalSpace
  228. rect.origin.y += watermark.verticalSpace
  229. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  230. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  231. context.rotate(by: radian)
  232. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  233. watermark.image.draw(in: rect, from: NSZeroRect, operation: .overlay, fraction: watermark.opacity)
  234. }
  235. }
  236. NSGraphicsContext.restoreGraphicsState()
  237. KMPrint("................................... 12")
  238. }
  239. func drawBackgroundPage(_ page: CPDFPage, to context: CGContext) {
  240. guard let background = background else { return}
  241. let pageBounds = page.bounds(for: .cropBox)
  242. if let currentContext = NSGraphicsContext.current {
  243. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  244. }
  245. NSGraphicsContext.saveGraphicsState()
  246. page.transform(context, for: .cropBox)
  247. if let backgroundColor = background.color,
  248. backgroundColor != NSColor.clear {
  249. var size = pageBounds.size
  250. size.width *= background.scale
  251. size.height *= background.scale
  252. let radian: CGFloat = background.rotation * (Double.pi/180.0)
  253. var transform = CGAffineTransform(rotationAngle: radian)
  254. var rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  255. rect = rect.applying(transform)
  256. if background.verticalMode == 0 {
  257. rect.origin.y = pageBounds.size.height - rect.size.height
  258. } else if background.verticalMode == 1 {
  259. rect.origin.y = (pageBounds.size.height - rect.size.height) / 2.0
  260. } else {
  261. rect.origin.y = 0
  262. }
  263. if background.horizontalMode == 0 {
  264. rect.origin.x = 0
  265. } else if background.horizontalMode == 1 {
  266. rect.origin.x = (pageBounds.size.width - rect.size.width) / 2.0
  267. } else {
  268. rect.origin.x = pageBounds.size.width - rect.size.width
  269. }
  270. rect.origin.y += background.verticalSpace
  271. rect.origin.x += background.horizontalSpace
  272. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  273. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  274. context.rotate(by: CGFloat(radian))
  275. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  276. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  277. if let color = background.color?.usingColorSpaceName(NSColorSpaceName.calibratedRGB) {
  278. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  279. }
  280. context.setFillColor(red: red, green: green, blue: blue, alpha: background.opacity ?? 1.0)
  281. context.fill(CGRect(x: rect.origin.x + (rect.size.width - size.width) / 2.0,
  282. y: rect.origin.y + (rect.size.height - size.height) / 2.0,
  283. width: size.width, height: size.height))
  284. } else if let backgroundImage = background.image {
  285. var size = backgroundImage.size
  286. if background.imagePath.extension.lowercased() == "pdf",
  287. FileManager.default.fileExists(atPath: background.imagePath) {
  288. // Handle PDF background image case
  289. } else {
  290. size = CGSize(width: size.width * 2, height: size.height * 2)
  291. }
  292. size.width *= background.scale
  293. size.height *= background.scale
  294. let radian: CGFloat = background.rotation * (Double.pi/180.0)
  295. var transform = CGAffineTransform(rotationAngle: radian)
  296. var rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  297. rect = rect.applying(transform)
  298. if background.verticalMode == 0 {
  299. rect.origin.y = pageBounds.size.height - rect.size.height
  300. } else if background.verticalMode == 1 {
  301. rect.origin.y = (pageBounds.size.height - rect.size.height) / 2.0
  302. } else {
  303. rect.origin.y = 0
  304. }
  305. if background.horizontalMode == 0 {
  306. rect.origin.x = 0
  307. } else if background.horizontalMode == 1 {
  308. rect.origin.x = (pageBounds.size.width - rect.size.width) / 2.0
  309. } else {
  310. rect.origin.x = pageBounds.size.width - rect.size.width
  311. }
  312. rect.origin.y += background.verticalSpace
  313. rect.origin.x += background.horizontalSpace
  314. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  315. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  316. context.rotate(by: radian)
  317. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  318. backgroundImage.draw(in: CGRect(x: rect.origin.x + (rect.size.width - size.width) / 2.0,
  319. y: rect.origin.y + (rect.size.height - size.height) / 2.0,
  320. width: size.width, height: size.height),
  321. from: NSZeroRect,
  322. operation: .sourceOver,
  323. fraction: background.opacity)
  324. }
  325. NSGraphicsContext.restoreGraphicsState()
  326. }
  327. func drawHeaderFooterPage(_ page: CPDFPage, to context: CGContext) {
  328. guard let headerFooter = headerFooter else { return }
  329. let pageBounds = page.bounds(for: .cropBox)
  330. let xMargin = 0.0//pageBounds.origin.x * 2
  331. let yMargin = 0.0//pageBounds.origin.y * 2
  332. NSGraphicsContext.saveGraphicsState()
  333. if context != nil {
  334. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  335. }
  336. page.transform(context, for: .cropBox)
  337. let color = headerFooter.getTextColor()
  338. let font = NSFont.boldSystemFont(ofSize: CGFloat(headerFooter.getTextFontSize()))
  339. var style = NSMutableParagraphStyle()
  340. style.alignment = .center
  341. style.lineBreakMode = .byCharWrapping
  342. var size = NSZeroSize
  343. var attributes: [NSAttributedString.Key: Any] = [
  344. .paragraphStyle: style,
  345. .foregroundColor: color,
  346. .font: font
  347. ]
  348. let index = self.document?.index(for: page) ?? 0
  349. var topOffset: CGFloat = 0
  350. var bottomOffset: CGFloat = 0
  351. let topLeftString = headerFooter.topLeftString
  352. var tString = replacingOldString(topLeftString, currentPage: Int(index))
  353. size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
  354. options: [.usesLineFragmentOrigin, .usesFontLeading],
  355. attributes: attributes).size
  356. var posY = min(pageBounds.size.height - (headerFooter.topMargin), pageBounds.size.height - size.height)
  357. tString.draw(in: CGRect(x: headerFooter.leftMargin + xMargin, y: posY + yMargin, width: size.width, height: size.height), withAttributes: attributes)
  358. let topCenterString = headerFooter.topCenterString
  359. tString = replacingOldString(topCenterString, currentPage: Int(index))
  360. size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
  361. options: [.usesLineFragmentOrigin, .usesFontLeading],
  362. attributes: attributes).size
  363. posY = min(pageBounds.size.height - (headerFooter.topMargin), pageBounds.size.height - size.height)
  364. tString.draw(in: CGRect(x: pageBounds.size.width/2 - size.width/2 + xMargin, y: posY + yMargin, width: size.width, height: size.height), withAttributes: attributes)
  365. let topRightString = headerFooter.topRightString
  366. tString = replacingOldString(topRightString, currentPage: Int(index))
  367. size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
  368. options: [.usesLineFragmentOrigin, .usesFontLeading],
  369. attributes: attributes).size
  370. posY = min(pageBounds.size.height - (headerFooter.topMargin), pageBounds.size.height - size.height)
  371. tString.draw(in: CGRect(x: pageBounds.size.width - (headerFooter.rightMargin) - size.width + xMargin,
  372. y: posY + yMargin,
  373. width: size.width,
  374. height: size.height),
  375. withAttributes: attributes)
  376. let bottomLeftString = headerFooter.bottomLeftString
  377. tString = replacingOldString(bottomLeftString, currentPage: Int(index))
  378. size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
  379. options: [.usesLineFragmentOrigin, .usesFontLeading],
  380. attributes: attributes).size
  381. posY = max((headerFooter.bottomMargin) - size.height, 0)
  382. tString.draw(in: CGRect(x: headerFooter.leftMargin + xMargin, y: posY + yMargin, width: size.width, height: size.height), withAttributes: attributes)
  383. let bottomCenterString = headerFooter.bottomCenterString
  384. tString = replacingOldString(bottomCenterString, currentPage: Int(index))
  385. size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
  386. options: [.usesLineFragmentOrigin, .usesFontLeading],
  387. attributes: attributes).size
  388. posY = max((headerFooter.bottomMargin) - size.height, 0)
  389. tString.draw(in: CGRect(x: pageBounds.size.width/2 - size.width/2 + xMargin,
  390. y: posY + yMargin,
  391. width: size.width,
  392. height: size.height),
  393. withAttributes: attributes)
  394. let bottomRightString = headerFooter.bottomRightString
  395. tString = replacingOldString(bottomRightString, currentPage: Int(index))
  396. size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
  397. options: [.usesLineFragmentOrigin, .usesFontLeading],
  398. attributes: attributes).size
  399. posY = max((headerFooter.bottomMargin) - size.height, 0)
  400. tString.draw(in: CGRect(x: pageBounds.size.width - (headerFooter.rightMargin) - size.width + xMargin,
  401. y: posY + yMargin,
  402. width: size.width,
  403. height: size.height),
  404. withAttributes: attributes)
  405. context.setStrokeColor(NSColor(red: 51.0/255.0, green: 186.0/255.0, blue: 234.0/255.0, alpha: 1.0).cgColor)
  406. context.setLineWidth(1.0)
  407. context.move(to: CGPoint(x: xMargin, y: headerFooter.bottomMargin + bottomOffset))
  408. context.addLine(to: CGPoint(x: xMargin + pageBounds.size.width, y: headerFooter.bottomMargin + bottomOffset))
  409. context.move(to: CGPoint(x: xMargin + headerFooter.leftMargin, y: 0))
  410. context.addLine(to: CGPoint(x: xMargin + headerFooter.leftMargin, y: pageBounds.size.height))
  411. context.move(to: CGPoint(x: xMargin + pageBounds.size.width - (headerFooter.rightMargin), y: 0))
  412. context.addLine(to: CGPoint(x: xMargin + pageBounds.size.width - (headerFooter.rightMargin), y: pageBounds.size.height))
  413. context.move(to: CGPoint(x: xMargin, y: pageBounds.size.height - (headerFooter.topMargin) - topOffset))
  414. context.addLine(to: CGPoint(x: xMargin + pageBounds.size.width, y: pageBounds.size.height - (headerFooter.topMargin) - topOffset))
  415. let arr: [CGFloat] = [8, 1]
  416. context.setLineDash(phase: 0, lengths: arr)
  417. context.drawPath(using: .stroke)
  418. NSGraphicsContext.restoreGraphicsState()
  419. }
  420. func replacingOldString(_ oldString: String?, currentPage page: Int) -> String {
  421. guard let oldString = oldString else {
  422. return ""
  423. }
  424. var newString = oldString
  425. let startString = self.headerFooter?.startString ?? ""
  426. var newPage = ""
  427. if let isBates = self.headerFooter?.isBates, isBates {
  428. let pattern = "<<#\\d*?#\\d*#.*?>>|<<#\\d*?#\\d*?>>"
  429. if let dates = match(pattern, in: oldString) {
  430. for result in dates {
  431. let resultStr = (oldString as NSString).substring(with: result.range)
  432. let newResultStr = (resultStr as NSString).substring(with: NSMakeRange(2, resultStr.count - 4))
  433. let array = newResultStr.components(separatedBy: "#")
  434. if array.count > 1 {
  435. let batesDigits = array[1]
  436. if array.count > 2 {
  437. let firstString = array[2]
  438. newPage = String(format: "%0*ld", batesDigits.count, page + (firstString as NSString).integerValue)
  439. }
  440. if array.count > 3 {
  441. newPage = "\(array[3])\(newPage)"
  442. }
  443. if array.count > 4 {
  444. newPage += array[4]
  445. }
  446. newString = newString.replacingOccurrences(of: resultStr, with: newPage)
  447. }
  448. }
  449. }
  450. } else {
  451. newPage = "\(page + (startString as NSString).integerValue)"
  452. if !startString.isEmpty {
  453. newString = KMWatermarkPDFView.convertViewPageFormat(newString, currentPage: newPage, pageCount: "\(self.document?.pageCount ?? UInt(0 + (startString as NSString).integerValue - 1))")
  454. }
  455. }
  456. newString = KMWatermarkPDFView.convertDateFormat(newString)
  457. return newString
  458. }
  459. func match(_ pattern: String, in testString: String) -> [NSTextCheckingResult]? {
  460. do {
  461. let regex = try NSRegularExpression(pattern: pattern, options: [])
  462. let results = regex.matches(in: testString, options: [], range: NSRange(location: 0, length: testString.count))
  463. return results
  464. } catch {
  465. return nil
  466. }
  467. }
  468. static func convertDateFormat(_ oldString: String) -> String {
  469. var newString = oldString
  470. for dateFormat in KMHeaderFooterManager.defaultManager.dateFormatArray {
  471. if newString.contains(dateFormat) {
  472. let formatString = dateFormat.replacingOccurrences(of: "m", with: "M")
  473. let replace = "<<\(dateFormat)>>"
  474. let date = Date()
  475. let dateFormatter = DateFormatter()
  476. dateFormatter.dateFormat = formatString
  477. let dateString = dateFormatter.string(from: date)
  478. newString = newString.replacingOccurrences(of: replace, with: dateString)
  479. }
  480. }
  481. return newString
  482. }
  483. static func convertViewPageFormat(_ oldString: String, currentPage: String, pageCount: String) -> String {
  484. var newString = oldString
  485. let pageFormatArray = ["1", "1 of n", "1/n", "Page 1", "Page 1 of n"]
  486. for pageFormat in pageFormatArray {
  487. let pageFormat = "<<\(pageFormat)>>"
  488. if newString.contains(pageFormat) {
  489. var tString: String?
  490. switch pageFormat {
  491. case "<<1>>":
  492. tString = currentPage
  493. case "<<1 of n>>":
  494. tString = "\(currentPage) of \(pageCount)"
  495. case "<<1/n>>":
  496. tString = "\(currentPage)/\(pageCount)"
  497. case "<<Page 1>>":
  498. tString = "Page \(currentPage)"
  499. case "<<Page 1 of n>>":
  500. tString = "Page \(currentPage) of \(pageCount)"
  501. default:
  502. break
  503. }
  504. if let tString = tString {
  505. newString = newString.replacingOccurrences(of: pageFormat, with: tString)
  506. }
  507. }
  508. }
  509. return newString
  510. }
  511. }