KMRedactTools.swift 2.3 KB

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