Explorar el Código

【打印】旋转后打印跑版问题修复

lizhe hace 1 año
padre
commit
75debc1b24

+ 36 - 3
PDF Office/PDF Master/Class/PDFTools/Print/KMPrintWindowController.swift

@@ -151,7 +151,7 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
                     document = PDFDocument(url: URL(fileURLWithPath: filePath))!
                 }
             } else if inputDocument is PDFDocument {
-                document = inputDocument as! PDFDocument
+                document = inputDocument as? PDFDocument
             }
         }
 
@@ -166,8 +166,8 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
             
             let printPresent = KMPrintPresenter()
             let filePath = KMPrintPresenter.fetchSaveFilePath("")
-            let paperSize = (document?.page(at: 0)?.bounds(for: .cropBox).size)
-            let context: CGContext = printPresent.createContext(filePath, paperSize!)
+            let paperSize = (document?.page(at: 0)?.bounds(for: .cropBox).size) ?? CGSizeZero
+            let context: CGContext = printPresent.createContext(filePath, paperSize)
             var pages: [PDFPage] = []
             for index in inputPageRange.selectPages {
                 let page = document?.page(at: index)
@@ -178,7 +178,40 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
             
             for drawPage in pages {
                 context.beginPDFPage(nil)
+                
+                context.saveGState()
+                
+                var pageSize = drawPage.bounds(for: .cropBox).size
+
+                if (drawPage.rotation == 90 || drawPage.rotation == 270) {
+                    let height = pageSize.height
+                    pageSize.height = pageSize.width
+                    pageSize.width = height
+                }
+                
+                // 检查要自适应的大小是否大于参考矩形的大小
+                let maxWidth = paperSize.width
+                let maxHeight = paperSize.height
+                var scaledSize = pageSize
+
+                var ratio = 1.0
+                if pageSize.width > maxWidth || pageSize.height > maxHeight {
+                    let widthRatio = maxWidth / pageSize.width
+                    let heightRatio = maxHeight / pageSize.height
+                    ratio = min(widthRatio, heightRatio)
+                    scaledSize = CGSize(width: pageSize.width * ratio, height: pageSize.height * ratio)
+                }
+
+                // 计算自适应后的矩形大小和位置
+                let scaledWidth = scaledSize.width
+                let scaledHeight = scaledSize.height
+                let scaledRect = CGRect(x: (paperSize.width - scaledWidth) / 2, y: (paperSize.height - scaledHeight) / 2, width: scaledWidth, height: scaledHeight)
+
+                context.translateBy(x: scaledRect.origin.x, y: scaledRect.origin.y)
+                context.scaleBy(x: ratio, y: ratio)
+                
                 drawPage.draw(with: .cropBox, to: context)
+                context.restoreGState()
                 context.endPDFPage()
             }
             context.closePDF()

+ 1 - 2
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/Thumbnail/KMPDFThumbnailItem.xib

@@ -1,12 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
-        <deployment identifier="macosx"/>
         <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
-        <customObject id="-2" userLabel="File's Owner" customClass="KMPDFThumbnailItem" customModule="PDF_Office" customModuleProvider="target">
+        <customObject id="-2" userLabel="File's Owner" customClass="KMPDFThumbnailItem" customModule="PDF_Master" customModuleProvider="target">
             <connections>
                 <outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
             </connections>

+ 2 - 3
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/Thumbnail/KMThumbnailViewController.xib

@@ -1,12 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
-        <deployment identifier="macosx"/>
         <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
-        <customObject id="-2" userLabel="File's Owner" customClass="KMThumbnailViewController" customModule="PDF_Office" customModuleProvider="target">
+        <customObject id="-2" userLabel="File's Owner" customClass="KMThumbnailViewController" customModule="PDF_Master" customModuleProvider="target">
             <connections>
                 <outlet property="doublePageBtn" destination="y7O-T9-4pt" id="PlC-YF-qgf"/>
                 <outlet property="headerView" destination="ByL-P0-CBo" id="eV2-UI-ZCZ"/>
@@ -81,7 +80,7 @@ Gw
                         <constraint firstItem="y7O-T9-4pt" firstAttribute="centerY" secondItem="ByL-P0-CBo" secondAttribute="centerY" id="wjz-OJ-KhO"/>
                     </constraints>
                 </customView>
-                <customView translatesAutoresizingMaskIntoConstraints="NO" id="0Ot-Gw-V0A" customClass="KMPageEditThumbnailView" customModule="PDF_Office" customModuleProvider="target">
+                <customView translatesAutoresizingMaskIntoConstraints="NO" id="0Ot-Gw-V0A" customClass="KMPageEditThumbnailView" customModule="PDF_Master" customModuleProvider="target">
                     <rect key="frame" x="0.0" y="0.0" width="280" height="396"/>
                 </customView>
             </subviews>