pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

[Question] - Renaming fields like Adobe Acrobat

Open timothyjohnwilson opened this issue 6 years ago • 6 comments

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

timothyjohnwilson avatar Sep 15 '20 05:09 timothyjohnwilson

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!

Hopding avatar Sep 15 '20 11:09 Hopding

That would be stellar, thanks!

timothyjohnwilson avatar Sep 15 '20 12:09 timothyjohnwilson

IsacroField.setPartialName what I'm looking for?

timothyjohnwilson avatar Sep 15 '20 13:09 timothyjohnwilson

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

Hopding avatar Sep 15 '20 21:09 Hopding

I'm going to close this for now. Please feel free to reopen if you have additional questions.

Hopding avatar Sep 23 '20 22:09 Hopding

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:

  1. working: nameWithoutPrefix -> after setPartialName-> newPrefix.nameWithoutPrefix
  2. not working: group1.checkbox1 -> after setPartialName -> 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.

StefanZivkovic avatar Feb 03 '22 07:02 StefanZivkovic