iTextSharp RenameField bug?

半世苍凉 提交于 2019-12-01 19:52:28
chrisberl

Maybe the author didn't explain it completely, but I think there really is a bug. Let's assume we have a PDF document with an AcroForm which contains two fields: name and owner.name. Now we want to rename owner.name to owner.name1 and name to name1. The first one will succeed, but the name in the field registry (AcroFields.fields) is now name1, not owner.name1 as expected (before it was owner.name). The seconds rename then fails because name1 already exists in the field registry. It affects only the registry, the field name in the resulting PDF document is correct.

Here the critical code snippet from Java iText:

// snippet from com.itextpdf.text.pdf.AcroFields.renameField
int idx2 = newName.lastIndexOf('.') + 1;
// cut the last part from the original name
newName = newName.substring(idx2);
PdfString ss = new PdfString(newName, PdfObject.TEXT_UNICODE);
// problem: only the last part will be registered, this must 
// be IMO the (original) whole name including the dots
fields.put(newName, item);

There is no such thing as a field with a subclass. The dots in the field names refer to a hierarchy. For instance: if you have a field named person. This field can have children such as name and address. The fully qualified names of these child fields would then be person.name and person.address. Address can in turn have child fields, such as street, city and country, resulting in fully qualified names such as person.address.street, person.address.city and person.address.country.

You can rename fields, such as street, but you can't change the hierarchy, because the fully qualified name isn't present anywhere inside the PDF. So if you have a field with a fully qualified name person.address.street, you can only rename the street part. For instance: you can rename person.address.street into person.address.line1. You can not rename person.address.street into person.street as that would change the structure of the form.

What the docs say is that you can rename field names, but you can't change the field structure.

You say that something feels as a bug, but I don't see what is wrong. The docs indicate that ef is the only part of the fully qualified name that can be changed. Nowhere does it say that you can change ab.cd.ef into xy as that would mean that you have to rewrite a structure tree consisting of different PDF dictionaries instead of renaming the value of a key in a single dictionary.

This is, by the way, also explained in my book.

Addendum:

I've created a simple example named RenameField. It takes a form (subscribe.pdf) and creates a new form (subscribe_renamed.pdf). The difference? We have renamed "personal.loginname" into "personal.login". It would have been impossible to rename "personal.loginname" into "login" as that would require changing the hierarchy (just test it, you'll see it won't work).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!