首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用documentAttributes of CGPDFDocument

如何使用documentAttributes of CGPDFDocument
EN

Stack Overflow用户
提问于 2017-05-25 17:47:04
回答 1查看 1.5K关注 0票数 2

根据苹果的文档的说法,CGPDFDocument有一个叫做documentAttributes的变量

代码语言:javascript
复制
var documentAttributes: [AnyHashable : Any]? { get set }

在Swift中,我很难理解如何使用它获取或设置PDF的文档属性。Xcode并不提供它作为一个点之后的自动完成,例如myPDF.documentAttributes

你是怎么用的?我正在尝试获取/设置文档元数据,如Author、Creator、Subject。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-26 02:51:33

再看一下你提供的链接。不是CGPDFDocument,而是Quartz.PDFDocument。以下是访问它的一种方法:

代码语言:javascript
复制
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

若要更改属性,请执行以下操作:

代码语言:javascript
复制
pdfDoc.documentAttributes?["Title"] = "Cheese"
pdfDoc.write(to: URL(fileURLWithPath: "/path/to/file.pdf")) // save the PDF file
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44186746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档