Browse Source

【合并】合并后的文档是损坏的 -- 自己的库不支持重绘,已换成系统库

lizhe 9 months ago
parent
commit
f8aab29d55

+ 43 - 47
PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/KMMergeWindowController.swift

@@ -18,13 +18,12 @@ class KMMergeWindowController: KMBaseWindowController {
     
 //    var cancelAction: KMMergeWindowControllerCancelAction?
     
-    var oldPDFDocument: CPDFDocument = CPDFDocument()
+    var oldPDFDocument: PDFDocument = PDFDocument()
     var password: String = ""
     
     var oriDucumentUrl: URL? {
         didSet {
-            guard let document = CPDFDocument(url: oriDucumentUrl!) else { return }
-            oldPDFDocument = document
+            oldPDFDocument = PDFDocument(url: oriDucumentUrl!)!
             oldPDFDocument.unlock(withPassword: self.password)
         }
     }
@@ -37,7 +36,7 @@ class KMMergeWindowController: KMBaseWindowController {
 //    - (id)initWithPDFDocument:(PDFDocument *)document password:(NSString *)password
 //    {
 //        if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
-//            
+//
 //    //        self.PDFDocument = document;
 //            self.PDFDocument = [[PDFDocument alloc] init];
 //            self.editType = KMPDFPageEditAppend;
@@ -158,8 +157,8 @@ extension KMMergeWindowController {
 //        _isSuccessfully = NO;
 //        [self.nCancelVC setEnabled:NO];
 //        self.canMerge = NO;
-//        
-        var rootPDFOutlineArray: [CPDFOutline] = []
+//
+        var rootPDFOutlineArray: [PDFOutline] = []
         var allPage = true //只有是全部才支持大纲的合并
         for file in files {
             if file.fetchSelectPages().count == 0 {
@@ -173,7 +172,7 @@ extension KMMergeWindowController {
             allPage = file.bAllPage
             
             /*防止文件被地址变换后crash*/
-            guard let tDocument = CPDFDocument(url: NSURL(fileURLWithPath: file.filePath) as URL) else {
+            guard let tDocument = PDFDocument(url: NSURL(fileURLWithPath: file.filePath) as URL) else {
                 print("文件不存在")
                 let alert = NSAlert.init()
                 alert.alertStyle = .critical
@@ -182,30 +181,31 @@ extension KMMergeWindowController {
                 return
             }
             
-            var outlineArray: [CPDFOutline] = []
+            var outlineArray: [PDFOutline] = []
             
 //            if file.isLocked {
                 tDocument.unlock(withPassword: file.password)
 //            }
             
-            if tDocument.outlineRoot() != nil {
-                rootPDFOutlineArray.append(tDocument.outlineRoot())
-                self.fetchAllOfChildren(tDocument.outlineRoot(), containerArray: &outlineArray)
-                outlineArray.removeObject(tDocument.outlineRoot())
+            if tDocument.outlineRoot != nil {
+                rootPDFOutlineArray.append((tDocument.outlineRoot)!)
+                self.fetchAllOfChildren((tDocument.outlineRoot)!, containerArray: &outlineArray)
+                outlineArray.removeObject((tDocument.outlineRoot)!)
             } else {
-                let rootOutline = tDocument.setNewOutlineRoot()
-                if rootOutline != nil {
-                    rootPDFOutlineArray.append(rootOutline!)
+                let rootOutline = PDFOutline.init()
+                tDocument.outlineRoot = rootOutline
+                if tDocument.outlineRoot != nil {
+                    rootPDFOutlineArray.append(tDocument.outlineRoot!)
                 }
             }
             
             for number in file.fetchSelectPages() {
-                let page = tDocument.page(at: UInt(number - 1))
+                let page = tDocument.page(at: number - 1)
 //                if pageIndex != nil {
 //                    self.oldPDFDocument.insert(page!, at: pageIndex!)
 //                    pageIndex = pageIndex! + 1
 //                } else {
-                self.oldPDFDocument.insertPageObject(page, at: self.oldPDFDocument.pageCount)
+                    self.oldPDFDocument.insert(page!, at: self.oldPDFDocument.pageCount)
 //                }
 //                self.insertIndexSet.addIndex:(self.pdfDocument.pageCount - 1)
             }
@@ -213,30 +213,26 @@ extension KMMergeWindowController {
         
         
         let fileName = (files.first?.filePath.deletingPathExtension.lastPathComponent ?? "") + "_Merged"
-//        DispatchQueue.main.async {
-            if self.oldPDFDocument.outlineRoot() == nil {
-                self.oldPDFDocument.setNewOutlineRoot()
+        DispatchQueue.main.async {
+            if self.oldPDFDocument.outlineRoot == nil {
+                self.oldPDFDocument.outlineRoot = PDFOutline.init()
             }
-            if allPage {
+//            if allPage {
                 var insertIndex = 0
                 for i in 0..<rootPDFOutlineArray.count {
                     let rootOutline = rootPDFOutlineArray[i]
                     for j in 0..<rootOutline.numberOfChildren {
-                        let outline = rootOutline.child(at: j)
-                        if outline != nil {
-                            self.oldPDFDocument.outlineRoot().insertChild(outline!, at: UInt(insertIndex))
-                            insertIndex = insertIndex + 1
-                        }
+                        self.oldPDFDocument.outlineRoot?.insertChild(rootOutline.child(at: j)!, at: insertIndex)
+                        insertIndex = insertIndex + 1
                     }
                 }
-            }
                 
                 self.handleReDraw()
                 
                 if self.oriDucumentUrl != nil {
                     
                     let newPath = self.oldPDFDocument.documentURL!.path
-                    var options: [CPDFDocumentWriteOption : Any] = [:]
+                    var options: [PDFDocumentWriteOption : Any] = [:]
                     var success = false
                     let password = self.password
                     let pdf = self.oldPDFDocument
@@ -327,10 +323,10 @@ extension KMMergeWindowController {
                     }
                 }
 //            }
-//        }
+        }
     }
     
-    func fetchAllOfChildren(_ aOutline: CPDFOutline, containerArray aMArray: inout [CPDFOutline]) {
+    func fetchAllOfChildren(_ aOutline: PDFOutline, containerArray aMArray: inout [PDFOutline]) {
         if !aMArray.contains(aOutline) {
             aMArray.append(aOutline)
         }
@@ -352,7 +348,7 @@ extension KMMergeWindowController {
                 return
             }
             
-            var pagesArray: [CPDFPage] = []
+            var pagesArray: [PDFPage] = []
             let pageCount = self.oldPDFDocument.pageCount
             for i in 0..<pageCount {
                 pagesArray.append(self.oldPDFDocument.page(at: 0)!)
@@ -362,16 +358,16 @@ extension KMMergeWindowController {
             for i in 0..<pageCount {
                 let page: KMMergePDFPage = KMMergePDFPage.init()
                 page.setBounds(CGRectMake(0, 0, size.width, size.height), for: .mediaBox)
-                page.drawingPage = pagesArray[Int(i)]
-                self.oldPDFDocument.insertPageObject(page, at: i)
+                page.drawingPage = pagesArray[i]
+                self.oldPDFDocument.insert(page, at: i)
             }
             
-            if self.oldPDFDocument.outlineRoot() != nil {
-                let childCount = self.oldPDFDocument.outlineRoot().numberOfChildren
-                var outlineArray: [CPDFOutline] = []
+            if self.oldPDFDocument.outlineRoot != nil {
+                let childCount = self.oldPDFDocument.outlineRoot?.numberOfChildren
+                var outlineArray: [PDFOutline] = []
                 
-                for i in 0..<childCount {
-                    outlineArray.append(self.oldPDFDocument.outlineRoot().child(at: i))
+                for i in 0..<childCount! {
+                    outlineArray.append((self.oldPDFDocument.outlineRoot?.child(at: i))!)
                 }
                 
                 for outline in outlineArray {
@@ -382,17 +378,17 @@ extension KMMergeWindowController {
     }
 }
 
-class KMMergePDFPage: CPDFPage {
-    var drawingPage: CPDFPage?
+class KMMergePDFPage: PDFPage {
+    var drawingPage: PDFPage?
     
-    override func draw(with box: CPDFDisplayBox, to context: CGContext!) {
+    override func draw(with box: PDFDisplayBox, to context: CGContext) {
         super.draw(with: box, to: context)
-     
+        
         let pageSize = self.bounds(for: .cropBox).size
         self.drawPage(with: context, page: self.drawingPage!, pageSize: pageSize)
     }
-
-    func drawPage(with context: CGContext, page: CPDFPage, pageSize: CGSize) {
+    
+    func drawPage(with context: CGContext, page: PDFPage, pageSize: CGSize) {
         var originalSize = page.bounds(for: .cropBox).size
         
         // 如果页面的旋转角度为90或者270,宽高交换
@@ -412,13 +408,13 @@ class KMMergePDFPage: CPDFPage {
         
         if #available(macOS 10.12, *) {
             page.draw(with: .cropBox, to: context)
-            page.transform(context, for: .cropBox)
+            page.transformContext(for: .cropBox)
         } else {
             NSGraphicsContext.saveGraphicsState()
             NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
-            page.draw(with: .cropBox, to: context)
+            page.draw(with: .cropBox)
             NSGraphicsContext.restoreGraphicsState()
-            page.transform(context, for: .cropBox)
+            page.transformContext(for: .cropBox)
         }
         
         context.restoreGState()