Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当字段名有两个磅符号时,iText将引发XML错误_C#_Itextsharp - Fatal编程技术网

C# 当字段名有两个磅符号时,iText将引发XML错误

C# 当字段名有两个磅符号时,iText将引发XML错误,c#,itextsharp,C#,Itextsharp,当我试图为包含两个磅符号的字段名设置值时,iText遇到了一个奇怪的错误。问题是我无法控制原始表单字段名 有人知道怎么避开这件事吗?我尝试过重命名字段,但它没有对字段进行重命名 这会出错 form.SetField("form1[0].#subform[8].#area[12].Line4_FirstName3[4]", "Bill"); 但是,如果字段名类似,但只有一个磅符号,则不会 form.SetField("form1[0].#subform[8].Line4_FirstName3[4

当我试图为包含两个磅符号的字段名设置值时,iText遇到了一个奇怪的错误。问题是我无法控制原始表单字段名

有人知道怎么避开这件事吗?我尝试过重命名字段,但它没有对字段进行重命名

这会出错

form.SetField("form1[0].#subform[8].#area[12].Line4_FirstName3[4]", "Bill");
但是,如果字段名类似,但只有一个磅符号,则不会

form.SetField("form1[0].#subform[8].Line4_FirstName3[4]", "Bill");
引发了类型为“System.Web.HttpUnhandledException”的异常。-->System.Xml.XmlException:名称中不能包含“#”字符(十六进制值0x23)。 位于System.Xml.XmlDocument.CheckName(字符串名称) at System.Xml.xmlement..ctor(XmlName名称,布尔值为空,XmlDocument doc) 位于System.Xml.XmlDocument.CreateElement(字符串前缀、字符串localName、字符串namespaceURI) 位于System.Xml.XmlDocument.CreateElement(字符串名称) 位于iTextSharp.text.pdf.XfaForm.Xml2SomDatasets.InsertNode(XmlNode,String shortName) 位于iTextSharp.text.pdf.AcroFields.SetField(字符串名称、字符串值、字符串显示) 位于iTextSharp.text.pdf.AcroFields.SetField(字符串名称、字符串值)

这来自iText 4.1.2.0

当我尝试重命名字段以删除“#area[12]”周围的第二个磅符号类型测试时,它不会重命名

PdfReader reader2 = new PdfReader(pdfTemplate);
using (FileStream fs = new FileStream(TemporaryFile, FileMode.Create)) {
PdfStamper stamper = new PdfStamper(reader2, fs);
AcroFields pdfForm = stamper.AcroFields;
//Dim de As New DictionaryEntry
foreach (DictionaryEntry de in pdfForm.Fields) {
    pdfForm.RenameField(de.Key.ToString(), Guid.NewGuid().ToString("N"));
}

stamper.Close();

}我遇到了同样的问题,我努力解决,为字段所属的母版页设置了一个名称(在LiveCycle中)。 这样我就不再在字段名上有第二个“#”,所以我更改了名称,问题就不再存在了

在您的情况下,请尝试获取字段的全名

PdfReader reader2 = new PdfReader(pdfTemplate);
using (FileStream fs = new FileStream(TemporaryFile, FileMode.Create)) 
{
   PdfStamper stamper = new PdfStamper(reader2, fs);
   AcroFields pdfForm = stamper.AcroFields;

   //Dim de As New DictionaryEntry
   foreach (DictionaryEntry de in pdfForm.Fields) {
       //try this...
       var fullname= fields.GetTranslatedFieldName(de.Key.ToString());
       pdfForm.RenameField(fullname, Guid.NewGuid().ToString("N"));
   }

   stamper.Close();

我知道这个问题由来已久。但也许我的回答会对某人有所帮助。 在问题中,ReplaceField函数不起作用,因为它只能更改名称的最后一部分(都在字段名的最后一个doth之后)。 对于这种情况,以下代码将起作用:

Dictionary<string, string> changedFields = new Dictionary<string, string>();
foreach (string fieldName in formFields.Fields.Keys)
{
    if (fieldName.Contains("#"))
    {
        if (fieldName.Contains("."))
        {
            string[] names = fieldName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            if (names.Last().Contains("#"))
            {
                changedFields.Add(fieldName, String.Format("{0}.{1}", String.Join(".", names, 0, names.Length - 1), names.Last().Replace("#", "")));
            }
        }
        else
        {
            changedFields.Add(fieldName, fieldName.Replace("#", ""));
        }
    }
}
foreach (string oldName in changedFields.Keys)
{
    stamper.AcroFields.RenameField(oldName, changedFields[oldName]);
}
Dictionary changedFields=new Dictionary();
foreach(formFields.Fields.Keys中的字符串字段名)
{
if(fieldName.Contains(“#”)
{
if(fieldName.Contains(“.”)
{
string[]names=fieldName.Split(新字符串[]{“.”},StringSplitOptions.RemoveEmptyEntries);
if(names.Last()包含(“#”)
{
changedFields.Add(fieldName,String.Format(“{0}.{1}”),String.Join(“.”,names,0,names.Length-1),names.Last().Replace(“#“,”);
}
}
其他的
{
changedFields.Add(fieldName,fieldName.Replace(“#“,”);
}
}
}
foreach(changedFields.Keys中的字符串oldName)
{
stamper.AcroFields.RenameField(oldName,changedFields[oldName]);
}

您正在填写XFA表单,就像填写AcroForm一样。这个问题的答案可能需要5到10页。不幸的是,我现在没有时间写5到10页关于这个主题。谢谢布鲁诺。这是一个很好的提示/信息。在使用AcroForms这么长时间后,我已经忘记了这种表单样式。现在,我必须处理这两个问题。