我正在编写一个需要读取 XFA 和 AcroField 模板的 C# 应用程序。由于公司的规模和可能与应用程序相关的现有 PDF 文档的数量,选择一个并使用它是不可能的。
我目前正在使用 iTextSharp 读取 AcroFields,但实际上并没有保存更改。我使用 Acrobat Pro 的试用版制作了 AcroFields。
编辑:(我删除了很多原始帖子)
我有一个可行的解决方法,但我不想对 XML 进行深度优先搜索。我也没有弄清楚文本字段以外的任何内容。
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;
}
最佳答案
好的,我想出了如何获取 XFA 和 AcroField PDF 文档的字段名称,这是我最初的问题。
我还使用了一个名为 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;
}
关于c# - 我如何以编程方式读取 AcroFields 和 XFA 字段以进行填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11432771/