// // KMRedactTools.swift // PDF Master // // Created by tangchao on 2023/1/19. // import Cocoa class KMRedactTools: NSObject { class func eraserDocument(_ document: CPDFDocument, callback: @escaping (_ result: Bool, _ errorAnno: CPDFAnnotation?) -> ()) { for i in 0 ..< document.pageCount { let page: CPDFPage = document.page(at: i) for anno in page.annotations { if ((anno.isKind(of: CPDFRedactAnnotation.self)) == false) { continue } let result = page.erasureRedact(from: anno.bounds) if (result == false) { callback(false, anno) return } } } callback(true, nil) } class func getPageIndexs(_ type: Int, string: String, _ pageCount: Int) -> IndexSet { var indexs: IndexSet = [] if (type == 1) { /// 所有页面 for i in 0 ..< pageCount { indexs.insert(i) } } else if (type == 2) { /// 奇数页 for i in 0 ..< pageCount { if (i % 2 == 1) { continue } indexs.insert(i) } } else if (type == 3) { /// 偶数页 for i in 0 ..< pageCount { if (i % 2 == 0) { continue } indexs.insert(i) } } else if (type == 4) { let array = KMPageRangeTools.findSelectPage(pageRangeString: string, pageCount: pageCount) for i in array { indexs.insert(i-1) } } return indexs } class func createRedactAnnotation(_ document: CPDFDocument, _ anno: CPDFRedactAnnotation) -> CPDFRedactAnnotation { let redactAnno = CPDFRedactAnnotation(document: document) redactAnno?.setInteriorColor(anno.interiorColor()) redactAnno?.setBorderColor(anno.borderColor()) redactAnno?.setOverlayText(anno.overlayText()) redactAnno?.setFontColor(anno.fontColor()) redactAnno?.setAlignment(anno.alignment()) redactAnno?.setFont(anno.font()) redactAnno?.bounds = anno.bounds return redactAnno! } }