KMRedactTools.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // KMRedactTools.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/19.
  6. //
  7. import Cocoa
  8. class KMRedactTools: NSObject {
  9. class func eraserDocument(_ document: CPDFDocument, callback: @escaping (_ result: Bool, _ errorAnno: CPDFAnnotation?) -> ()) {
  10. for i in 0 ..< document.pageCount {
  11. guard let page: CPDFPage = document.page(at: i) else {
  12. continue
  13. }
  14. for anno in page.annotations {
  15. if ((anno.isKind(of: CPDFRedactAnnotation.self)) == false) {
  16. continue
  17. }
  18. let result = page.erasureRedact(from: anno.bounds)
  19. if (result == false) {
  20. callback(false, anno)
  21. return
  22. }
  23. }
  24. }
  25. callback(true, nil)
  26. }
  27. class func getPageIndexs(_ type: Int, string: String, _ pageCount: Int) -> IndexSet {
  28. var indexs: IndexSet = []
  29. if (type == 1) { /// 所有页面
  30. for i in 0 ..< pageCount {
  31. indexs.insert(i)
  32. }
  33. } else if (type == 2) { /// 奇数页
  34. for i in 0 ..< pageCount {
  35. if (i % 2 == 1) {
  36. continue
  37. }
  38. indexs.insert(i)
  39. }
  40. } else if (type == 3) { /// 偶数页
  41. for i in 0 ..< pageCount {
  42. if (i % 2 == 0) {
  43. continue
  44. }
  45. indexs.insert(i)
  46. }
  47. } else if (type == 4) {
  48. let array = KMPageRangeTools.findSelectPage(pageRangeString: string, pageCount: pageCount)
  49. for i in array {
  50. indexs.insert(i-1)
  51. }
  52. }
  53. return indexs
  54. }
  55. class func createRedactAnnotation(_ document: CPDFDocument, _ anno: CPDFRedactAnnotation) -> CPDFRedactAnnotation {
  56. let redactAnno = CPDFRedactAnnotation(document: document)
  57. redactAnno?.setInteriorColor(anno.interiorColor())
  58. redactAnno?.setBorderColor(anno.borderColor())
  59. redactAnno?.setOverlayText(anno.overlayText())
  60. redactAnno?.setFontColor(anno.fontColor())
  61. redactAnno?.setAlignment(anno.alignment())
  62. redactAnno?.setFont(anno.font())
  63. redactAnno?.bounds = anno.bounds
  64. return redactAnno!
  65. }
  66. }