2

According to Apple's documentation CGPDFDocument has a var called documentAttributes:

var documentAttributes: [AnyHashable : Any]? { get set }

I'm having trouble seeing how to use this to either get, or set document attributes of a PDF in Swift. Xcode doesn't offer it as an auto-completion following a dot, e.g. myPDF.documentAttributes.

How do you use it? I'm trying to get/set the document metadata such as Author, Creator, Subject.

benwiggy
  • 1,440
  • 17
  • 35
  • It's a dictionary whose keys can be found [here](https://developer.apple.com/reference/quartz/pdfdocument/document_attribute_keys). You would do something like `let title = pdfDocument.documentAttributes[PDFDocumentTitleAttribute]` – Code Different May 25 '17 at 18:21
  • I get "value of type 'CGPDFDocument' has no member 'documentAttributes". – benwiggy May 25 '17 at 19:14
  • The documentation in your link says it's available on OS X 10.12+. What version of OS X are you targeting? – Code Different May 25 '17 at 19:38
  • Yes, I'm on Sierra 10.12.5. How do the get and set bits fit in? – benwiggy May 25 '17 at 19:51
  • New link for PDFDocumentAttribute is https://developer.apple.com/documentation/pdfkit/pdfdocumentattribute – Haripal Wagh Oct 04 '18 at 10:35

1 Answers1

2

Had a second look at the link you provided. It's not CGPDFDocument but Quartz.PDFDocument. Heres one way to access it:

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"])")
}

The list of keys differ from file to file so you need to check each one and deal with nil when the key is not available.


To change the attributes:

pdfDoc.documentAttributes?["Title"] = "Cheese"
pdfDoc.write(to: URL(fileURLWithPath: "/path/to/file.pdf")) // save the PDF file
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Thank you. Why on earth are there two completely separate PDFDocument APIs? How ridiculous. – benwiggy May 26 '17 at 06:47
  • I can't see how to set a value, though. `attributes["Title"] = "Cheese"` doesn't work, nor does `updateValue`. – benwiggy May 26 '17 at 11:22
  • `attributes` contains a *copy* of `documentAttributes` so your statement had no effects on the PDF document. See my edited answer – Code Different May 26 '17 at 11:30
  • I know this is old, but I see there is a PDFKit as well as PDFDocument still in Quartz as mentioned above. Which one should be used to today? I'm using XCode 11.6 and Swift 5.3. – SouthernYankee65 Jul 17 '20 at 23:51
  • I've struggled with creating a PDF recently and my conclusion is that the Core Graphics version existed first and PDFKit was added after to simply it. The fact that PDFKit has properties pointing to Core Graphics leads me to believe that it is a wrapper and Core Graphics still does the work. Use PDFKit. – Michael Salmon May 11 '21 at 17:23