且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当字段名称包含使用iTextSharp的句点时,如何重命名PDF表单域

更新时间:2023-02-19 11:05:25

这在iText (Sharp)源代码:

  / ** 
*重命名一个字段。只有名字的最后部分才能重命名。例如,如果原始字段为ab.cd.ef,只有ef部分可以重命名,则
*。
* @param oldName旧字段名称
* @param newName新字段名称
* @return< CODE> true< / CODE>如果重命名成功,则< CODE> false< / CODE>
* otherwise
* /
virtual public bool RenameField(String oldName,String newName)

因此,使用此方法,您可以将 First.Name 重命名为 First.NameOrEmpty 或类似的内容,而不是 EmptyOrFirst.Name strong>。

背景是,PDF中的表单域不仅仅被安排为具有全名的对象列表,而是作为一个树,其中的全名一个节点是其祖先的部分名称和节点本身以点分隔的连接。



因此,除了最后一个点之后的部分,部分但引入了一个新点)实际上意味着将树中的字段移动到不同的父节点,该节点可能或可能不必在该过程中创建。



另一方面, RenameField 方法仅支持更改最终字段树元素itsel的本地名称f。

这是明智的。表单字段可以从其父项继承许多属性。所以,如果你重命名一个字段,你是否希望结果字段从新的父类继承?或者应该将所有继承的属性显式添加到节点中?还是只有那些不同于各自的新的继承信息?或者你有另一个想法吗?


I use iTextSharp to rename PDF form field. The old field names and the new replacement names are entered in a text box (comma separated). My code then loops through each line, grabs the old field name, looks for the field in the PDF and renames it to the new field. Finally, a new PDF is created from the old PDF, and all of the fields in the new PDF will have the new names.

My problem is that I can't seem to rename the PDF fields which contain a period in the field name (e.g. First.Name). Is there a way to rename such PDF fields using iTextSharp? I looked everywhere and couldn't find a clear answers or code examples.

Here is the code I'm using so far:

//source PDF file
string src = PDFFile.Text;

//destination PDF file
string dest = @"Renamed_" + DateTime.Now.ToString("yyyymmddhhss") + ".pdf";

//open source PDF
PdfReader reader = new PdfReader(src);

using (FileStream fs = new FileStream(dest, FileMode.Create))
{
PdfStamper stamper = new PdfStamper(reader, fs);
AcroFields fields = stamper.AcroFields;

//read each line from the txtOldAndNewFieldNames text box
//each line contains <old field name>,<new field name>
//the two values are then put in an array
//finally, the PDF field with <old field name> is renamed to <new field name>

foreach (string line in txtOldAndNewFieldNames.Lines)
{

    string[] namePair = new string[2];
    namePair = line.Split(',');

    try
    {
        fields.RenameField(namePair[0], namePair[1]);
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
stamper.Close();
reader.Close();
}

This is described in the iText(Sharp) source code:

/**
 * Renames a field. Only the last part of the name can be renamed. For example,
 * if the original field is "ab.cd.ef" only the "ef" part can be renamed.
 * @param oldName the old field name
 * @param newName the new field name
 * @return <CODE>true</CODE> if the renaming was successful, <CODE>false</CODE>
 * otherwise
 */    
virtual public bool RenameField(String oldName, String newName)

Thus, using this method you can rename First.Name to First.NameOrEmpty or something like that but not to EmptyOrFirst.Name.

The background is that form fields in PDFs are not merely arranged as a list of objects with the full name but instead as a tree in which the full name of a node is the concatenation of the partial names of its ancestors and the node itself separated by dots.

Thus, changing anything but the part after the last dot (or changing that part but introducing a new dot) actually means moving the field in the tree to a different parent node which might or might not have to be created in the process.

The RenameField method, on the other hand, only supports changing the local name of the final field tree element itself.

And this is sensible. Form fields can inherit a number of properties from their parent. So if you rename a field, do you want the result field to inherit from the new parent instead? Or shall all inherited properties be explicitly added to the node? Or only those differing from the respective new inherited information? Or do you have yet another idea?