123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- //
- // CPDFDocument+PageEditManager.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2022/11/29.
- //
- import Foundation
- extension CPDFDocument {
- func getFileNameAccordingSelctPages(_ pages: [CPDFPage]) -> String {
- let docmentName = self.documentURL.lastPathComponent
- var pagesName: String = ""
- if pages.count > 1 {
- pagesName = " pages"
- } else {
- pagesName = " page"
- }
-
- var fileName: String = ""
- if pages.count > 0 {
- if pages.count == 1 {
- let idx = self.index(for: pages.first) + 1
- return String(format: "%@ %@ %@", docmentName, pagesName, String(format: "%ld", idx)).appending(".pdf")
- }
-
- var sortIndex = Set<Int>()
- for page in pages {
- let idx = index(for: page) + 1
- sortIndex.insert(Int(idx))
- }
- let sortArray = sortIndex.sorted()
-
- var a = 0
- var b = 0
-
- for num in sortArray {
- if fileName == "" {
- if num == b+1 {
- b = num
- if num == sortArray.last {
- fileName = fileName.appending(String(format: "%ld-%ld", a, b))
- }
- } else {
- if a == b {
- fileName = fileName.appending(String(format: "%ld,", a))
- } else {
- fileName = fileName.appending(String(format: "%ld-%ld,", a, b))
- }
- a = num; b = num
- if num == sortArray.last {
- fileName = fileName.appending(String(format: "%ld", a))
- }
- }
- } else {
- a = num; b = num
- }
- }
- if fileName.count > 50 {
- fileName = String(fileName.prefix(50))
- }
-
- fileName = String(format: "%@ %@ %@", docmentName, pagesName, fileName)
- return fileName.appending(".pdf")
- }
-
- return "Untitled.pdf"
- }
-
- func insertWithInsertPages(_ pages: [CPDFPage], insertPageIndex pageIndex: Int) -> Bool {
- var i = 0
- for (idx, page) in pages.enumerated() {
- let reulst = self.insertPageObject(page, at: UInt(pageIndex + i))
- i += 1
- }
- return true
- }
-
- func extractAsOneDocument(withPages pages: [CPDFPage], savePath: String?) -> Bool {
- let insertPages: [CPDFPage] = pages
- for i in 0...insertPages.count-1 {
- let page = pages[i]
-
- self.insertPageObject(page, at: self.pageCount)
- }
-
- var writeSuccess = self.write(toFile: savePath)
- return writeSuccess
- }
-
- func extractPerPageDocument(withPages pages: [CPDFPage], folerPath: String?) -> [URL]? {
- var successArray: [URL] = []
- for page in pages {
- var pdf = CPDFDocument.init()
-
- pdf?.insertPageObject(page, at: pdf!.pageCount)
- var outPath = folerPath ?? ""
- let documentFileName = String(self.documentURL.lastPathComponent.split(separator: ".")[0]) ?? NSLocalizedString("Untitled", comment: "")
-
- let dex = self.index(for: page) + 1
- outPath = URL(fileURLWithPath: outPath).appendingPathComponent(documentFileName).path
- outPath = String(format: "%@ page %ld", outPath, dex)
- outPath = URL(fileURLWithPath: outPath).appendingPathExtension("pdf").path
- var writeSuccess = pdf!.write(to: URL(fileURLWithPath: outPath))
- if !writeSuccess {
- writeSuccess = NSData(data: pdf!.dataRepresentation()).write(toFile: outPath, atomically: true)
- }
- if writeSuccess {
- successArray.append(URL(fileURLWithPath: outPath))
- }
- }
- return successArray
- }
-
- func splitByPagesWith(_ splitPageCount: Int, folerPath: String, fileName: String) -> [String]? {
- let pageCount = self.pageCount
- var fileCount = 0
- var j = 1
- var isSuccessfull = false
- var tdocument: CPDFDocument?
- var successArray: [String] = []
- for i in 0 ..< pageCount {
- if (j == 1) {
- fileCount += 1
- tdocument = CPDFDocument()
- let page = self.page(at: i)
- tdocument?.insertPageObject((page!), at: tdocument!.pageCount)
- if (j == splitPageCount) {
- let filePath = "\(folerPath)/\(fileName) \(fileCount).pdf"
- isSuccessfull = tdocument?.write(toFile: filePath) ?? false
- j = 1
- if (!isSuccessfull) {
- break
- } else {
- successArray.append(filePath)
- }
- } else{
- j += 1
- }
- } else if (j == splitPageCount) {
- j = 1
- let page = self.page(at: i)
- tdocument?.insertPageObject((page!), at: tdocument!.pageCount)
- let filePath = "\(folerPath)/\(fileName) \(fileCount).pdf"
- isSuccessfull = tdocument?.write(toFile: filePath) ?? false
- if (!isSuccessfull) {
- break
- } else {
- successArray.append(filePath)
- }
- } else {
- let page = self.page(at: i)
- tdocument?.insertPageObject((page!), at: tdocument!.pageCount)
- j += 1
- }
-
- if (i == pageCount-1) {
- if (tdocument != nil) {
- let filePath = "\(folerPath)/\(fileName) \(fileCount).pdf"
- isSuccessfull = tdocument?.write(toFile: filePath) ?? false
- if (!isSuccessfull) {
- break
- } else {
- successArray.append(filePath)
- }
- }
- }
- }
- if (isSuccessfull) {
- return successArray
- } else {
- return nil
- }
- }
-
- func splitByFileWith(_ splitFileCount: Int, folerPath: String, fileName: String) -> [String]? {
- let pageCount = self.pageCount
- var averagelyCount = Int(pageCount) / splitFileCount
- var fileCount = 0
- var j = 1
- var isSuccessfull = false
- var tdocument: CPDFDocument?
- var successArray: [String] = []
-
- for i in 0 ..< pageCount {
- if (j == 1) {
- fileCount += 1
- if (fileCount == splitFileCount) {
- averagelyCount = Int(pageCount)-averagelyCount*(fileCount-1)
- }
- tdocument = CPDFDocument()
- let page = self.page(at: i)
- tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
- if (j == averagelyCount) {
- let tPath = "\(folerPath)/\(fileName) \(fileCount).pdf"
-
- isSuccessfull = tdocument!.write(toFile: tPath)
- tdocument = nil
- j = 1
- if (!isSuccessfull) {
- break
- } else {
- successArray.append(tPath)
- }
- }else{
- j += 1
- }
- } else if (j == averagelyCount) {
- j = 1
- let page = self.page(at: i)
- tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
- let tPath = "\(folerPath)/\(fileName) \(fileCount).pdf"
-
- isSuccessfull = tdocument!.write(toFile: tPath)
- tdocument = nil
- if (!isSuccessfull) {
- break
- } else {
- successArray.append(tPath)
- }
- } else{
- let page = self.page(at: i)
-
- tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
- j += 1
- }
- if (i == pageCount-1) {
- if (tdocument != nil) {
- let tPath = "\(folerPath)/\(fileName) \(fileCount).pdf"
-
- isSuccessfull = tdocument!.write(toFile: tPath)
- tdocument = nil
- if (!isSuccessfull) {
- break
- } else {
- successArray.append(tPath)
- }
- }
- }
- }
- if (isSuccessfull) {
- return successArray
- } else {
- return nil
- }
- }
-
- func splitByFileWithPages(_ fileAttribute: KMFileAttribute, folerPath: String, fileName: String) -> [String]? {
- var isSuccessfull = false
- var successArray: [String] = []
-
- var pdfDocument1 = CPDFDocument()
- var pdfDocument2 = CPDFDocument()
- for i in 0 ..< self.pageCount {
- var isSelected = false
- for number in fileAttribute.fetchSelectPages() {
- if number == i+1 {
- isSelected = true
- if let page = self.page(at: i) {
- pdfDocument1?.insertPageObject(page, at: pdfDocument1?.pageCount ?? 0)
- }
- }
- }
- if (!isSelected) {
- if let page = self.page(at: i) {
- pdfDocument2?.insertPageObject(page, at: pdfDocument2?.pageCount ?? 0)
- }
- }
- }
- if let cnt = pdfDocument1?.pageCount, cnt > 0 {
- let tPath = "\(folerPath)/\(fileName) 1.pdf"
- isSuccessfull = pdfDocument1?.write(toFile: tPath) ?? false
- if (isSuccessfull) {
- successArray.append(tPath)
- } else {
- return nil
- }
- }
- if let cnt = pdfDocument2?.pageCount, cnt > 0 {
- let tPath = "\(folerPath)/\(fileName) 2.pdf"
- isSuccessfull = pdfDocument2?.write(toFile: tPath) ?? false
- if (isSuccessfull) {
- successArray.append(tPath)
- }
- }
-
- if (isSuccessfull) {
- return successArray
- } else {
- return nil
- }
- }
- }
- extension CPDFDocument {
- func insertBlankPage(pageSize: NSSize, at index: Int) -> Bool {
- return self.insertPage(pageSize, at: UInt(index))
- }
- }
|