瀏覽代碼

综合 - 去掉合并模块PDFKit的相关调用,改为ComPDFKit

liaoxiaoyue 2 年之前
父節點
當前提交
c875728436

+ 72 - 71
PDF Office/PDF Office/Class/Merge/Controller/KMMergeViewController.swift

@@ -9,8 +9,8 @@ import Cocoa
 import PDFKit
 import CoreGraphics
 
-class KMMergePage: PDFPage {
-    var drawingPage: PDFPage!
+class KMMergePage: CPDFPage {
+    var drawingPage: CPDFPage!
     
 //    override func draw(with box: PDFDisplayBox) {
 //        super.draw(with: box)
@@ -21,15 +21,14 @@ class KMMergePage: PDFPage {
 //        drawPageWithContext(context: context, page: self.drawingPage, pageSize: pageSize)
 //    }
     
-    override func draw(with box: PDFDisplayBox, to context: CGContext) {
+    override func draw(with box: CPDFDisplayBox, to context: CGContext!) {
         super.draw(with: box, to: context)
-        
         let pageSize = bounds(for: .cropBox).size
         
         drawPageWithContext(context: context, page: self.drawingPage, pageSize: pageSize)
     }
     
-    func drawPageWithContext(context: CGContext, page: PDFPage, pageSize: NSSize) {
+    func drawPageWithContext(context: CGContext, page: CPDFPage, pageSize: NSSize) {
         var originalSize = page.bounds(for: .cropBox).size
         
         /// 如果page的旋转角度为90,或者270,宽高交换
@@ -113,7 +112,7 @@ class KMMergeViewController: NSViewController {
     private var pageRangeWindowController: KMPageRangePickerWindowController!
     private var mergeSettingWindowController: KMMergeSettingWindowController!
     
-    private var outPDFDocument = PDFDocument()
+    private var outPDFDocument = CPDFDocument()
     
     private var lockedFiles: [URL] = []
     
@@ -209,7 +208,7 @@ class KMMergeViewController: NSViewController {
                     [self] (windowController: NSWindowController) -> () in
                     
                     if mergeSettingWindowController.odd_even_mergeComboBox.state == .on { /// 奇偶数穿插合并
-                        let document = PDFDocument()
+                        let document = CPDFDocument()
                         outPDFDocument = document
                         
                         mergeActionForOddEven()
@@ -223,7 +222,7 @@ class KMMergeViewController: NSViewController {
                         let response = panel.runModal()
                         if response == .OK {
                             let url = panel.url
-                            let result = outPDFDocument.write(to: panel.url!)
+                            let result = outPDFDocument!.write(to: panel.url!)
                             if result {
                                 if button.state == .on { /// 开启文档
 
@@ -233,7 +232,7 @@ class KMMergeViewController: NSViewController {
                             }
                         }
                     } else { /// 正常合并
-                        let document = PDFDocument()
+                        let document = CPDFDocument()
                         outPDFDocument = document
                         
                         mergeAction()
@@ -246,7 +245,7 @@ class KMMergeViewController: NSViewController {
                         let response = panel.runModal()
                         if response == .OK {
                             let url = panel.url
-                            let result = document.write(to: panel.url!)
+                            let result = document!.write(to: panel.url!)
                             if result {
                                 if button.state == .on { /// 开启文档
 
@@ -350,7 +349,7 @@ class KMMergeViewController: NSViewController {
             return
         }
         
-        var rootPDFOutlineArray: [PDFOutline] = []
+        var rootPDFOutlineArray: [CPDFOutline] = []
         var pdfDocument = outPDFDocument
         
         var mergeOutline: Bool = true
@@ -359,52 +358,52 @@ class KMMergeViewController: NSViewController {
                 mergeOutline = false
             }
             
-            var pdfOutlineArray: [PDFOutline] = []
+            var pdfOutlineArray: [CPDFOutline] = []
             if (fileModel.document.outlineRoot != nil) {
-                rootPDFOutlineArray.append(fileModel.document.outlineRoot!)
-                fetchAllChildren(outline: fileModel.document.outlineRoot!, containers: &pdfOutlineArray)
-                pdfOutlineArray.removeObject(fileModel.document.outlineRoot!)
+                rootPDFOutlineArray.append(fileModel.document.outlineRoot())
+                fetchAllChildren(outline: fileModel.document.outlineRoot(), containers: &pdfOutlineArray)
+                pdfOutlineArray.removeObject(fileModel.document.outlineRoot())
             } else {
-                var rootOutline = PDFOutline()
-                fileModel.document.outlineRoot = rootOutline
-                if fileModel.document.outlineRoot != nil {
-                    rootPDFOutlineArray.append(fileModel.document.outlineRoot!)
+                var rootOutline = CPDFOutline()
+                fileModel.document.setOutlineRoot(rootOutline)
+                if fileModel.document.outlineRoot() != nil {
+                    rootPDFOutlineArray.append(fileModel.document.outlineRoot())
                 }
             }
             
             for outline in pdfOutlineArray {
                 /// 这段代码主要是调用他的getter方法;outline显示正确
-                outline.destination?.page?.dataRepresentation
+                outline.destination?.page().document.dataRepresentation()
             }
         }
         
         let oddDocument = dataArray.first?.document
-        let oddCount: Int = oddDocument!.pageCount
+        let oddCount: Int = Int(oddDocument!.pageCount)
         let evenDocument = dataArray.last?.document!
-        let evenCount: Int = evenDocument!.pageCount
+        let evenCount: Int = Int(evenDocument!.pageCount)
         let count = min(oddCount-1, evenCount-1)
         for i in 0 ... count {
-            pdfDocument.insert((oddDocument?.page(at: i))!, at: pdfDocument.pageCount)
-            pdfDocument.insert((evenDocument?.page(at: i))!, at: pdfDocument.pageCount)
+            pdfDocument!.insertPageObject((oddDocument?.page(at: UInt(i)))!, at: pdfDocument!.pageCount)
+            pdfDocument!.insertPageObject((evenDocument?.page(at: UInt(i)))!, at: pdfDocument!.pageCount)
         }
         
         if oddCount > evenCount {
             for i in count ... oddCount-1 {
-                pdfDocument.insert((oddDocument?.page(at: i))!, at: pdfDocument.pageCount)
+                pdfDocument!.insertPageObject((oddDocument?.page(at: UInt(i)))!, at: pdfDocument!.pageCount)
             }
         } else if oddCount < evenCount {
             for i in count ... evenCount-1 {
-                pdfDocument.insert((evenDocument?.page(at: i))!, at: pdfDocument.pageCount)
+                pdfDocument!.insertPageObject((evenDocument?.page(at: UInt(i)))!, at: pdfDocument!.pageCount)
             }
         }
         
         if mergeOutline {
-            pdfDocument.outlineRoot = PDFOutline()
+            pdfDocument?.setOutlineRoot(CPDFOutline())
             var index = 0
             for outlineRoot in rootPDFOutlineArray {
                 if outlineRoot != nil && outlineRoot.numberOfChildren > 0 {
                     for i in 0 ... outlineRoot.numberOfChildren-1 {
-                        pdfDocument.outlineRoot?.insertChild(outlineRoot.child(at: i)!, at: index)
+                        pdfDocument!.outlineRoot().insertChild(outlineRoot.child(at: UInt(i)), at: UInt(index))
                         index += 1
                     }
                 }
@@ -419,7 +418,7 @@ class KMMergeViewController: NSViewController {
 //            return
         }
         
-        var rootPDFOutlineArray: [PDFOutline] = []
+        var rootPDFOutlineArray: [CPDFOutline] = []
         var pdfDocument = outPDFDocument
         
         var mergeOutline: Bool = true
@@ -428,27 +427,27 @@ class KMMergeViewController: NSViewController {
                 mergeOutline = false
             }
             
-            var pdfOutlineArray: [PDFOutline] = []
-            if (fileModel.document.outlineRoot != nil) {
-                rootPDFOutlineArray.append(fileModel.document.outlineRoot!)
-                fetchAllChildren(outline: fileModel.document.outlineRoot!, containers: &pdfOutlineArray)
-                pdfOutlineArray.removeObject(fileModel.document.outlineRoot!)
+            var pdfOutlineArray: [CPDFOutline] = []
+            if (fileModel.document.outlineRoot() != nil) {
+                rootPDFOutlineArray.append(fileModel.document.outlineRoot())
+                fetchAllChildren(outline: fileModel.document.outlineRoot(), containers: &pdfOutlineArray)
+                pdfOutlineArray.removeObject(fileModel.document.outlineRoot())
             } else {
-                var rootOutline = PDFOutline()
-                fileModel.document.outlineRoot = rootOutline
-                if fileModel.document.outlineRoot != nil {
-                    rootPDFOutlineArray.append(fileModel.document.outlineRoot!)
+                var rootOutline = CPDFOutline()
+                fileModel.document.setOutlineRoot(rootOutline)
+                if fileModel.document.outlineRoot() != nil {
+                    rootPDFOutlineArray.append(fileModel.document.outlineRoot())
                 }
             }
             
             for outline in pdfOutlineArray {
                 /// 这段代码主要是调用他的getter方法;outline显示正确
-                outline.destination?.page?.dataRepresentation
+                outline.destination?.page().document.dataRepresentation()
             }
             
             if fileModel.pageRange == .all {
                 for i in 0 ... fileModel.document.pageCount-1 {
-                    pdfDocument.insert(fileModel.document.page(at: Int(i))!, at: pdfDocument.pageCount)
+                    pdfDocument!.insertPageObject(fileModel.document.page(at: UInt(i))!, at: pdfDocument!.pageCount)
                 }
             } else if fileModel.pageRange == .oddPages {
                 for i in 0 ... fileModel.document.pageCount-1 {
@@ -456,7 +455,7 @@ class KMMergeViewController: NSViewController {
                         continue
                     }
                     
-                    pdfDocument.insert(fileModel.document.page(at: Int(i))!, at: pdfDocument.pageCount)
+                    pdfDocument!.insertPageObject(fileModel.document.page(at: UInt(i))!, at: pdfDocument!.pageCount)
                 }
             } else if fileModel.pageRange == .evenPages {
                 for i in 0 ... fileModel.document.pageCount-1 {
@@ -464,23 +463,23 @@ class KMMergeViewController: NSViewController {
                         continue
                     }
                     
-                    pdfDocument.insert(fileModel.document.page(at: Int(i))!, at: pdfDocument.pageCount)
+                    pdfDocument!.insertPageObject(fileModel.document.page(at: UInt(i))!, at: pdfDocument!.pageCount)
                 }
             } else if fileModel.pageRange == .custom {
                 let pageNumbers = self.findSelectPage(fileModel: fileModel)
                 for i in pageNumbers {
-                    pdfDocument.insert(fileModel.document.page(at: Int(i-1))!, at: pdfDocument.pageCount)
+                    pdfDocument!.insertPageObject(fileModel.document.page(at: UInt(i-1))!, at: pdfDocument!.pageCount)
                 }
             }
         }
         
         if mergeOutline {
-            pdfDocument.outlineRoot = PDFOutline()
+            pdfDocument?.setOutlineRoot( CPDFOutline())
             var index = 0
             for outlineRoot in rootPDFOutlineArray {
                 if outlineRoot != nil && outlineRoot.numberOfChildren > 0 {
                     for i in 0 ... outlineRoot.numberOfChildren-1 {
-                        pdfDocument.outlineRoot?.insertChild(outlineRoot.child(at: i)!, at: index)
+                        pdfDocument!.outlineRoot().insertChild(outlineRoot.child(at: UInt(i)), at: UInt(index))
                         index += 1
                     }
                 }
@@ -509,26 +508,26 @@ class KMMergeViewController: NSViewController {
                 pageSize.height = CGFloat(mergeSettingWindowController.customHeightTextField.floatValue)
             }
             
-            var pagesArray: [PDFPage] = []
-            let pageCount = outPDFDocument.pageCount
+            var pagesArray: [CPDFPage] = []
+            let pageCount = outPDFDocument!.pageCount
             for _ in 0 ... pageCount-1 {
-                pagesArray.append(outPDFDocument.page(at: 0)!)
-                outPDFDocument.removePage(at: 0)
+                pagesArray.append(outPDFDocument!.page(at: 0)!)
+                outPDFDocument!.removePage(at: 0)
             }
             
             for i in 0 ... pageCount-1 {
                 var page = KMMergePage()
-                page.drawingPage = pagesArray[i]
+                page.drawingPage = pagesArray[Int(i)]
                 page.setBounds(NSMakeRect(0, 0, pageSize.width, pageSize.height), for: .cropBox)
-                outPDFDocument.insert(page, at: i)
+                outPDFDocument!.insertPageObject(page, at: i)
             }
             
             /// 如果是自定义大小,删除所有的outline,因为合并出来的outline是无效的
             if mergeSettingWindowController.pageSizeComboBox.indexOfSelectedItem == 5 { /// 自定义 595 x 841
-                let childCount: Int = outPDFDocument.outlineRoot!.numberOfChildren
-                var array: [PDFOutline] = []
+                let childCount: Int = Int(outPDFDocument!.outlineRoot().numberOfChildren)
+                var array: [CPDFOutline] = []
                 for i in 0 ... childCount-1 {
-                    array.append((outPDFDocument.outlineRoot?.child(at: i))!)
+                    array.append((outPDFDocument!.outlineRoot().child(at: UInt(i)))!)
                 }
                 
                 for outline in array {
@@ -538,7 +537,7 @@ class KMMergeViewController: NSViewController {
         }
     }
     
-    func fetchAllChildren(outline: PDFOutline, containers: inout [PDFOutline]) {
+    func fetchAllChildren(outline: CPDFOutline, containers: inout [CPDFOutline]) {
         if !containers.contains(outline) {
             containers.append(outline)
         }
@@ -652,7 +651,7 @@ class KMMergeViewController: NSViewController {
         let model: KMMergeFileModel = KMMergeFileModel();
         model.documentURL = documentURL
         if documentURL.pathExtension.lowercased() == "pdf" {
-            model.document = PDFDocument.init(url: documentURL)
+            model.document = CPDFDocument.init(url: documentURL)
             if model.document.isLocked {
                 lockedFiles.append(documentURL)
                 
@@ -666,16 +665,17 @@ class KMMergeViewController: NSViewController {
                 for i in 0 ... model.document.pageCount-1 {
                     let pageModel = KMMergePageModel()
                     pageModel.selected = true
-                    pageModel.pageID = i
+                    pageModel.pageID = Int(i)
                     model.pages.append(pageModel)
                 }
             } else {
                 
             }
         } else {
-            model.document = PDFDocument.init()
-            model.page = PDFPage(image: NSImage(contentsOf: documentURL)!)
-            model.document.insert(model.page, at: 0)
+            let doucument = CPDFDocument.init(url: documentURL)
+            model.document = doucument
+            model.page = doucument?.page(at: 0)
+            model.document.insertPageObject(model.page, at: 0)
         }
 
         self.dataArray.append(model)
@@ -686,7 +686,7 @@ class KMMergeViewController: NSViewController {
             let model: KMMergeFileModel = KMMergeFileModel();
             model.documentURL = documentURL
             if documentURL.pathExtension.lowercased() == "pdf" {
-                model.document = PDFDocument.init(url: documentURL)
+                model.document = CPDFDocument.init(url: documentURL)
                 model.myDocument = CPDFDocument.init(url: documentURL)
                 model.pageRange = .all
                 model.page = model.document.page(at: 0)
@@ -694,16 +694,17 @@ class KMMergeViewController: NSViewController {
                     for i in 0 ... model.document.pageCount {
                         let pageModel = KMMergePageModel()
                         pageModel.selected = true
-                        pageModel.pageID = i
+                        pageModel.pageID = Int(i)
                         model.pages.append(pageModel)
                     }
                 } else {
                     
                 }
             } else {
-                model.document = PDFDocument.init()
-                model.page = PDFPage(image: NSImage(contentsOf: documentURL)!)
-                model.document.insert(model.page, at: 0)
+                let doucument = CPDFDocument.init(url: documentURL)
+                model.document = doucument
+                model.page = doucument?.page(at: 0)
+                model.document.insertPageObject(model.page, at: 0)
             }
 
             self.dataArray[index] = model
@@ -794,7 +795,7 @@ class KMMergeViewController: NSViewController {
             if model.open {
                 for i in 0...(model.document.pageCount-1) {
                     if count == indexPath.item {
-                        index = i
+                        index = Int(i)
                         flagModel = model
                         break
                     }
@@ -857,7 +858,7 @@ class KMMergeViewController: NSViewController {
                         
                         let model = KMMergeFileModel()
                         model.documentURL = documentURL
-                        model.document = PDFDocument(url: documentURL!)
+                        model.document = CPDFDocument(url: documentURL!)
                         model.document.unlock(withPassword: string)
                         model.myDocument = CPDFDocument.init(url: documentURL)
                         model.pageRange = .all
@@ -867,7 +868,7 @@ class KMMergeViewController: NSViewController {
                             for i in 0 ... model.document.pageCount-1 {
                                 let pageModel = KMMergePageModel()
                                 pageModel.selected = true
-                                pageModel.pageID = i
+                                pageModel.pageID = Int(i)
                                 model.pages.append(pageModel)
                             }
                         }
@@ -987,7 +988,7 @@ extension KMMergeViewController: NSCollectionViewDataSource {
         var count: Int = 0
         for model in dataArray {
             if model.open {
-                count += model.document.pageCount
+                count += Int(model.document.pageCount)
             } else {
                 count += 1
             }
@@ -1008,7 +1009,7 @@ extension KMMergeViewController: NSCollectionViewDataSource {
             if model.open {
                 for i in 0...(model.document.pageCount-1) {
                     if count == indexPath.item {
-                        index = i
+                        index = Int(i)
                         flagModel = model
                         break
                     }
@@ -1102,7 +1103,7 @@ extension KMMergeViewController: NSCollectionViewDataSource {
         } else {
             let cellView: KMMergeCollectionPageViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "PageCellID"), for: indexPath) as! KMMergeCollectionPageViewItem
             let model = flagModel.pages[index]
-            model.page = flagModel.document.page(at: index)
+            model.page = flagModel.document.page(at: UInt(index))
             cellView.fileModel = flagModel
             cellView.model = model
             cellView.flodClick = {

+ 2 - 2
PDF Office/PDF Office/Class/Merge/Model/KMMergeFileModel.swift

@@ -17,9 +17,9 @@ enum KMMergeFilePageRange: String {
 
 class KMMergeFileModel: NSObject {
     var documentURL: URL!
-    var document: PDFDocument!
+    var document: CPDFDocument!
     var open : Bool = false
-    var page: PDFPage!
+    var page: CPDFPage!
     
     var password: String!
     

+ 1 - 1
PDF Office/PDF Office/Class/Merge/Model/KMMergePageModel.swift

@@ -10,6 +10,6 @@ import PDFKit
 
 class KMMergePageModel: NSObject {
     var selected: Bool = false
-    var page: PDFPage!
+    var page: CPDFPage!
     var pageID: Int = 0
 }

+ 1 - 1
PDF Office/PDF Office/Class/Merge/View/KMMergeCollectionPageViewItem.swift

@@ -109,7 +109,7 @@ class KMMergeCollectionPageViewItem: NSCollectionViewItem {
         let border: CGFloat = 10
         var bounds: NSRect = NSZeroRect
         if (myModel != nil) {
-            bounds = myModel.page.bounds(for: PDFDisplayBox.cropBox)
+            bounds = myModel.page.bounds
         }
         
         if (bounds.size.width == 0) {

+ 1 - 1
PDF Office/PDF Office/Class/Merge/View/KMMergeCollectionViewItem.swift

@@ -102,7 +102,7 @@ class KMMergeCollectionViewItem: NSCollectionViewItem {
         let border: CGFloat = 10
         var bounds: NSRect = NSZeroRect
         if (myModel != nil) {
-            bounds = myModel.page.bounds(for: PDFDisplayBox.cropBox)
+            bounds = myModel.page.bounds
         }
         
         if (bounds.size.width == 0) {

+ 6 - 6
PDF Office/PDF Office/Class/Merge/View/KMPDFThumbnialPageView.swift

@@ -9,7 +9,7 @@ import Cocoa
 import PDFKit
 
 class KMPDFThumbnialPageLayer: CATiledLayer {
-    var page: PDFPage!
+    var page: CPDFPage!
     
     override init() {
         super.init()
@@ -25,20 +25,20 @@ class KMPDFThumbnialPageLayer: CATiledLayer {
     
     override func draw(in ctx: CGContext) {
         ctx.saveGState();
-        var bounds = page.bounds(for: PDFDisplayBox.cropBox)
-        let transform = page.transform(for: PDFDisplayBox.cropBox)
+        var bounds = page.bounds
+        let transform = page.transform()
         bounds = bounds.applying(transform);
         ctx.scaleBy(x: self.bounds.size.width / bounds.size.width,
                     y: self.bounds.size.height / bounds.size.height);
-        page.draw(with: PDFDisplayBox.cropBox, to: ctx)
+        page.draw(with: .cropBox, to: ctx)
         ctx.restoreGState();
     }
 }
 
 class KMPDFThumbnialPageView: NSView {
     
-    var myPage: PDFPage!
-    var page: PDFPage {
+    var myPage: CPDFPage!
+    var page: CPDFPage {
         get {
             myPage
         }