KMWatermarkPDFView.swift 29 KB

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