当字段名称包含使用iTextSharp的句点时,如何重命名PDF表单域 [英] How to rename a PDF form field when the field name contains a period using iTextSharp

查看:182
本文介绍了当字段名称包含使用iTextSharp的句点时,如何重命名PDF表单域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp重新命名PDF表单字段。旧的字段名称和新的替换名称将输入到文本框中(逗号分隔)。然后,我的代码循环遍历每一行,抓取旧字段名称,在PDF中查找字段并将其重命名为新字段。最后,从旧PDF创建一个新的PDF,并且新PDF中的所有字段将具有新名称。



我的问题是我似乎无法重新命名包含字段名称中的句点的PDF字段(例如,First.Name)。有没有办法使用iTextSharp重命名这些PDF字段?我到处找,并找不到明确的答案或代码示例。



以下是目前使用的代码:

  //源PDF文件
字符串src = PDFFile.Text;

//目标PDF文件
字符串dest = @Renamed_+ DateTime.Now.ToString(yyyymmddhhss)+.pdf;

//开源PDF
PdfReader reader = new PdfReader(src);

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

//从txtOldAndNewFieldNames文本框中读取每行
//每行包含<旧字段名称>,<新字段名称>
//然后将这两个值放入数组
//最后,带有<旧字段名称>的PDF字段被重新命名为<新的字段名称>

foreach(txtOldAndNewFieldNames.Lines中的字符串行)
{

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

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


解决方案

这在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?

这篇关于当字段名称包含使用iTextSharp的句点时,如何重命名PDF表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆