CPDFDocument+PageEditManager.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // CPDFDocument+PageEditManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/11/29.
  6. //
  7. import Foundation
  8. extension CPDFDocument {
  9. func getFileNameAccordingSelctPages(_ pages: [CPDFPage]) -> String {
  10. let docmentName = self.documentURL.lastPathComponent
  11. var pagesName: String = ""
  12. if pages.count > 1 {
  13. pagesName = " pages"
  14. } else {
  15. pagesName = " page"
  16. }
  17. var fileName: String = ""
  18. if pages.count > 0 {
  19. if pages.count == 1 {
  20. let idx = self.index(for: pages.first) + 1
  21. return String(format: "%@ %@ %@", docmentName, pagesName, String(format: "%ld", idx)).appending(".pdf")
  22. }
  23. var sortIndex = Set<Int>()
  24. for page in pages {
  25. let idx = index(for: page) + 1
  26. sortIndex.insert(Int(idx))
  27. }
  28. let sortArray = sortIndex.sorted()
  29. var a = 0
  30. var b = 0
  31. for num in sortArray {
  32. if fileName == "" {
  33. if num == b+1 {
  34. b = num
  35. if num == sortArray.last {
  36. fileName = fileName.appending(String(format: "%ld-%ld", a, b))
  37. }
  38. } else {
  39. if a == b {
  40. fileName = fileName.appending(String(format: "%ld,", a))
  41. } else {
  42. fileName = fileName.appending(String(format: "%ld-%ld,", a, b))
  43. }
  44. a = num; b = num
  45. if num == sortArray.last {
  46. fileName = fileName.appending(String(format: "%ld", a))
  47. }
  48. }
  49. } else {
  50. a = num; b = num
  51. }
  52. }
  53. if fileName.count > 50 {
  54. fileName = String(fileName.prefix(50))
  55. }
  56. fileName = String(format: "%@ %@ %@", docmentName, pagesName, fileName)
  57. return fileName.appending(".pdf")
  58. }
  59. return "Untitled.pdf"
  60. }
  61. func insertWithInsertPages(_ pages: [CPDFPage], insertPageIndex pageIndex: Int) -> Bool {
  62. var i = 0
  63. for (idx, page) in pages.enumerated() {
  64. let reulst = self.insertPageObject(page, at: UInt(pageIndex + i))
  65. i += 1
  66. }
  67. return true
  68. }
  69. func extractAsOneDocument(withPages pages: [CPDFPage], savePath: String?) -> Bool {
  70. let insertPages: [CPDFPage] = pages
  71. for i in 0...insertPages.count-1 {
  72. let page = pages[i]
  73. self.insertPageObject(page, at: self.pageCount)
  74. }
  75. var writeSuccess = self.write(toFile: savePath)
  76. return writeSuccess
  77. }
  78. func extractPerPageDocument(withPages pages: [CPDFPage], folerPath: String?) -> [URL]? {
  79. var successArray: [URL] = []
  80. for page in pages {
  81. var pdf = CPDFDocument.init()
  82. pdf?.insertPageObject(page, at: pdf!.pageCount)
  83. var outPath = folerPath ?? ""
  84. let documentFileName = String(self.documentURL.lastPathComponent.split(separator: ".")[0]) ?? NSLocalizedString("Untitled", comment: "")
  85. let dex = self.index(for: page) + 1
  86. outPath = URL(fileURLWithPath: outPath).appendingPathComponent(documentFileName).path
  87. outPath = String(format: "%@ page %ld", outPath, dex)
  88. outPath = URL(fileURLWithPath: outPath).appendingPathExtension("pdf").path
  89. var writeSuccess = pdf!.write(to: URL(fileURLWithPath: outPath))
  90. if !writeSuccess {
  91. writeSuccess = NSData(data: pdf!.dataRepresentation()).write(toFile: outPath, atomically: true)
  92. }
  93. if writeSuccess {
  94. successArray.append(URL(fileURLWithPath: outPath))
  95. }
  96. }
  97. return successArray
  98. }
  99. func splitByPagesWith(_ splitPageCount: Int, folerPath: String, fileName: String) -> [String]? {
  100. let pageCount = self.pageCount
  101. var fileCount = 0
  102. var j = 1
  103. var isSuccessfull = false
  104. var tdocument: CPDFDocument?
  105. var successArray: [String] = []
  106. for i in 0 ..< pageCount {
  107. if (j == 1) {
  108. fileCount += 1
  109. tdocument = CPDFDocument()
  110. let page = self.page(at: i)
  111. tdocument?.insertPageObject((page!), at: tdocument!.pageCount)
  112. if (j == splitPageCount) {
  113. let filePath = "\(folerPath)/\(fileName) \(fileCount).pdf"
  114. isSuccessfull = tdocument?.write(toFile: filePath) ?? false
  115. j = 1
  116. if (!isSuccessfull) {
  117. break
  118. } else {
  119. successArray.append(filePath)
  120. }
  121. } else{
  122. j += 1
  123. }
  124. } else if (j == splitPageCount) {
  125. j = 1
  126. let page = self.page(at: i)
  127. tdocument?.insertPageObject((page!), at: tdocument!.pageCount)
  128. let filePath = "\(folerPath)/\(fileName) \(fileCount).pdf"
  129. isSuccessfull = tdocument?.write(toFile: filePath) ?? false
  130. if (!isSuccessfull) {
  131. break
  132. } else {
  133. successArray.append(filePath)
  134. }
  135. } else {
  136. let page = self.page(at: i)
  137. tdocument?.insertPageObject((page!), at: tdocument!.pageCount)
  138. j += 1
  139. }
  140. if (i == pageCount-1) {
  141. if (tdocument != nil) {
  142. let filePath = "\(folerPath)/\(fileName) \(fileCount).pdf"
  143. isSuccessfull = tdocument?.write(toFile: filePath) ?? false
  144. if (!isSuccessfull) {
  145. break
  146. } else {
  147. successArray.append(filePath)
  148. }
  149. }
  150. }
  151. }
  152. if (isSuccessfull) {
  153. return successArray
  154. } else {
  155. return nil
  156. }
  157. }
  158. func splitByFileWith(_ splitFileCount: Int, folerPath: String, fileName: String) -> [String]? {
  159. let pageCount = self.pageCount
  160. var averagelyCount = Int(pageCount) / splitFileCount
  161. var fileCount = 0
  162. var j = 1
  163. var isSuccessfull = false
  164. var tdocument: CPDFDocument?
  165. var successArray: [String] = []
  166. for i in 0 ..< pageCount {
  167. if (j == 1) {
  168. fileCount += 1
  169. if (fileCount == splitFileCount) {
  170. averagelyCount = Int(pageCount)-averagelyCount*(fileCount-1)
  171. }
  172. tdocument = CPDFDocument()
  173. let page = self.page(at: i)
  174. tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
  175. if (j == averagelyCount) {
  176. let tPath = "\(folerPath)/\(fileName) \(fileCount).pdf"
  177. isSuccessfull = tdocument!.write(toFile: tPath)
  178. tdocument = nil
  179. j = 1
  180. if (!isSuccessfull) {
  181. break
  182. } else {
  183. successArray.append(tPath)
  184. }
  185. }else{
  186. j += 1
  187. }
  188. } else if (j == averagelyCount) {
  189. j = 1
  190. let page = self.page(at: i)
  191. tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
  192. let tPath = "\(folerPath)/\(fileName) \(fileCount).pdf"
  193. isSuccessfull = tdocument!.write(toFile: tPath)
  194. tdocument = nil
  195. if (!isSuccessfull) {
  196. break
  197. } else {
  198. successArray.append(tPath)
  199. }
  200. } else{
  201. let page = self.page(at: i)
  202. tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
  203. j += 1
  204. }
  205. if (i == pageCount-1) {
  206. if (tdocument != nil) {
  207. let tPath = "\(folerPath)/\(fileName) \(fileCount).pdf"
  208. isSuccessfull = tdocument!.write(toFile: tPath)
  209. tdocument = nil
  210. if (!isSuccessfull) {
  211. break
  212. } else {
  213. successArray.append(tPath)
  214. }
  215. }
  216. }
  217. }
  218. if (isSuccessfull) {
  219. return successArray
  220. } else {
  221. return nil
  222. }
  223. }
  224. func splitByFileWithPages(_ fileAttribute: KMFileAttribute, folerPath: String, fileName: String) -> [String]? {
  225. var isSuccessfull = false
  226. var successArray: [String] = []
  227. var pdfDocument1 = CPDFDocument()
  228. var pdfDocument2 = CPDFDocument()
  229. for i in 0 ..< self.pageCount {
  230. var isSelected = false
  231. for number in fileAttribute.fetchSelectPages() {
  232. if number == i+1 {
  233. isSelected = true
  234. if let page = self.page(at: i) {
  235. pdfDocument1?.insertPageObject(page, at: pdfDocument1?.pageCount ?? 0)
  236. }
  237. }
  238. }
  239. if (!isSelected) {
  240. if let page = self.page(at: i) {
  241. pdfDocument2?.insertPageObject(page, at: pdfDocument2?.pageCount ?? 0)
  242. }
  243. }
  244. }
  245. if let cnt = pdfDocument1?.pageCount, cnt > 0 {
  246. let tPath = "\(folerPath)/\(fileName) 1.pdf"
  247. isSuccessfull = pdfDocument1?.write(toFile: tPath) ?? false
  248. if (isSuccessfull) {
  249. successArray.append(tPath)
  250. } else {
  251. return nil
  252. }
  253. }
  254. if let cnt = pdfDocument2?.pageCount, cnt > 0 {
  255. let tPath = "\(folerPath)/\(fileName) 2.pdf"
  256. isSuccessfull = pdfDocument2?.write(toFile: tPath) ?? false
  257. if (isSuccessfull) {
  258. successArray.append(tPath)
  259. }
  260. }
  261. if (isSuccessfull) {
  262. return successArray
  263. } else {
  264. return nil
  265. }
  266. }
  267. }
  268. extension CPDFDocument {
  269. func insertBlankPage(pageSize: NSSize, at index: Int) -> Bool {
  270. return self.insertPage(pageSize, at: UInt(index))
  271. }
  272. }