|
@@ -38,9 +38,9 @@ class KMWatermarkPDFView: CPDFView {
|
|
|
// }
|
|
|
} else if let headerFooter = self.headerFooter {
|
|
|
super.draw(page, to: context)
|
|
|
- if let context = NSGraphicsContext.current?.cgContext {
|
|
|
+// if let context = NSGraphicsContext.current?.cgContext {
|
|
|
drawHeaderFooterPage(page, to: context)
|
|
|
- }
|
|
|
+// }
|
|
|
} else {
|
|
|
super.draw(page, to: context)
|
|
|
}
|
|
@@ -383,76 +383,137 @@ class KMWatermarkPDFView: CPDFView {
|
|
|
}
|
|
|
|
|
|
func drawHeaderFooterPage(_ page: CPDFPage, to context: CGContext) {
|
|
|
- guard let headerFooter = headerFooter else { return}
|
|
|
-
|
|
|
+
|
|
|
+ guard let headerFooter = headerFooter else { return }
|
|
|
+
|
|
|
let pageBounds = page.bounds(for: .cropBox)
|
|
|
-
|
|
|
NSGraphicsContext.saveGraphicsState()
|
|
|
- if let currentContext = NSGraphicsContext.current {
|
|
|
+
|
|
|
+ if context != nil {
|
|
|
NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
|
|
|
}
|
|
|
+
|
|
|
page.transform(context, for: .cropBox)
|
|
|
-
|
|
|
- let color = headerFooter.textColor
|
|
|
- let font = NSFont.boldSystemFont(ofSize: headerFooter.getTextFontSize())
|
|
|
-
|
|
|
+
|
|
|
+ let color = headerFooter.getTextColor()
|
|
|
+ let font = NSFont.boldSystemFont(ofSize: CGFloat(headerFooter.getTextFontSize()))
|
|
|
var style = NSMutableParagraphStyle()
|
|
|
style.alignment = .center
|
|
|
style.lineBreakMode = .byCharWrapping
|
|
|
+
|
|
|
+ var size = NSZeroSize
|
|
|
+ var attributes: [NSAttributedString.Key: Any] = [
|
|
|
+ .paragraphStyle: style,
|
|
|
+ .foregroundColor: color,
|
|
|
+ .font: font
|
|
|
+ ]
|
|
|
+
|
|
|
+ let index = self.document?.index(for: page) ?? 0
|
|
|
+
|
|
|
+ var topOffset: CGFloat = 0
|
|
|
+ var bottomOffset: CGFloat = 0
|
|
|
+
|
|
|
+ let topLeftString = headerFooter.topLeftString
|
|
|
+ var tString = replacingOldString(topLeftString, currentPage: Int(index))
|
|
|
+
|
|
|
+ size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
|
+ options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
|
+ attributes: attributes).size
|
|
|
+
|
|
|
+ var posY = min(pageBounds.size.height - (headerFooter.topMargin), pageBounds.size.height - size.height)
|
|
|
+ tString.draw(in: CGRect(x: headerFooter.leftMargin, y: posY, width: size.width, height: size.height), withAttributes: attributes)
|
|
|
+
|
|
|
+
|
|
|
+ let topCenterString = headerFooter.topCenterString
|
|
|
+ tString = replacingOldString(topCenterString, currentPage: Int(index))
|
|
|
+
|
|
|
+ size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
|
+ options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
|
+ attributes: attributes).size
|
|
|
+
|
|
|
+ posY = min(pageBounds.size.height - (headerFooter.topMargin), pageBounds.size.height - size.height)
|
|
|
+ tString.draw(in: CGRect(x: pageBounds.size.width/2 - size.width/2, y: posY, width: size.width, height: size.height), withAttributes: attributes)
|
|
|
|
|
|
- var size = CGSize.zero
|
|
|
- var dictionary = [NSAttributedString.Key: Any]()
|
|
|
-
|
|
|
- dictionary[.paragraphStyle] = style
|
|
|
- dictionary[.foregroundColor] = color
|
|
|
- dictionary[.font] = font
|
|
|
+
|
|
|
+ let topRightString = headerFooter.topRightString
|
|
|
+ tString = replacingOldString(topRightString, currentPage: Int(index))
|
|
|
+
|
|
|
+ size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
|
+ options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
|
+ attributes: attributes).size
|
|
|
+
|
|
|
+ posY = min(pageBounds.size.height - (headerFooter.topMargin), pageBounds.size.height - size.height)
|
|
|
+ tString.draw(in: CGRect(x: pageBounds.size.width - (headerFooter.rightMargin) - size.width,
|
|
|
+ y: posY,
|
|
|
+ width: size.width,
|
|
|
+ height: size.height),
|
|
|
+ withAttributes: attributes)
|
|
|
+
|
|
|
+
|
|
|
+ let bottomLeftString = headerFooter.bottomLeftString
|
|
|
+ tString = replacingOldString(bottomLeftString, currentPage: Int(index))
|
|
|
+
|
|
|
+ size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
|
+ options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
|
+ attributes: attributes).size
|
|
|
+
|
|
|
+ posY = max((headerFooter.bottomMargin) - size.height, 0)
|
|
|
+ tString.draw(in: CGRect(x: headerFooter.leftMargin, y: posY, width: size.width, height: size.height), withAttributes: attributes)
|
|
|
|
|
|
- if let index = self.document?.index(for: page) {
|
|
|
- let topOffset: CGFloat = 0
|
|
|
- let bottomOffset: CGFloat = 0
|
|
|
-
|
|
|
- if headerFooter.topLeftString.count > 0 {
|
|
|
- let tString = self.replacingOldString(headerFooter.topLeftString, currentPage: Int(index))
|
|
|
-
|
|
|
- size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
|
- options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
|
- attributes: dictionary).size
|
|
|
- let posY = min(pageBounds.size.height - (headerFooter.topMargin), pageBounds.size.height - size.height)
|
|
|
- tString.draw(in: CGRect(x: headerFooter.leftMargin,
|
|
|
- y: posY,
|
|
|
- width: size.width,
|
|
|
- height: size.height),
|
|
|
- withAttributes: dictionary)
|
|
|
- }
|
|
|
-
|
|
|
- // Repeat the similar code for other strings (topCenter, topRight, bottomLeft, bottomCenter, bottomRight)
|
|
|
-
|
|
|
- // Draw horizontal and vertical lines
|
|
|
- if let context = NSGraphicsContext.current?.cgContext {
|
|
|
- context.setStrokeColor(NSColor(red: 51.0/255.0, green: 186.0/255.0, blue: 234.0/255.0, alpha: 1.0).cgColor)
|
|
|
- context.setLineWidth(1.0)
|
|
|
-
|
|
|
- context.move(to: CGPoint(x: 0, y: headerFooter.bottomMargin + bottomOffset))
|
|
|
- context.addLine(to: CGPoint(x: pageBounds.size.width, y: headerFooter.bottomMargin + bottomOffset))
|
|
|
-
|
|
|
- context.move(to: CGPoint(x: headerFooter.leftMargin, y: 0))
|
|
|
- context.addLine(to: CGPoint(x: headerFooter.leftMargin, y: pageBounds.size.height))
|
|
|
-
|
|
|
- context.move(to: CGPoint(x: pageBounds.size.width - (headerFooter.rightMargin), y: 0))
|
|
|
- context.addLine(to: CGPoint(x: pageBounds.size.width - (headerFooter.rightMargin), y: pageBounds.size.height))
|
|
|
-
|
|
|
- context.move(to: CGPoint(x: 0, y: pageBounds.size.height - (headerFooter.topMargin) - topOffset))
|
|
|
- context.addLine(to: CGPoint(x: pageBounds.size.width, y: pageBounds.size.height - (headerFooter.topMargin ) - topOffset))
|
|
|
-
|
|
|
- let dashArray: [CGFloat] = [8, 1]
|
|
|
- context.setLineDash(phase: 0, lengths: dashArray)
|
|
|
- context.drawPath(using: .stroke)
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ let bottomCenterString = headerFooter.bottomCenterString
|
|
|
+ tString = replacingOldString(bottomCenterString, currentPage: Int(index))
|
|
|
+
|
|
|
+ size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
|
+ options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
|
+ attributes: attributes).size
|
|
|
+
|
|
|
+ posY = max((headerFooter.bottomMargin) - size.height, 0)
|
|
|
+ tString.draw(in: CGRect(x: pageBounds.size.width/2 - size.width/2,
|
|
|
+ y: posY,
|
|
|
+ width: size.width,
|
|
|
+ height: size.height),
|
|
|
+ withAttributes: attributes)
|
|
|
|
|
|
+
|
|
|
+ let bottomRightString = headerFooter.bottomRightString
|
|
|
+ tString = replacingOldString(bottomRightString, currentPage: Int(index))
|
|
|
+
|
|
|
+ size = tString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
|
|
|
+ options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
|
+ attributes: attributes).size
|
|
|
+
|
|
|
+ posY = max((headerFooter.bottomMargin) - size.height, 0)
|
|
|
+ tString.draw(in: CGRect(x: pageBounds.size.width - (headerFooter.rightMargin) - size.width,
|
|
|
+ y: posY,
|
|
|
+ width: size.width,
|
|
|
+ height: size.height),
|
|
|
+ withAttributes: attributes)
|
|
|
+
|
|
|
+
|
|
|
+ context.setStrokeColor(NSColor(red: 51.0/255.0, green: 186.0/255.0, blue: 234.0/255.0, alpha: 1.0).cgColor)
|
|
|
+ context.setLineWidth(1.0)
|
|
|
+
|
|
|
+ context.move(to: CGPoint(x: 0, y: headerFooter.bottomMargin + bottomOffset))
|
|
|
+ context.addLine(to: CGPoint(x: pageBounds.size.width, y: headerFooter.bottomMargin + bottomOffset))
|
|
|
+
|
|
|
+ context.move(to: CGPoint(x: headerFooter.leftMargin, y: 0))
|
|
|
+ context.addLine(to: CGPoint(x: headerFooter.leftMargin, y: pageBounds.size.height))
|
|
|
+
|
|
|
+ context.move(to: CGPoint(x: pageBounds.size.width - (headerFooter.rightMargin), y: 0))
|
|
|
+ context.addLine(to: CGPoint(x: pageBounds.size.width - (headerFooter.rightMargin), y: pageBounds.size.height))
|
|
|
+
|
|
|
+ context.move(to: CGPoint(x: 0, y: pageBounds.size.height - (headerFooter.topMargin) - topOffset))
|
|
|
+ context.addLine(to: CGPoint(x: pageBounds.size.width, y: pageBounds.size.height - (headerFooter.topMargin) - topOffset))
|
|
|
+
|
|
|
+ let arr: [CGFloat] = [8, 1]
|
|
|
+ context.setLineDash(phase: 0, lengths: arr)
|
|
|
+ context.drawPath(using: .stroke)
|
|
|
+
|
|
|
NSGraphicsContext.restoreGraphicsState()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func replacingOldString(_ oldString: String?, currentPage page: Int) -> String {
|
|
|
guard let oldString = oldString else {
|
|
|
return ""
|