Free Support Forum - aspose.com

Aspose Words - Get all fields

Received : 2007/10/02 07:21:43
Message : Hi

I note that you have a Field class. Given an instance of a document, how does one access all of that document's Field's? I want to do something like

foreach (Field field in Document.fields)
{
Console.Writeline(field.GetFieldCode());
Console.Writeline(field.FieldSeparator);
}

Regards

Gavin


This message was posted using Aspose.Live 2 Forum

Hi

Thanks for your request. You can try to use the following code.

Document doc = new Document(@"236_96982_gavinsinai\in.doc");

NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);

string fieldCode = string.Empty;

string fieldValue = string.Empty;

FieldStart fieldStart = null;

FieldSeparator fieldSeparator = null;

FieldEnd fieldEnd = null;

foreach (FieldStart start in starts)

{

// get field start

fieldStart = start;

Node node = start.NextSibling;

// get field code

while (node.NodeType == NodeType.Run)

{

fieldCode += (node as Run).Text;

node = node.NextSibling;

}

if (node.NodeType == NodeType.FieldSeparator)

{

// get field separator

fieldSeparator = (FieldSeparator)node;

node = node.NextSibling;

// get field value

while (node.NodeType == NodeType.Run)

{

fieldValue += (node as Run).Text;

node = node.NextSibling;

}

}

// get field end

fieldEnd = (FieldEnd)node;

}

I hope that it will help you.

Best regards.