根据苹果的文档的说法,CGPDFDocument有一个叫做documentAttributes的变量
var documentAttributes: [AnyHashable : Any]? { get set }在Swift中,我很难理解如何使用它获取或设置PDF的文档属性。Xcode并不提供它作为一个点之后的自动完成,例如myPDF.documentAttributes。
你是怎么用的?我正在尝试获取/设置文档元数据,如Author、Creator、Subject。
发布于 2017-05-26 02:51:33
再看一下你提供的链接。不是CGPDFDocument,而是Quartz.PDFDocument。以下是访问它的一种方法:
let pdfDoc = PDFDocument(url: URL(fileURLWithPath: "/path/to/file.pdf"))!
if let attributes = pdfDoc.documentAttributes {
let keys = attributes.keys // the set of keys differ from file to file
let firstKey = keys[keys.startIndex] // get the first key, whatever the turns out to be
// since Dictionaries are not ordered
print("\(firstKey): \(attributes[firstKey]!)")
print("Title: \(attributes["Title"])")
}每个文件的密钥列表各不相同,因此当密钥不可用时,您需要检查每个密钥并处理nil。
若要更改属性,请执行以下操作:
pdfDoc.documentAttributes?["Title"] = "Cheese"
pdfDoc.write(to: URL(fileURLWithPath: "/path/to/file.pdf")) // save the PDF filehttps://stackoverflow.com/questions/44186746
复制相似问题