I’m using MBS PDFKit to access PDF metadata. It should be read/write, but I haven’t been able to create a custom entry. Google AI (yes, I know) gives this code. Adding a standard field (e.g. Author) works fine, but a custom field (e.g. ProjectID" does nothing.
Var pdfDoc As New PDFDocumentMBS // Create a new PDF Document
// Set standard metadata
pdfDoc.Author = "Xojo Developer"
pdfDoc.Title = "My New PDF Document"
pdfDoc.Subject = "Demonstrating Metadata"
pdfDoc.Keywords = "Xojo, PDF, Metadata"
// Add a custom metadata attribute
pdfDoc.documentAttributes.Value("ProjectID") = "PROJ-2025-001"
// Save the PDF document
Var f As FolderItem = SpecialFolder.Desktop.Child("MyPDFWithCustomMetadata.pdf")
pdfDoc.Save(f)
If I’m reading the documentation correctly, here’s how you want to do it:
var attributeDict as dictionary
attributeDict=pdfDoc.documentAttributes
attributeDict.Value("ProjectID") = "PROJ-2025-001"
pdfDoc.documentAttributes=attributeDict
How about you add some pages to the PDF!?
@Eric Williams That’s a very good suggestion. I tried that, and it works (kinda). I think there is some issue where the new metadata isn’t being saved with the PDF. @Christian_Schmitz I tried creating a new page and doing an append, which had no effect. In any case, I don’t want to alter the PDF, just the metadata.
I thought it would be worth giving a slightly cut-down version of the code,.
dim pdfDoc as PDFDocumentMBS = new PDFDocumentMBS(self.thePDFFolderItem)
dim d as dictionary = pdfDoc.documentAttributes
dim pageNum as integer = d.lookup("firstContentPage", 0) //see if this value exists already
setPDFPageNumDialog.showModal //ask user to supply a number
if setPDFPageNumDialog.buttonPushed = 1 then
pageNum = val(setPDFPageNumDialog.TextArea1.text) //the number the user typed
if pageNum > 0 then
d.value("firstContentPage") = str(pageNum)
call PDFDoc.write(thePDFFolderItem) //PDF is saved
dim newD as dictionary = pdfDoc.documentAttributes // the new attribute is still present in pdfDoc. But if I open the PDF again it is not there.
end if
end if
I can’t get Eric’s code to work either. Where should the new data show up in the DPF?
You can change subject e.g. like this:
// read a file
Var f As New FolderItem("Installation.pdf")
Var p As New PDFDocumentMBS(f)
// change an attribute
Var d As Dictionary = p.documentAttributes
d.Value(PDFDocumentMBS.PDFDocumentSubjectAttribute) = "Hello World"
p.documentAttributes = d
// and write back to disk
Var f2 As New FolderItem("Installation2.pdf")
Call p.write(f2)
No, that doesn’t work either.
dim theDoc as new PDFDocumentMBS(thePDFFile)
dim attributeDict as dictionary
attributeDict = theDoc.documentAttributes
if attributeDict = Nil then attributeDict = new Dictionary
attributeDict.Value(PDFDocumentMBS.PDFDocumentSubjectAttribute) = "Hello World"
theDoc.documentAttributes = attributeDict
call theDoc.write(thePDFFile)
The result:
Does write return true or false?
Maybe it can’t overwrite the file?
You are correct: writing doesn’t work. The file was just created with CGPDFDocumentMBS:
How do I get more information why the write was not successful?
Maybe don’t use same folder item with same file name?
The old file may still be open!
@Christian_Schmitz Would you take a look at documentAttributes? In this simple test I see that the standard property (Author) is saved to the file but the custom property (myComment) is not saved.
var f as FolderItem = SpecialFolder.Desktop.child("test.pdf")
var pdfDoc as PDFDocumentMBS = new PDFDocumentMBS(f)
var d as dictionary = pdfDoc.documentAttributes
if d = nil then d = new Dictionary
d.value("myComment") = "Hello World"
d.value("Author") = "Joe Smith"
pdfDoc.documentAttributes = d
var result as boolean = PDFDoc.write(f) //save
pdfDoc = new PDFDocumentMBS(f)
var newDict as dictionary = pdfDoc.documentAttributes //Authors is correct, myComment is missing
break
I think the answer is that PDFKit does not allow you to create custom attributes, just read and write the standard ones (e.g. Author, Title, etc.).
Yes, you can only do the attributes listed on Apple’s website.
if you need more, you can use our DynaPDF plugin (Lite or higher)