KMWatermarkPDFView.swift 29 KB

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