|
@@ -277,78 +277,78 @@ extension KMPrintWindowController {
|
|
|
document = PDFDocument.init(url: inputData!)!
|
|
|
}
|
|
|
|
|
|
- //页面选取
|
|
|
- if ((inputPageRange.type == .currentPage ||
|
|
|
- inputPageRange.type == .custom) &&
|
|
|
- inputPageRange.selectPages.count != 0) {
|
|
|
-
|
|
|
- let printPresent = KMPrintPresenter()
|
|
|
- let filePath = KMPrintPresenter.fetchSaveFilePath("")
|
|
|
- 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)
|
|
|
- if page != nil {
|
|
|
- pages.append(page!)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for drawPage in pages {
|
|
|
- context.beginPDFPage(nil)
|
|
|
-
|
|
|
- context.saveGState()
|
|
|
+ if document != nil {
|
|
|
+ //页面选取
|
|
|
+ if ((inputPageRange.type == .currentPage ||
|
|
|
+ inputPageRange.type == .custom) &&
|
|
|
+ inputPageRange.selectPages.count != 0) {
|
|
|
|
|
|
- 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 printPresent = KMPrintPresenter()
|
|
|
+ let filePath = KMPrintPresenter.fetchSaveFilePath("")
|
|
|
+ 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)
|
|
|
+ if page != nil {
|
|
|
+ pages.append(page!)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 检查要自适应的大小是否大于参考矩形的大小
|
|
|
- let maxWidth = paperSize.width
|
|
|
- let maxHeight = paperSize.height
|
|
|
- var scaledSize = pageSize
|
|
|
+ for drawPage in pages {
|
|
|
+ context.beginPDFPage(nil)
|
|
|
+
|
|
|
+ context.saveGState()
|
|
|
+
|
|
|
+ var pageSize = drawPage.bounds(for: .cropBox).size
|
|
|
|
|
|
- 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)
|
|
|
- }
|
|
|
+ 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)
|
|
|
+ // 计算自适应后的矩形大小和位置
|
|
|
+ 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)
|
|
|
+ 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()
|
|
|
|
|
|
- drawPage.draw(with: .cropBox, to: context)
|
|
|
- context.restoreGState()
|
|
|
- context.endPDFPage()
|
|
|
+ document = PDFDocument(url: URL(fileURLWithPath: filePath))!
|
|
|
}
|
|
|
- context.closePDF()
|
|
|
-
|
|
|
- document = PDFDocument(url: URL(fileURLWithPath: filePath))!
|
|
|
- }
|
|
|
|
|
|
- if document != nil {
|
|
|
if inputDocument != nil {
|
|
|
document?.unlock(withPassword: password)
|
|
|
}
|
|
|
|
|
|
if (inputDocument!.allowsPrinting == false) {
|
|
|
- KMPasswordInputWindow.openWindow(window: NSApp.mainWindow!, type: .owner, url: (inputDocument!.documentURL)!) { result , password in
|
|
|
+ KMPasswordInputWindow.openWindow(window: NSApp.mainWindow!, type: .owner, url: (inputDocument!.documentURL)!) { result , resultPassword in
|
|
|
if (result == .cancel) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- document!.unlock(withPassword: password!)
|
|
|
+ document!.unlock(withPassword: resultPassword ?? "")
|
|
|
KMPrintWindowController.openPrintView(document: document)
|
|
|
}
|
|
|
return
|