Browse Source

对照结果 - 导出 PDF 对照文档

zhudongyong 2 years ago
parent
commit
4c0360d3d0

+ 4 - 0
KdanAutoTest/KdanAuto.xcodeproj/project.pbxproj

@@ -14,6 +14,7 @@
 		240550FF2952F42F00B95E0B /* ComPDFKit_Conversion.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 2451F58A2952E4E50049FA9C /* ComPDFKit_Conversion.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
 		240934B42992735200839CC8 /* FileConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240934B32992735200839CC8 /* FileConverter.swift */; };
 		240934B72992737200839CC8 /* ImageProcess.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240934B62992737200839CC8 /* ImageProcess.swift */; };
+		2411D9CD29ABAF36009DE4A8 /* PDFKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2411D9CC29ABAF36009DE4A8 /* PDFKit.framework */; };
 		242F966D298BAE2200CFF56C /* Toggle_off.png in Resources */ = {isa = PBXBuildFile; fileRef = 242F966A298BAE2200CFF56C /* Toggle_off.png */; };
 		242F966E298BAE2200CFF56C /* Toggle_on.png in Resources */ = {isa = PBXBuildFile; fileRef = 242F966B298BAE2200CFF56C /* Toggle_on.png */; };
 		242F966F298BAE2200CFF56C /* Toggle_half.png in Resources */ = {isa = PBXBuildFile; fileRef = 242F966C298BAE2200CFF56C /* Toggle_half.png */; };
@@ -120,6 +121,7 @@
 		240509B62943479800B501B2 /* ProcessThumbnal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProcessThumbnal.swift; sourceTree = "<group>"; };
 		240934B32992735200839CC8 /* FileConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileConverter.swift; sourceTree = "<group>"; };
 		240934B62992737200839CC8 /* ImageProcess.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageProcess.swift; sourceTree = "<group>"; };
+		2411D9CC29ABAF36009DE4A8 /* PDFKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PDFKit.framework; path = System/Library/Frameworks/PDFKit.framework; sourceTree = SDKROOT; };
 		242F966A298BAE2200CFF56C /* Toggle_off.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Toggle_off.png; sourceTree = "<group>"; };
 		242F966B298BAE2200CFF56C /* Toggle_on.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Toggle_on.png; sourceTree = "<group>"; };
 		242F966C298BAE2200CFF56C /* Toggle_half.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Toggle_half.png; sourceTree = "<group>"; };
@@ -196,6 +198,7 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				2411D9CD29ABAF36009DE4A8 /* PDFKit.framework in Frameworks */,
 				2451F58B2952E4E50049FA9C /* ComPDFKit_Conversion.framework in Frameworks */,
 				240509B329433CDE00B501B2 /* QuickLookThumbnailing.framework in Frameworks */,
 				240509B429433CDE00B501B2 /* QuickLook.framework in Frameworks */,
@@ -244,6 +247,7 @@
 		240509AF29433CDE00B501B2 /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				2411D9CC29ABAF36009DE4A8 /* PDFKit.framework */,
 				240509B129433CDE00B501B2 /* QuickLook.framework */,
 				240509B029433CDE00B501B2 /* QuickLookThumbnailing.framework */,
 				240509B229433CDE00B501B2 /* QuickLookUI.framework */,

BIN
KdanAutoTest/KdanAuto.xcodeproj/project.xcworkspace/xcuserdata/zhudongyong.xcuserdatad/UserInterfaceState.xcuserstate


+ 57 - 18
KdanAutoTest/KdanAuto/Class/CompareViewController/CompareViewController.swift

@@ -37,30 +37,69 @@ class CompareViewController: NSViewController, NSTableViewDataSource, NSTableVie
     @IBAction func exportAction(_ sender:NSButton) {
         let savePanel = NSSavePanel.init();
         let checkBox = NSButton(checkboxWithTitle: "过滤无差异项", target: self, action: nil);
+        checkBox.frame = CGRectMake(0, 0, 150, 45);
         
         savePanel.allowedContentTypes = [.pdf]
         savePanel.accessoryView = checkBox
         
         if (savePanel.runModal() == .OK) {
-            let url = NSURL.init(fileURLWithPath: "");
-            let pdf = PDFDocument.init(url: url as URL);
+            let url = savePanel.url!;
+            NSLog("\(url)")
+            let pdf = PDFDocument.init();
+            let needFilter = checkBox.state == .on
             
-            //            for ( pageCount = 0; pageCount < _photoArray.count; pageCount++) {
-            //                BOOL isDir;
-            //                NSString *filePath = _photoArray[pageCount];
-            //                if ( [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir] && !isDir) {
-            //                    @autoreleasepool {
-            //                        NSImage *image = [[NSImage alloc] initWithContentsOfFile:filePath];
-            //                        PDFPage *page = [[PDFPage alloc] initWithImage:image];
-            //                        PDFPage *copyPage = [page copy];
-            //                        [pdf insertPage:copyPage atIndex: [pdf pageCount]];
-            //
-            //                        [copyPage release];
-            //                        [page release];
-            //                        [image release];
-            //                    }
-            //                }
-            //            }
+            processNextImages(pdf, index: 0, url: url, needFilter: needFilter)
+        }
+    }
+    
+    // Save PDF
+    func processNextImages(_ pdf:PDFDocument, index:Int32, url:URL, needFilter:Bool) -> Void {
+        let file = _files[0] as! NSMutableDictionary
+        
+        NSLog("处理第\(index)页")
+        if (!needFilter) {
+            let cell = ImageCompareCellView.shared()
+            cell?.frame = CGRectMake(0, 0, CGRectGetWidth(self.listView.frame), 900);
+            cell?.setFileInfo(file);
+            
+            let image = cell?.processCompareImage()
+            if (image != nil) {
+                let page = PDFPage.init(image: image!)
+                let nPage = page?.copy() as! PDFPage
+                pdf.insert(nPage, at: pdf.pageCount)
+            }
+            
+            let degree = file.degree()
+            if (abs(degree - 100) > 0) {
+                let image = cell?.processCoverImage()
+                if (image != nil) {
+                    let page = PDFPage.init(image: image!)
+                    let nPage = page?.copy() as! PDFPage
+                    pdf.insert(nPage, at: pdf.pageCount)
+                }
+            }
+        }else {
+            let cell = ImageCompareCellView.shared()
+            cell?.frame = CGRectMake(0, 0, CGRectGetWidth(self.listView.frame), 900);
+            cell?.setFileInfo(file);
+            
+            let degree = file.degree()
+            if (abs(degree - 100) > 0) {
+                let image = cell?.processCoverImage()
+                if (image != nil) {
+                    let page = PDFPage.init(image: image!)
+                    let nPage = page?.copy() as! PDFPage
+                    pdf.insert(nPage, at: pdf.pageCount)
+                }
+            }
+        }
+        
+        if ((index+1) < _files.count) {
+            DispatchQueue.main.asyncAfter(deadline: DispatchTime.init(uptimeNanoseconds: 100)) {
+                self.processNextImages(pdf, index: index+1, url: url, needFilter: needFilter);
+            };
+        }else {
+            pdf.write(to: url);
         }
     }
     

+ 69 - 3
KdanAutoTest/KdanAuto/Class/CompareViewController/ImageCompareCellView.swift

@@ -79,7 +79,7 @@ class ImageCompareCellView : NSTableCellView {
                 let lastPathComponent = NSString(string: _fileInfo.refFilePath()!).lastPathComponent
                 setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]\(_fileInfo.fileName())/\(lastPathComponent)"))
             }
-
+            
             let degree = _fileInfo.degree()
             if degree == -1 {
                 _degreeInfoLbl.stringValue = "文件不存在"
@@ -146,8 +146,8 @@ class ImageCompareCellView : NSTableCellView {
             self._imageView04.image = nil;
             _imageView04.isHidden = _showCoverBtn.state == .off || _segmentedControl.indexOfSelectedItem != 1
             
-//            let image2 = NSImage.init(contentsOfFile: resultPath)
-//            _imageView04.image = image2;
+            //            let image2 = NSImage.init(contentsOfFile: resultPath)
+            //            _imageView04.image = image2;
             let degree = _fileInfo.degree()
             if (abs(degree - 100) > 0) {
                 _activityView.isHidden = false
@@ -181,6 +181,72 @@ class ImageCompareCellView : NSTableCellView {
         }
     }
     
+    func processCompareImage() -> NSImage? {
+        _imageView03.image = nil;
+        _imageView04.image = nil;
+        
+        let comparePath = _fileInfo.comparePath()
+        let resultPath = _fileInfo.resultPath()
+        
+        let image = NSImage.init(contentsOfFile: comparePath)
+        _imageView01.image = image;
+        
+        let image2 = NSImage.init(contentsOfFile: resultPath)
+        _imageView02.image = image2;
+        
+        _showCoverBtn.isHidden = true;
+        _segmentedControl.isHidden = true;
+        _degreeInfoLbl.isHidden = true;
+        
+        let nimage = self.imageFroView(self)
+        return nimage
+    }
+    
+    func processCoverImage() -> NSImage? {
+        let comparePath = _fileInfo.comparePath()
+        let resultPath = _fileInfo.resultPath()
+        
+        let image = NSImage.init(contentsOfFile: comparePath)
+        _imageView03.image = image;
+        self._imageView04.image = nil;
+        _imageView04.isHidden = false
+        
+        var image2 = nil as NSImage?
+        
+        let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as! String
+        
+        if FileManager.default.fileExists(atPath: coverPath) {
+            image2 = NSImage.init(contentsOfFile: coverPath)
+        }else {
+            image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)
+            
+            if nil != image2 {
+                let rep = NSBitmapImageRep.init(cgImage: image2!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
+                let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
+                
+                let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
+                try? data!.write(to: url)
+            }
+        }
+        
+        self._imageView04.image = image2
+        self._activityView.isHidden = true
+        
+        self._showCoverBtn.isHidden = true;
+        self._segmentedControl.isHidden = true;
+        self._degreeInfoLbl.isHidden = true;
+        
+        let nimage = self.imageFroView(self)
+        return nimage
+    }
+    
+    func imageFroView(_ view:NSView) -> NSImage? {
+        let data = view.dataWithPDF(inside: view.frame)
+        let image = NSImage.init(data: data);
+        
+        return image ?? nil;
+    }
+    
     @IBAction func showCoverAction(_ sender:NSButton) {
         if sender.state == .on {
             _showCoverBtn.title = "隐藏覆盖层"

+ 2 - 2
KdanAutoTest/KdanAuto/Class/CompareViewController/ImageCompareCellView.xib

@@ -14,9 +14,9 @@
             <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
             <subviews>
                 <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ZfV-Hf-yE5">
-                    <rect key="frame" x="0.0" y="777" width="669" height="21"/>
+                    <rect key="frame" x="0.0" y="777" width="678" height="21"/>
                     <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
-                    <textFieldCell key="cell" lineBreakMode="clipping" title="Label" id="yAG-7D-A8X">
+                    <textFieldCell key="cell" lineBreakMode="truncatingMiddle" title="Label" id="yAG-7D-A8X">
                         <font key="font" metaFont="systemMedium" size="13"/>
                         <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>

+ 4 - 0
KdanAutoTest/KdanAuto/KdanAutoDebug.entitlements

@@ -2,6 +2,8 @@
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
+	<key>com.apple.security.app-sandbox</key>
+	<true/>
 	<key>com.apple.security.cs.allow-dyld-environment-variables</key>
 	<true/>
 	<key>com.apple.security.cs.allow-jit</key>
@@ -14,5 +16,7 @@
 	<true/>
 	<key>com.apple.security.cs.disable-library-validation</key>
 	<true/>
+	<key>com.apple.security.files.user-selected.read-write</key>
+	<true/>
 </dict>
 </plist>