首页 文章

我如何以编程方式读取AcroFields和XFA字段以进行填充?

提问于
浏览
2

我正在编写一个需要在XFA和AcroField模板中读取的C#应用程序 . 由于公司的规模和可能连接到应用程序的现有PDF文档的数量,选择一个并继续使用它是不可能的 .

我目前正在使用iTextSharp读取AcroFields,但它实际上并没有保存更改 . 我使用Acrobat Pro的试用版制作了AcroFields .

编辑:(我删除了很多原创帖子)

我有一个解决方法有点工作,但我宁愿不在XML上进行Deapth First Search . 我还没有找到除文本字段之外的任何内容 .

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;
}

1 回答

  • 1

    好的,我想出了如何获取XFA和AcroField PDF文档的字段名称,这是我原来的问题 .

    我还使用了一个名为 myKey 的类 . 它有 Value 和关键 . 我重写 .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;
    }
    

相关问题