-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
PDFExternalPageObject.swift
47 lines (40 loc) · 1.31 KB
/
PDFExternalPageObject.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// PDFExternalPageObject.swift
// TPPDF
//
// Created by Philip Niedertscheider on 12.08.19.
//
#if os(iOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif
class PDFExternalPageObject: PDFRenderObject {
var page: CGPDFPage
init(page: CGPDFPage) {
self.page = page
}
override func calculate(generator _: PDFGenerator, container: PDFContainer) throws -> [PDFLocatedRenderObject] {
frame = page.getBoxRect(.mediaBox)
return [
(container, self)
]
}
override func draw(generator _: PDFGenerator, container _: PDFContainer, in context: PDFContext) throws {
if !context.currentPageContainsDrawnContent {
// If page is empty and no page is active, we are good to go
// Otherwise we need to delete the empty page
if context.hasActivePage {
context.resetDelayedCommands()
}
} else {
// if something has been drawn on the page, end the and draw the external PDF page on a new page
PDFContextGraphics.endPDFPage(in: context)
}
PDFContextGraphics.beginPDFPage(in: context, for: frame, flipped: false)
context.saveGState()
context.drawPDFPage(page)
context.restoreGState()
applyAttributes(in: context)
}
}