2

XFAテンプレートとAcroFieldテンプレートの両方で読み取る必要があるC#アプリケーションを作成しています。会社の規模とアプリケーションに接続できる既存のPDFドキュメントの数のために、1つを選んでそれを使用することは問題外です。

現在、iTextSharpを使用してAcroFieldsを読み込んでいますが、実際には変更を保存していません。私はAcrobatProの試用版を使用してAcroFieldsを作成しました。

編集:(元の投稿をたくさん削除しました)

ある程度機能する回避策がありますが、XMLでDeapthFirstSearchを実行したくありません。また、テキストフィールド以外はまだ理解していません。

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

1 に答える 1

1

OK、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;
}
于 2012-07-12T14:19:08.910 に答える