Ver código fonte

【注释】补充文档加载时进行Table注释转换

wanjun 10 meses atrás
pai
commit
43af46fc14

+ 45 - 0
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -195,6 +195,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
             mainViewController?.document = pdfDocument
         }
         
+        convertNotesUsingPDFDocument((mainViewController?.document)!)
         self.view = mainViewController?.view
         
         if let currentBrowser = currentWindowController?.browser {
@@ -892,6 +893,50 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
         KMPrint("showWindow")
     }
     
+    func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument) {
+        mainViewController!.beginProgressSheet(withMessage: NSLocalizedString("Converting notes", comment: "Message for progress sheet").appending("..."), maxValue: 0)
+        
+        let count = pdfDocument.pageCount
+        let pdfView = mainViewController?.listView
+        
+        for i in 0..<count {
+            let page = pdfDocument.page(at: i)
+            
+            var addAnnotations = [CPDFAnnotation]()
+            var removeAnnotations = [CPDFAnnotation]()
+            
+            for annotation in page?.annotations ?? [] {
+                var newAnnotation: CPDFAnnotation?
+                if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
+                    let table = KMTableAnnotation(KMNoteBounds: annotation.bounds, document: pdfView!.document)
+                    table.border = annotation.border
+                    table.color = annotation.color
+                    table.createForm(withList: annotation.contents, andPaths: inkAnnotation.bezierPaths())
+                    table.updateAppearanceInk(withIsAdd: false)
+                    newAnnotation = table
+                }
+                if let newAnnotation = newAnnotation {
+                    addAnnotations.append(newAnnotation)
+                    removeAnnotations.append(annotation)
+                }
+            }
+            
+            for i in 0..<addAnnotations.count {
+                let newAnnotation = addAnnotations[i]
+                let annotation = removeAnnotations[i]
+                
+                // this is only to make sure markup annotations generate the lineRects, for thread safety
+                pdfView!.addAnnotation(with: newAnnotation, to: page)
+                pdfView!.remove(annotation)
+            }
+        }
+        
+        mainViewController!.dismissProgressSheet()
+        
+        pdfView?.undoManager?.removeAllActions()
+        undoManager?.removeAllActions()
+    }
+    
     // MARK: - Private Methods
     
     private func _km_write(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws {