|
@@ -18,12 +18,13 @@ class KMMergeWindowController: KMBaseWindowController {
|
|
|
|
|
|
// var cancelAction: KMMergeWindowControllerCancelAction?
|
|
// var cancelAction: KMMergeWindowControllerCancelAction?
|
|
|
|
|
|
- var oldPDFDocument: PDFDocument = PDFDocument()
|
|
|
|
|
|
+ var oldPDFDocument: CPDFDocument = CPDFDocument()
|
|
var password: String = ""
|
|
var password: String = ""
|
|
|
|
|
|
var oriDucumentUrl: URL? {
|
|
var oriDucumentUrl: URL? {
|
|
didSet {
|
|
didSet {
|
|
- oldPDFDocument = PDFDocument(url: oriDucumentUrl!)!
|
|
|
|
|
|
+ guard let document = CPDFDocument(url: oriDucumentUrl!) else { return }
|
|
|
|
+ oldPDFDocument = document
|
|
oldPDFDocument.unlock(withPassword: self.password)
|
|
oldPDFDocument.unlock(withPassword: self.password)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -158,7 +159,7 @@ extension KMMergeWindowController {
|
|
// [self.nCancelVC setEnabled:NO];
|
|
// [self.nCancelVC setEnabled:NO];
|
|
// self.canMerge = NO;
|
|
// self.canMerge = NO;
|
|
//
|
|
//
|
|
- var rootPDFOutlineArray: [PDFOutline] = []
|
|
|
|
|
|
+ var rootPDFOutlineArray: [CPDFOutline] = []
|
|
var allPage = true //只有是全部才支持大纲的合并
|
|
var allPage = true //只有是全部才支持大纲的合并
|
|
for file in files {
|
|
for file in files {
|
|
if file.fetchSelectPages().count == 0 {
|
|
if file.fetchSelectPages().count == 0 {
|
|
@@ -172,7 +173,7 @@ extension KMMergeWindowController {
|
|
allPage = file.bAllPage
|
|
allPage = file.bAllPage
|
|
|
|
|
|
/*防止文件被地址变换后crash*/
|
|
/*防止文件被地址变换后crash*/
|
|
- guard let tDocument = PDFDocument(url: NSURL(fileURLWithPath: file.filePath) as URL) else {
|
|
|
|
|
|
+ guard let tDocument = CPDFDocument(url: NSURL(fileURLWithPath: file.filePath) as URL) else {
|
|
print("文件不存在")
|
|
print("文件不存在")
|
|
let alert = NSAlert.init()
|
|
let alert = NSAlert.init()
|
|
alert.alertStyle = .critical
|
|
alert.alertStyle = .critical
|
|
@@ -181,31 +182,30 @@ extension KMMergeWindowController {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- var outlineArray: [PDFOutline] = []
|
|
|
|
|
|
+ var outlineArray: [CPDFOutline] = []
|
|
|
|
|
|
// if file.isLocked {
|
|
// if file.isLocked {
|
|
tDocument.unlock(withPassword: file.password)
|
|
tDocument.unlock(withPassword: file.password)
|
|
// }
|
|
// }
|
|
|
|
|
|
- if tDocument.outlineRoot != nil {
|
|
|
|
- rootPDFOutlineArray.append((tDocument.outlineRoot)!)
|
|
|
|
- self.fetchAllOfChildren((tDocument.outlineRoot)!, containerArray: &outlineArray)
|
|
|
|
- outlineArray.removeObject((tDocument.outlineRoot)!)
|
|
|
|
|
|
+ if tDocument.outlineRoot() != nil {
|
|
|
|
+ rootPDFOutlineArray.append(tDocument.outlineRoot())
|
|
|
|
+ self.fetchAllOfChildren(tDocument.outlineRoot(), containerArray: &outlineArray)
|
|
|
|
+ outlineArray.removeObject(tDocument.outlineRoot())
|
|
} else {
|
|
} else {
|
|
- let rootOutline = PDFOutline.init()
|
|
|
|
- tDocument.outlineRoot = rootOutline
|
|
|
|
- if tDocument.outlineRoot != nil {
|
|
|
|
- rootPDFOutlineArray.append(tDocument.outlineRoot!)
|
|
|
|
|
|
+ let rootOutline = tDocument.setNewOutlineRoot()
|
|
|
|
+ if rootOutline != nil {
|
|
|
|
+ rootPDFOutlineArray.append(rootOutline!)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
for number in file.fetchSelectPages() {
|
|
for number in file.fetchSelectPages() {
|
|
- let page = tDocument.page(at: number - 1)
|
|
|
|
|
|
+ let page = tDocument.page(at: UInt(number - 1))
|
|
// if pageIndex != nil {
|
|
// if pageIndex != nil {
|
|
// self.oldPDFDocument.insert(page!, at: pageIndex!)
|
|
// self.oldPDFDocument.insert(page!, at: pageIndex!)
|
|
// pageIndex = pageIndex! + 1
|
|
// pageIndex = pageIndex! + 1
|
|
// } else {
|
|
// } else {
|
|
- self.oldPDFDocument.insert(page!, at: self.oldPDFDocument.pageCount)
|
|
|
|
|
|
+ self.oldPDFDocument.insertPageObject(page, at: self.oldPDFDocument.pageCount)
|
|
// }
|
|
// }
|
|
// self.insertIndexSet.addIndex:(self.pdfDocument.pageCount - 1)
|
|
// self.insertIndexSet.addIndex:(self.pdfDocument.pageCount - 1)
|
|
}
|
|
}
|
|
@@ -214,15 +214,15 @@ extension KMMergeWindowController {
|
|
|
|
|
|
let fileName = (files.first?.filePath.deletingPathExtension.lastPathComponent ?? "") + "_Merged"
|
|
let fileName = (files.first?.filePath.deletingPathExtension.lastPathComponent ?? "") + "_Merged"
|
|
DispatchQueue.main.async {
|
|
DispatchQueue.main.async {
|
|
- if self.oldPDFDocument.outlineRoot == nil {
|
|
|
|
- self.oldPDFDocument.outlineRoot = PDFOutline.init()
|
|
|
|
|
|
+ if self.oldPDFDocument.outlineRoot() == nil {
|
|
|
|
+ self.oldPDFDocument.setNewOutlineRoot()
|
|
}
|
|
}
|
|
// if allPage {
|
|
// if allPage {
|
|
var insertIndex = 0
|
|
var insertIndex = 0
|
|
for i in 0..<rootPDFOutlineArray.count {
|
|
for i in 0..<rootPDFOutlineArray.count {
|
|
let rootOutline = rootPDFOutlineArray[i]
|
|
let rootOutline = rootPDFOutlineArray[i]
|
|
for j in 0..<rootOutline.numberOfChildren {
|
|
for j in 0..<rootOutline.numberOfChildren {
|
|
- self.oldPDFDocument.outlineRoot?.insertChild(rootOutline.child(at: j)!, at: insertIndex)
|
|
|
|
|
|
+ self.oldPDFDocument.outlineRoot().insertChild(rootOutline.child(at: j)!, at: UInt(insertIndex))
|
|
insertIndex = insertIndex + 1
|
|
insertIndex = insertIndex + 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -232,7 +232,7 @@ extension KMMergeWindowController {
|
|
if self.oriDucumentUrl != nil {
|
|
if self.oriDucumentUrl != nil {
|
|
|
|
|
|
let newPath = self.oldPDFDocument.documentURL!.path
|
|
let newPath = self.oldPDFDocument.documentURL!.path
|
|
- var options: [PDFDocumentWriteOption : Any] = [:]
|
|
|
|
|
|
+ var options: [CPDFDocumentWriteOption : Any] = [:]
|
|
var success = false
|
|
var success = false
|
|
let password = self.password
|
|
let password = self.password
|
|
let pdf = self.oldPDFDocument
|
|
let pdf = self.oldPDFDocument
|
|
@@ -326,7 +326,7 @@ extension KMMergeWindowController {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- func fetchAllOfChildren(_ aOutline: PDFOutline, containerArray aMArray: inout [PDFOutline]) {
|
|
|
|
|
|
+ func fetchAllOfChildren(_ aOutline: CPDFOutline, containerArray aMArray: inout [CPDFOutline]) {
|
|
if !aMArray.contains(aOutline) {
|
|
if !aMArray.contains(aOutline) {
|
|
aMArray.append(aOutline)
|
|
aMArray.append(aOutline)
|
|
}
|
|
}
|
|
@@ -348,7 +348,7 @@ extension KMMergeWindowController {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- var pagesArray: [PDFPage] = []
|
|
|
|
|
|
+ var pagesArray: [CPDFPage] = []
|
|
let pageCount = self.oldPDFDocument.pageCount
|
|
let pageCount = self.oldPDFDocument.pageCount
|
|
for i in 0..<pageCount {
|
|
for i in 0..<pageCount {
|
|
pagesArray.append(self.oldPDFDocument.page(at: 0)!)
|
|
pagesArray.append(self.oldPDFDocument.page(at: 0)!)
|
|
@@ -358,16 +358,16 @@ extension KMMergeWindowController {
|
|
for i in 0..<pageCount {
|
|
for i in 0..<pageCount {
|
|
let page: KMMergePDFPage = KMMergePDFPage.init()
|
|
let page: KMMergePDFPage = KMMergePDFPage.init()
|
|
page.setBounds(CGRectMake(0, 0, size.width, size.height), for: .mediaBox)
|
|
page.setBounds(CGRectMake(0, 0, size.width, size.height), for: .mediaBox)
|
|
- page.drawingPage = pagesArray[i]
|
|
|
|
- self.oldPDFDocument.insert(page, at: i)
|
|
|
|
|
|
+ page.drawingPage = pagesArray[Int(i)]
|
|
|
|
+ self.oldPDFDocument.insertPageObject(page, at: i)
|
|
}
|
|
}
|
|
|
|
|
|
- if self.oldPDFDocument.outlineRoot != nil {
|
|
|
|
- let childCount = self.oldPDFDocument.outlineRoot?.numberOfChildren
|
|
|
|
- var outlineArray: [PDFOutline] = []
|
|
|
|
|
|
+ if self.oldPDFDocument.outlineRoot() != nil {
|
|
|
|
+ let childCount = self.oldPDFDocument.outlineRoot().numberOfChildren
|
|
|
|
+ var outlineArray: [CPDFOutline] = []
|
|
|
|
|
|
- for i in 0..<childCount! {
|
|
|
|
- outlineArray.append((self.oldPDFDocument.outlineRoot?.child(at: i))!)
|
|
|
|
|
|
+ for i in 0..<childCount {
|
|
|
|
+ outlineArray.append(self.oldPDFDocument.outlineRoot().child(at: i))
|
|
}
|
|
}
|
|
|
|
|
|
for outline in outlineArray {
|
|
for outline in outlineArray {
|
|
@@ -378,17 +378,17 @@ extension KMMergeWindowController {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-class KMMergePDFPage: PDFPage {
|
|
|
|
- var drawingPage: PDFPage?
|
|
|
|
|
|
+class KMMergePDFPage: CPDFPage {
|
|
|
|
+ var drawingPage: CPDFPage?
|
|
|
|
|
|
- override func draw(with box: PDFDisplayBox, to context: CGContext) {
|
|
|
|
|
|
+ override func draw(with box: CPDFDisplayBox, to context: CGContext!) {
|
|
super.draw(with: box, to: context)
|
|
super.draw(with: box, to: context)
|
|
-
|
|
|
|
|
|
+
|
|
let pageSize = self.bounds(for: .cropBox).size
|
|
let pageSize = self.bounds(for: .cropBox).size
|
|
self.drawPage(with: context, page: self.drawingPage!, pageSize: pageSize)
|
|
self.drawPage(with: context, page: self.drawingPage!, pageSize: pageSize)
|
|
}
|
|
}
|
|
-
|
|
|
|
- func drawPage(with context: CGContext, page: PDFPage, pageSize: CGSize) {
|
|
|
|
|
|
+
|
|
|
|
+ func drawPage(with context: CGContext, page: CPDFPage, pageSize: CGSize) {
|
|
var originalSize = page.bounds(for: .cropBox).size
|
|
var originalSize = page.bounds(for: .cropBox).size
|
|
|
|
|
|
// 如果页面的旋转角度为90或者270,宽高交换
|
|
// 如果页面的旋转角度为90或者270,宽高交换
|
|
@@ -408,13 +408,13 @@ class KMMergePDFPage: PDFPage {
|
|
|
|
|
|
if #available(macOS 10.12, *) {
|
|
if #available(macOS 10.12, *) {
|
|
page.draw(with: .cropBox, to: context)
|
|
page.draw(with: .cropBox, to: context)
|
|
- page.transformContext(for: .cropBox)
|
|
|
|
|
|
+ page.transform(context, for: .cropBox)
|
|
} else {
|
|
} else {
|
|
NSGraphicsContext.saveGraphicsState()
|
|
NSGraphicsContext.saveGraphicsState()
|
|
NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
|
|
NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
|
|
- page.draw(with: .cropBox)
|
|
|
|
|
|
+ page.draw(with: .cropBox, to: context)
|
|
NSGraphicsContext.restoreGraphicsState()
|
|
NSGraphicsContext.restoreGraphicsState()
|
|
- page.transformContext(for: .cropBox)
|
|
|
|
|
|
+ page.transform(context, for: .cropBox)
|
|
}
|
|
}
|
|
|
|
|
|
context.restoreGState()
|
|
context.restoreGState()
|