Skip to main content
Skip table of contents

Using walkTree() in C#

📌 Overview

This article demonstrates the use of the walktree()method in C#. This offers the ability to walk through the DOM calling a given function on each node, and can be called on an IDOMFixedPage node to visit every object on a page.

ℹ️ Key Concepts

The function is allowed to:

  • edit or inspect the node in any way

  • replace the node (via parent.replaceNode() orparent->replaceNode() in C++)

  • remove the node (via parent.extractChild()or parent->extractChild())

  • edit or modify the node's children in any way. However, the result is undefined if the node's siblings are removed or reordered. Keep in mind that if a node within a brush or form is altered, all uses of that brush or form will see the change.

⌨️ Code Example

Two methods for creating the callback are shown below, one with a delegate that provides an additional layer of abstraction. Both point to the same function DumpNodeInfo(); use which ever method suits your needs. Full implementation available at WalkTreeExample.cs.

Method 1

Method 2

C#
private class NodeTreeCallback : WalkTreeCallback
{
    public override bool visitNode(IDOMNode node)
    {
        DumpNodeInfo(node);
        return true;
    }
}
C#
using var inFixedPage = assembly.getDocument().getPage().getContent();

WalkTreeCallback callback = new NodeTreeCallback();

inFixedPage.walkTree(callback.getCallbackFunc(), callback.getPriv(), false, true);
C#
private class NodeTreeCallbackDelegate(NodeTreeCallbackDelegate.visitNodeDelegate visitNodeHandler) : WalkTreeCallback
{
    public delegate void visitNodeDelegate(IDOMNode node);

    public override bool visitNode(IDOMNode node)
    {
        if (visitNodeHandler == null)
            return false;
        visitNodeHandler(node);
        return true;
    }
}
C#
using var inFixedPage = assembly.getDocument().getPage().getContent();

void VisitNodeHandler(IDOMNode node) => DumpNodeInfo(node);
WalkTreeCallback callback = new NodeTreeCallbackDelegate(VisitNodeHandler);

inFixedPage.walkTree(callback.getCallbackFunc(), callback.getPriv(), false, true);

☑️ Conclusion

The walkTree() method in C# provides a powerful way to traverse the DOM, allowing for inspection, modification, and management of nodes within a PDF document. By utilizing the provided code examples and understanding the key concepts, users can effectively manipulate document structures to suit their needs.

📚 Additional Resources

If you need additional help, see our API documentation for detailed information on class/method usage, or raise a support ticket via our customer portal.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.