[Question] - Renaming fields like Adobe Acrobat
The question is as simple as it gets. is it possible to rename PDF fields like you can in Adobe Acrobat? this would make my life so much easier 😅 If not, im down to try anything
Hello @timothyjohnwilson! pdf-lib does not currently provide any APIs for renaming fields. But I don’t see any reason why such an API couldn’t be implemented. I think it would be relatively straightforward. If you’re interested in working on this, I’d be happy to point you at the relevant sections of the codebase and PDF spec, as well as answer any questions you might have!
That would be stellar, thanks!
IsacroField.setPartialName what I'm looking for?
The main thing you'll want to understand is the difference between a fully qualified field name and a partial field name. Section 12.7.3.2 of the PDF spec explains the distinction. It's also described a bit in the API doc for PDFField.getName.
Setting the partial name for a field is quite easy. you can use PDFAcroField.setPartialName for that. Changing the fully qualified name is not quite so easy as it would require creating new non-terminal fields in the tree as well as relocating the field to one of those new non-terminals. pdf-lib has a lot of the code to do this already in place in order to support creation of new form fields. For example, here's some code from PDFForm.createTextField that creates non terminals nodes and set the partial name: https://github.com/Hopding/pdf-lib/blob/95adc0f95e0d25ee3070f86d958148a5917e58ad/src/api/form/PDFForm.ts#L492-L495
I'm going to close this for now. Please feel free to reopen if you have additional questions.
Hi, on one of projects i have been extensively using this library. At one point i was in need to update pdf form fields by renaming some of them, using library APIs. Since i couldn't find something like rename the only choice i had was setPartialName.
I notice that setPartialName fits my needs when there is no dot . in form field name, and it is not working when i have dot. Not sure if there are similar limitations for other non-alphanumeric characters.
My need is to apply newPrefix. (read any kind of prefix with dot - in 99,9% of cases characters with underscores and one dot at the end) to existing name of form field.
Example when is (not)working:
- working:
nameWithoutPrefix-> aftersetPartialName->newPrefix.nameWithoutPrefix - not working:
group1.checkbox1-> aftersetPartialName->group1.newPrefix.group1.checkbox1
Not sure how i can understand content from previous comments - is setPartialName expected to work like this, or is this kind of bug or limited behavior?
@Hopding please let me know your understanding of this.