我正在编写一个需要读入XFA和AcroField模板的C#应用程序。由于公司的规模和可能连接到应用程序的现有PDF文档的数量,选择一个并使用它是不可能的。
我目前正在使用iTextSharp读取AcroFields,但它实际上并没有保存更改。我用试用版的Acrobat Pro制作了AcroFields。
编辑:(我删除了很多原创帖子)
我有一个变通的办法,但我不想在XML上进行Deapth First搜索。除了文本字段之外,我还没有让它弄清楚其他任何东西。
public List<String> getKeys(AcroFields af)
{
XfaForm xfa = af.Xfa;
List<String> Keys = new List<string>();
foreach (var field in af.Fields)
{
Keys.Add(field.Key);
}
if (xfa.XfaPresent)
{
System.Xml.XmlNode n = xfa.DatasetsNode.FirstChild;
if (n == null) return Keys;
// drill down in to the children
while (n.FirstChild != null) { n = n.FirstChild; }
// if the node is filled in data, grab the parent
if ((n.Name.ToCharArray(0, 1))[0] == '#') n = n.ParentNode;
while ((n = n.NextSibling) != null)
{
Keys.Add(n.Name);
}
}
return Keys;
}发布于 2012-07-12 22:19:09
好了,我想出了如何获得XFA和AcroField文档的字段名,这是我最初的问题。
我还使用了一个名为myKey的类。它有一个值和一个键。我重写了.equals以比较键值,并编写了自己的.ToString。
public AcroFields loadAcroFields(String path)
{
PdfReader pdfReader = new PdfReader(path);
AcroFields fields = pdfReader.AcroFields;
pdfReader.Close();
return fields;
}
public List<myKey> getKeys(AcroFields af)
{
XfaForm xfa = af.Xfa;
List<myKey> Keys = new List<myKey>();
foreach (var field in af.Fields)
{
Keys.Add( new myKey(field.Key, af.GetField(field.Key)));
}
if (xfa.XfaPresent)
{
System.Xml.XmlNode n = xfa.DatasetsNode.FirstChild;
Keys.AddRange(BFS(n));
}
return Keys;
}
public List<myKey> BFS(System.Xml.XmlNode n)
{
List<myKey> Keys = new List<myKey>();
System.Xml.XmlNode n2 = n;
if (n == null) return Keys;
if (n.FirstChild == null)
{
n2 = n;
if ((n2.Name.ToCharArray(0, 1))[0] == '#') n2 = n2.ParentNode;
while ((n2 = n2.NextSibling) != null)
{
Keys.Add(new myKey(n2.Name, n2.Value));
}
}
if (n.FirstChild != null)
{
n2 = n.FirstChild;
Keys.AddRange(BFS(n2));
}
n2 = n;
while ((n2 = n2.NextSibling) != null)
{
Keys.AddRange(BFS(n2));
}
return Keys;
}https://stackoverflow.com/questions/11432771
复制相似问题