Pregunta

I am writing a C# application that will need to read in both XFA and AcroField templates. Due to the size of the company and the number of existing PDF documents that may be hooked up to the application, picking one and going with it is out of the question.

I am currently using iTextSharp to read in the AcroFields, but it is not actually saving the changes. I made the AcroFields using the trial version of Acrobat Pro.

EDIT: (I deleted a lot of the origional post)

I have a workaround somewhat working, but I would rather not do a Deapth First Search on the XML. I also do not have it figuring out anything other than text fields yet.

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;
}
¿Fue útil?

Solución

OK, I figured out how to get the field names for both XFA and AcroField PDF Documents, and that was my original question.

I also used a class called myKey. It has a value and a key. I overrode the .equals to just compare the key value, and wrote my own .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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top