Browse Source

【页面编辑】拆分功能调式

tangchao 1 year ago
parent
commit
33e46cfaff

+ 0 - 16
PDF Office/PDF Master.xcodeproj/xcuserdata/kdanmobile.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -356,21 +356,5 @@
             landmarkType = "7">
          </BreakpointContent>
       </BreakpointProxy>
-      <BreakpointProxy
-         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
-         <BreakpointContent
-            uuid = "733B6073-1A8A-4652-B1B6-1D87E4993F42"
-            shouldBeEnabled = "Yes"
-            ignoreCount = "0"
-            continueAfterRunningActions = "No"
-            filePath = "PDF Master/Class/PDFTools/PageEdit/Window/SplitWindowController.swift"
-            startingColumnNumber = "9223372036854775807"
-            endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "309"
-            endingLineNumber = "309"
-            landmarkName = "buttonItemClicked_Split(_:)"
-            landmarkType = "7">
-         </BreakpointContent>
-      </BreakpointProxy>
    </Breakpoints>
 </Bucket>

+ 81 - 0
PDF Office/PDF Master/Class/Home/Category/CPDFDocument+PageEditManager.swift

@@ -183,6 +183,87 @@ extension CPDFDocument {
             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()
+//                PDFPage * page = [[[self pageAtIndex:i]copy] autorelease];
+                let page = self.page(at: i)
+//                [tdocument insertPage:page atIndex:[tdocument pageCount]];
+                tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
+                if (j == averagelyCount) {
+//                    NSString *tPath = [folerPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %ld.pdf",fileName,fileCount]];
+                    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
+//                PDFPage * page = [[[self pageAtIndex:i]copy] autorelease];
+                let page = self.page(at: i)
+//                [tdocument insertPage:page atIndex:[tdocument pageCount]];
+                tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
+//                NSString *tPath = [folerPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %ld.pdf",fileName,fileCount]];
+                let tPath = "\(folerPath)/\(fileName) \(fileCount).pdf"
+
+                isSuccessfull = tdocument!.write(toFile: tPath)
+                tdocument = nil
+                if (!isSuccessfull) {
+                    break
+                } else {
+                    successArray.append(tPath)
+                }
+            } else{
+//                PDFPage * page = [[[self pageAtIndex:i]copy] autorelease];
+                let page = self.page(at: i)
+                
+//                [tdocument insertPage:page atIndex:[tdocument pageCount]];
+                tdocument?.insertPageObject(page!, at: tdocument?.pageCount ?? 0)
+                j += 1
+            }
+            if (i == pageCount-1) {
+                if (tdocument != nil) {
+//                    NSString *tPath = [folerPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %ld.pdf",fileName,fileCount]];
+                    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
+        }
+    }
 }
 
 extension CPDFDocument {

+ 8 - 4
PDF Office/PDF Master/Class/PDFTools/PageEdit/Window/SplitWindowController.swift

@@ -198,6 +198,7 @@ class SplitWindowController: NSWindowController {
                     }
                 }
                 fileAttribute?.pagesString = tPagesString
+                fileAttribute?.pagesType = .odd
             } else if self.pageRangeComboBox.stringValue == KMLocalizedString("Even Pages Only", nil) {
                 var tPagesString = ""
                 for i in 0 ..< self._PDFDocument!.pageCount {
@@ -210,8 +211,10 @@ class SplitWindowController: NSWindowController {
                     }
                 }
                 fileAttribute?.pagesString = tPagesString
+                fileAttribute?.pagesType = .even
             } else {
                 fileAttribute?.pagesString = self.pageRangeComboBox.stringValue
+                fileAttribute?.pagesType = .custom
             }
             if let data = fileAttribute?.fetchSelectPages().isEmpty, data {
                 KMAlertTool.runModelForMainThread(message: KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
@@ -280,7 +283,8 @@ class SplitWindowController: NSWindowController {
                 } else if (self.byFileButton.state == .on) {
                     let dex = self.byFileStepper.integerValue
                     DispatchQueue.global().async {
-                        let successArray = self._PDFDocument?.splitByPagesWith(dex, folerPath: tFolderPath, fileName: folderName)
+//                        let successArray = self._PDFDocument?.splitByPagesWith(dex, folerPath: tFolderPath, fileName: folderName)
+                        let successArray = self._PDFDocument?.splitByFileWith(dex, folerPath: tFolderPath, fileName: folderName)
                         DispatchQueue.main.async {
                             self.progress.isHidden = true
                             self.cancelButton.isEnabled = true
@@ -324,11 +328,11 @@ class SplitWindowController: NSWindowController {
                             }
                         }
                         if (pdfDocument1!.pageCount > 0) {
-                            let tpath = "\(tFolderPath)\\\(folderName) 1.pdf"
+                            let tpath = "\(tFolderPath)/\(folderName) 1.pdf"
                             _isSuccessfully = pdfDocument1?.write(toFile:tpath) ?? false
                         }
                         if (pdfDocument2!.pageCount > 0) {
-                            let tPath = "\(tFolderPath)\\\(folderName) 2.pdf"
+                            let tPath = "\(tFolderPath)/\(folderName) 2.pdf"
                             pdfDocument2?.write(toFile: tPath)
                         }
                         DispatchQueue.main.async {
@@ -344,7 +348,7 @@ class SplitWindowController: NSWindowController {
                                 if (response == .alertFirstButtonReturn) {
                                     self.km_quick_endSheet()
                                     DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
-                                        let filePath = "\(tFolderPath)\\\(folderName) 1.pdf"
+                                        let filePath = "\(tFolderPath)/\(folderName) 1.pdf"
                                         self._viewFileAtFinder(filePath)
                                     }
                                 }