Aspose::Words::DocumentVisitor::VisitFieldStart method
Contents
[
Hide
]DocumentVisitor::VisitFieldStart method
Called when a field starts in the document.
virtual Aspose::Words::VisitorAction Aspose::Words::DocumentVisitor::VisitFieldStart(System::SharedPtr<Aspose::Words::Fields::FieldStart> fieldStart)
Parameter | Type | Description |
---|---|---|
fieldStart | System::SharedPtr<Aspose::Words::Fields::FieldStart> | The object that is being visited. |
ReturnValue
A VisitorAction value that specifies how to continue the enumeration.
Remarks
A field in a Word document consists of a field code and field value.
For example, a field that displays a page number can be represented as follows:
[FieldStart]PAGE[FieldSeparator]98[FieldEnd]
The field separator separates field code from field value in the document. Note that some fields have only field code and do not have field separator and field value.
Fields can be nested.
Examples
Shows how to print the node structure of every field in a document.
void FieldToText()
{
auto doc = MakeObject<Document>(MyDir + u"DocumentVisitor-compatible features.docx");
auto visitor = MakeObject<ExDocumentVisitor::FieldStructurePrinter>();
// When we get a composite node to accept a document visitor, the visitor visits the accepting node,
// and then traverses all the node's children in a depth-first manner.
// The visitor can read and modify each visited node.
doc->Accept(visitor);
std::cout << visitor->GetText() << std::endl;
}
class FieldStructurePrinter : public DocumentVisitor
{
public:
FieldStructurePrinter() : mVisitorIsInsideField(false), mDocTraversalDepth(0)
{
mBuilder = MakeObject<System::Text::StringBuilder>();
mVisitorIsInsideField = false;
}
String GetText()
{
return mBuilder->ToString();
}
VisitorAction VisitRun(SharedPtr<Run> run) override
{
if (mVisitorIsInsideField)
{
IndentAndAppendLine(String(u"[Run] \"") + run->GetText() + u"\"");
}
return VisitorAction::Continue;
}
VisitorAction VisitFieldStart(SharedPtr<FieldStart> fieldStart) override
{
IndentAndAppendLine(String(u"[Field start] FieldType: ") + System::ObjectExt::ToString(fieldStart->get_FieldType()));
mDocTraversalDepth++;
mVisitorIsInsideField = true;
return VisitorAction::Continue;
}
VisitorAction VisitFieldEnd(SharedPtr<FieldEnd> fieldEnd) override
{
mDocTraversalDepth--;
IndentAndAppendLine(u"[Field end]");
mVisitorIsInsideField = false;
return VisitorAction::Continue;
}
VisitorAction VisitFieldSeparator(SharedPtr<FieldSeparator> fieldSeparator) override
{
IndentAndAppendLine(u"[FieldSeparator]");
return VisitorAction::Continue;
}
private:
bool mVisitorIsInsideField;
int mDocTraversalDepth;
SharedPtr<System::Text::StringBuilder> mBuilder;
void IndentAndAppendLine(String text)
{
for (int i = 0; i < mDocTraversalDepth; i++)
{
mBuilder->Append(u"| ");
}
mBuilder->AppendLine(text);
}
};
See Also
- Enum VisitorAction
- Class FieldStart
- Class DocumentVisitor
- Namespace Aspose::Words
- Library Aspose.Words for C++