KMMergeWindowController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // KMMergeWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias KMMergeWindowControllerCancelAction = (_ controller: KMMergeWindowController) -> Void
  9. typealias KMMergeWindowControllerAddFilesAction = (_ controller: KMMergeWindowController) -> Void
  10. typealias KMMergeWindowControllerMergeAction = (_ controller: KMMergeWindowController, _ filePath: String) -> Void
  11. typealias KMMergeWindowControllerClearAction = (_ controller: KMMergeWindowController) -> Void
  12. class KMMergeWindowController: KMBaseWindowController {
  13. @IBOutlet weak var mergeView: KMMergeView!
  14. // var cancelAction: KMMergeWindowControllerCancelAction?
  15. var oldPDFDocument: PDFDocument = PDFDocument()
  16. var password: String = ""
  17. var oriDucumentUrl: URL? {
  18. didSet {
  19. oldPDFDocument = PDFDocument(url: oriDucumentUrl!)!
  20. }
  21. }
  22. var pageIndex: Int?
  23. var mergeAction: KMMergeWindowControllerMergeAction?
  24. // - (id)initWithPDFDocument:(PDFDocument *)document password:(NSString *)password
  25. // {
  26. // if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
  27. //
  28. // // self.PDFDocument = document;
  29. // self.PDFDocument = [[PDFDocument alloc] init];
  30. // self.editType = KMPDFPageEditAppend;
  31. // _lockFilePathArr = [[NSMutableArray alloc] init];
  32. // _files = [[NSMutableArray alloc] init];
  33. //
  34. // KMFileAttribute *file = [[KMFileAttribute alloc] init];
  35. // file.myPDFDocument = document;
  36. // file.filePath = document.documentURL.path;
  37. // file.oriFilePath = self.oriDucumentUrl.path;
  38. // if (password && password.length > 0) {
  39. // file.password = password;
  40. // file.isLocked = YES;
  41. // }
  42. // [self.files addObject:file];
  43. // }
  44. // return self;
  45. // }
  46. convenience init(document: PDFDocument, password: String) {
  47. self.init(windowNibName: "KMMergeWindowController")
  48. }
  49. override func windowDidLoad() {
  50. super.windowDidLoad()
  51. self.window!.title = NSLocalizedString("Merge PDF Files", comment: "");
  52. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  53. mergeView.addFilesAction = { [unowned self] view in
  54. self.addFile()
  55. }
  56. mergeView.clearAction = { [unowned self] view in
  57. }
  58. mergeView.mergeAction = { [unowned self] view, files, size in
  59. self.mergeFiles(files: files, size: size)
  60. }
  61. mergeView.cancelAction = { [unowned self] view in
  62. cancelAction?(self)
  63. }
  64. }
  65. }
  66. extension KMMergeWindowController {
  67. func addFile() {
  68. // if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
  69. // //免費版只支援2個檔案做合併小于20M的文件合并
  70. // if (_files.count >= 2 || self.allFileSize > (20 * 1024 * 1024)) {
  71. // #if VERSION_DMG
  72. // [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
  73. // #else
  74. // KMToolCompareWindowController * vc = [KMToolCompareWindowController toolCompareWithType:KMCompareWithToolType_PageEdit setSelectIndex:1];
  75. // [vc showWindow:nil];
  76. // #endif
  77. // return;
  78. // }
  79. //
  80. // }
  81. let openPanel = NSOpenPanel()
  82. openPanel.allowedFileTypes = ["pdf"]
  83. if KMPurchaseManager.manager.state == .subscription {
  84. openPanel.allowsMultipleSelection = true
  85. openPanel.message = NSLocalizedString("Select files to merge. To select multiple files press cmd ⌘ button on the keyboard and click on the target files one by one.", comment: "")
  86. } else {
  87. openPanel.allowsMultipleSelection = false
  88. openPanel.message = NSLocalizedString("Select files to merge, only one file can be selected at a time.", comment: "")
  89. }
  90. openPanel.beginSheetModal(for: self.window!) { (result) in
  91. if result == NSApplication.ModalResponse.OK {
  92. var array: [URL] = []
  93. for fileURL in openPanel.urls {
  94. array.append(fileURL)
  95. // if let filePath = fileURL.path {
  96. // if !FileManager.default.isExecutableFile(atPath: filePath) {
  97. // continue
  98. // }
  99. //
  100. // do {
  101. // let attrib = try FileManager.default.attributesOfItem(atPath: filePath)
  102. //// if let fileSize = attrib[FileAttributeKey.size] as? NSNumber {
  103. //// self.allFileSize += fileSize.floatValue
  104. //// }
  105. ////
  106. //// if !IAPProductsManager.defaultManager.isAvailableAllFunction {
  107. //// if self.allFileSize > (20 * 1024 * 1024) || self.files.count >= 2 {
  108. //// let vc = KMToolCompareWindowController.toolCompare(withType: .pageEdit, setSelectIndex: 1)
  109. //// vc?.showWindow(nil)
  110. //// self.allFileSize -= fileSize.floatValue
  111. //// self.addFiles(array)
  112. //// return
  113. //// }
  114. //// }
  115. // array.append(fileURL)
  116. // } catch {
  117. // print("Error getting file attributes: \(error)")
  118. // }
  119. // }
  120. }
  121. self.mergeView.addFilePaths(urls: array)
  122. }
  123. }
  124. }
  125. func mergeFiles(files: [KMFileAttribute], size: CGSize = CGSizeZero) {
  126. var filesCount = 1
  127. if self.oriDucumentUrl != nil {
  128. filesCount = 0
  129. }
  130. if files.count <= filesCount {
  131. let alert = NSAlert.init()
  132. alert.alertStyle = .critical
  133. alert.messageText = NSLocalizedString("To start merging, please select at least 2 files.", comment: "")
  134. alert.runModal()
  135. return
  136. }
  137. // _isSuccessfully = NO;
  138. // [self.nCancelVC setEnabled:NO];
  139. // self.canMerge = NO;
  140. //
  141. var rootPDFOutlineArray: [PDFOutline] = []
  142. var allPage = true //只有是全部才支持大纲的合并
  143. for file in files {
  144. if file.fetchSelectPages().count == 0 {
  145. let alert = NSAlert.init()
  146. alert.alertStyle = .critical
  147. alert.messageText = "\(file.filePath.lastPathComponent) + \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  148. alert.runModal()
  149. return
  150. }
  151. allPage = file.bAllPage
  152. let tDocument = PDFDocument(url: NSURL(fileURLWithPath: file.filePath) as URL)!
  153. var outlineArray: [PDFOutline] = []
  154. // if file.isLocked {
  155. tDocument.unlock(withPassword: file.password)
  156. // }
  157. if tDocument.outlineRoot != nil {
  158. rootPDFOutlineArray.append((tDocument.outlineRoot)!)
  159. self.fetchAllOfChildren((tDocument.outlineRoot)!, containerArray: &outlineArray)
  160. outlineArray.removeObject((tDocument.outlineRoot)!)
  161. } else {
  162. let rootOutline = PDFOutline.init()
  163. tDocument.outlineRoot = rootOutline
  164. if tDocument.outlineRoot != nil {
  165. rootPDFOutlineArray.append(tDocument.outlineRoot!)
  166. }
  167. }
  168. for number in file.fetchSelectPages() {
  169. let page = tDocument.page(at: number - 1)
  170. if pageIndex != nil {
  171. self.oldPDFDocument.insert(page!, at: pageIndex!)
  172. pageIndex = pageIndex! + 1
  173. } else {
  174. self.oldPDFDocument.insert(page!, at: self.oldPDFDocument.pageCount)
  175. }
  176. // self.insertIndexSet.addIndex:(self.pdfDocument.pageCount - 1)
  177. }
  178. }
  179. let fileName = (files.first?.filePath.lastPathComponent)! + "_Merged"
  180. DispatchQueue.main.async {
  181. self.oldPDFDocument.outlineRoot = PDFOutline.init()
  182. if allPage {
  183. var insertIndex = 0
  184. for i in 0..<rootPDFOutlineArray.count {
  185. let rootOutline = rootPDFOutlineArray[i]
  186. for j in 0..<rootOutline.numberOfChildren {
  187. self.oldPDFDocument.outlineRoot?.insertChild(rootOutline.child(at: j)!, at: insertIndex)
  188. }
  189. insertIndex = insertIndex + 1
  190. }
  191. self.handleReDraw()
  192. if self.oriDucumentUrl != nil {
  193. var success = self.oldPDFDocument.write(toFile: self.oldPDFDocument.documentURL!.path)
  194. self.mergeAction?(self, self.oldPDFDocument.documentURL!.path)
  195. } else {
  196. let savePanelAccessoryViewController = KMSavePanelAccessoryController.init()
  197. let savePanel = NSSavePanel()
  198. savePanel.nameFieldStringValue = fileName
  199. savePanel.allowedFileTypes = ["pdf"]
  200. savePanel.accessoryView = savePanelAccessoryViewController.view
  201. // self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  202. savePanel.beginSheetModal(for: self.window!) { result in
  203. if result != nil {
  204. var outputSavePanel = savePanel.url?.path
  205. DispatchQueue.main.async {
  206. var success = self.oldPDFDocument.write(toFile: outputSavePanel!)
  207. if !success {
  208. success = ((try?self.oldPDFDocument.dataRepresentation()?.write(to: URL(string: outputSavePanel!)!)) != nil)
  209. }
  210. if success {
  211. if savePanelAccessoryViewController.needOpen {
  212. NSDocumentController.shared.openDocument(withContentsOf: savePanel.url!, display: true) { document, open, error in
  213. }
  214. } else {
  215. NSWorkspace.shared.activateFileViewerSelecting([NSURL(fileURLWithPath: outputSavePanel!) as URL])
  216. }
  217. } else {
  218. let alert = NSAlert.init()
  219. alert.alertStyle = .critical
  220. alert.messageText = "\(String(describing: files.first?.filePath.lastPathComponent)) + \(NSLocalizedString("Failed to merge!", comment: ""))"
  221. alert.runModal()
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. func fetchAllOfChildren(_ aOutline: PDFOutline, containerArray aMArray: inout [PDFOutline]) {
  231. if !aMArray.contains(aOutline) {
  232. aMArray.append(aOutline)
  233. }
  234. for i in 0..<aOutline.numberOfChildren {
  235. if let childOutline = aOutline.child(at: i) {
  236. aMArray.append(childOutline)
  237. fetchAllOfChildren(childOutline, containerArray: &aMArray)
  238. }
  239. }
  240. }
  241. func handleReDraw() {
  242. if mergeView.originalSizeButton.state == .on {
  243. } else {
  244. let size = self.mergeView.newPageSize
  245. if size.width < 0 {
  246. return
  247. }
  248. var pagesArray: [PDFPage] = []
  249. let pageCount = self.oldPDFDocument.pageCount
  250. for i in 0..<pageCount {
  251. pagesArray.append(self.oldPDFDocument.page(at: 0)!)
  252. self.oldPDFDocument.removePage(at: 0)
  253. }
  254. for i in 0..<pageCount {
  255. let page: KMMergePDFPage = KMMergePDFPage.init()
  256. page.setBounds(CGRectMake(0, 0, size.width, size.height), for: .mediaBox)
  257. page.drawingPage = pagesArray[i]
  258. self.oldPDFDocument.insert(page, at: i)
  259. }
  260. if self.oldPDFDocument.outlineRoot != nil {
  261. let childCount = self.oldPDFDocument.outlineRoot?.numberOfChildren
  262. var outlineArray: [PDFOutline] = []
  263. for i in 0..<childCount! {
  264. outlineArray.append((self.oldPDFDocument.outlineRoot?.child(at: i))!)
  265. }
  266. for outline in outlineArray {
  267. outline.removeFromParent()
  268. }
  269. }
  270. }
  271. }
  272. }
  273. class KMMergePDFPage: PDFPage {
  274. var drawingPage: PDFPage?
  275. override func draw(with box: PDFDisplayBox, to context: CGContext) {
  276. super.draw(with: box, to: context)
  277. let pageSize = self.bounds(for: .cropBox).size
  278. self.drawPage(with: context, page: self.drawingPage!, pageSize: pageSize)
  279. }
  280. func drawPage(with context: CGContext, page: PDFPage, pageSize: CGSize) {
  281. var originalSize = page.bounds(for: .cropBox).size
  282. // 如果页面的旋转角度为90或者270,宽高交换
  283. if page.rotation % 180 != 0 {
  284. originalSize = CGSize(width: originalSize.height, height: originalSize.width)
  285. }
  286. let wRatio = pageSize.width / originalSize.width
  287. let hRatio = pageSize.height / originalSize.height
  288. let ratio = min(wRatio, hRatio)
  289. context.saveGState()
  290. let xTransform = (pageSize.width - originalSize.width * ratio) / 2
  291. let yTransform = (pageSize.height - originalSize.height * ratio) / 2
  292. context.translateBy(x: xTransform, y: yTransform)
  293. context.scaleBy(x: ratio, y: ratio)
  294. if #available(macOS 10.12, *) {
  295. page.draw(with: .cropBox, to: context)
  296. page.transformContext(for: .cropBox)
  297. } else {
  298. NSGraphicsContext.saveGraphicsState()
  299. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  300. page.draw(with: .cropBox)
  301. NSGraphicsContext.restoreGraphicsState()
  302. page.transformContext(for: .cropBox)
  303. }
  304. context.restoreGState()
  305. }
  306. }