-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
PDFInfo.swift
119 lines (103 loc) · 3.59 KB
/
PDFInfo.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// PDFInfo.swift
// TPPDF
//
// Created by Zheng-Xiang Ke on 2016/12/15.
//
#if os(iOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif
/// Information saved in PDF file metadata
public class PDFInfo {
/// Title of document
public var title: String
/// Author of document
public var author: String
/// Subject of document
public var subject: String
/// Keywords of document
public var keywords: [String]
/**
* The owner password of the PDF document
*
* If this password is set the document is encrypted; otherwise, the document will not be encrypted.
*/
public var ownerPassword: String?
/**
* The user password of the PDF document
*
* If the document is encrypted, then this value will be the user password for the document.
*/
public var userPassword: String?
/// Whether the document allows printing when unlocked with the user password.
public var allowsPrinting: Bool
/// Whether the document allows copying when unlocked with the user password.
public var allowsCopying: Bool
/**
* Creates a new object to manage the information metadata of a ``PDFDocument``
*
* - Parameters:
* - title: See ``PDFInfo/title`` for details.
* - author: See ``PDFInfo/author`` for details.
* - subject: See ``PDFInfo/subject`` for details.
* - keywords: See ``PDFInfo/keywords`` for details.
* - ownerPassword: See ``PDFInfo/ownerPassword`` for details.
* - userPassword: See ``PDFInfo/userPassword`` for details.
* - allowsPrinting: See ``PDFInfo/allowsPrinting`` for details.
* - allowsCopying: See ``PDFInfo/allowsCopying`` for details.
*/
public init(
title: String = "Title",
author: String = "Author",
subject: String = "Subject",
keywords: [String] = ["tppdf", "pdf", "generator"],
ownerPassword: String? = nil,
userPassword: String? = nil,
allowsPrinting: Bool = true,
allowsCopying: Bool = true
) {
self.title = title
self.author = author
self.subject = subject
self.keywords = keywords
self.ownerPassword = ownerPassword
self.userPassword = userPassword
self.allowsPrinting = allowsPrinting
self.allowsCopying = allowsCopying
}
/**
* Generates a dictionary of metadata with the following information:
*
* - Title
* - Author
* - Subject
* - Keywords
* - Allows Printing
* - Allows Copying
* - Creator
* - Owner Password
* - User Password
*/
func generate() -> [AnyHashable: Any] {
var documentInfo: [AnyHashable: Any] = [
kCGPDFContextTitle as String: title,
kCGPDFContextAuthor as String: author,
kCGPDFContextSubject as String: subject,
kCGPDFContextKeywords as String: keywords,
kCGPDFContextAllowsPrinting as String: allowsPrinting,
kCGPDFContextAllowsCopying as String: allowsCopying
]
var creator = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "TPPDF"
creator += " " + (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0.0")
documentInfo[kCGPDFContextCreator as String] = creator
if let ownerPassword = ownerPassword {
documentInfo[kCGPDFContextOwnerPassword as String] = ownerPassword
}
if let userPassword = userPassword {
documentInfo[kCGPDFContextUserPassword as String] = userPassword
}
return documentInfo
}
}