我正在尝试重命名 itext 中的字段。我可以成功重命名文本字段,但重命名复选框字段时出现错误。这是到目前为止的代码:
try {
pdfReader = new PdfReader(s_pdfread);
} catch (Exception ex) {
Console.WriteLine("Can't create Filestream from " + s_pdfread);
Console.WriteLine(ex.Message);
ShowHelp();
Environment.Exit(-1);
}
List <renameJob> jobsToDo = new List<renameJob> ();
try {
using (FileStream fs = new FileStream(s_pdfwrite, FileMode.Create)) {
PdfStamper stamper = new PdfStamper(pdfReader, fs);
AcroFields fields = stamper.AcroFields;
foreach (var field in stamper.AcroFields.Fields) {
Console.WriteLine("fieldKeySRC: " + field.Key);
string newFieldname = field.Key.Replace(s_searchFor, s_replaceWith);
newFieldname = newFieldname.Replace(".", "");
// special name for checkbox
/*switch (stamper.AcroFields.GetFieldType(field.Key)) {
case AcroFields.FIELD_TYPE_CHECKBOX:
newFieldname = newFieldname + ".0";
break;
} planned for future. In finaly version i need to add this .0 */
if (field.Key != newFieldname) {
renameJob job = new renameJob();
job.oldName = field.Key;
job.newName = newFieldname;
jobsToDo.Add(job);
}
}
foreach (renameJob job in jobsToDo) {
if (fields.RenameField(job.oldName, job.newName) == true) {
Console.WriteLine("renameField: " + job.oldName + " to " + job.newName);
}
else {
Console.WriteLine("!!! failed: renameField: " + job.oldName + " to " + job.newName);
}
}
stamper.Close();
为什么这对复选框会失败而对文本框工作正常......?