We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

INCLUDETEXT Field

Hi,

I have been reading and for what I gather the INCLUDETEXT field is not supported. I am trying to convert from Word Automation to Aspose.

What I want to know is how can I navigate to the INCLUDETEXT Field in order to remove it and insert my text. I already know how to generate the text to replace this, I just need to know how can I remove the field and be in the same position in my document

Thanks

Leonardo

Hi Leonardo,

Thanks for considering Aspose.Words.

Yes that field is unsupported at the moment. We will look into supporting it in the future. For the moment the method you have outlined will work well in it's place. Just incase you missed it, you can insert a document into another easily using the code from this article here:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-insert-a-document-into-another-document.html

Regarding how to find and remove a specific field in a document, please refer to the basic implementaiton below.

NodeCollection nodes2 = doc.GetChildNodes(NodeType.FieldStart, true);<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

foreach (FieldStart node in nodes2)

{

if (GetFieldCode(node).Contains("INCLUDETEXT"))

{

//Insert content before node

RemoveField(node);

}

}

private static string GetFieldCode(Aspose.Words.Fields.FieldStart fieldStart)

{

StringBuilder builder = new StringBuilder();

for (Node node = fieldStart; node != null && node.NodeType != NodeType.FieldSeparator && node.NodeType != NodeType.FieldEnd; node = node.NextPreOrder(node.Document))

{

// Use text only of Run nodes to avoid duplication.

if (node.NodeType == NodeType.Run)

builder.Append(node.GetText());

}

return builder.ToString();

}

private static void RemoveField(Aspose.Words.Fields.FieldStart fieldStart)

{

Node node = fieldStart;

bool isRemoving = true;

while( node != null && isRemoving)

{

if (node.NodeType == NodeType.FieldEnd)

isRemoving = false;

Node nextNode = node.NextPreOrder(node.Document);

node.Remove();

node = nextNode;

}

}

Please feel free to ask if you have any further queries.

Thanks,

Thanks Adam, but the code provided doesnt remove the INCLUDETEXT field.

Any other suggestions?

Thanks

Leonardo

Hi Leonardo,

Thanks for the additional information. It appears to be working properly on my side with a test document. Could you please attach your document here for testing and we will take a closer look.

Thanks,

Adam,

Please find attached a test document

Thanks

Leonardo

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. I think in this case you can try using DocumentVisitor. Please see the following code:

Document doc = new Document("TestDocument_1.doc");

RemoveIncludeTextFields rem = new RemoveIncludeTextFields();

doc.Accept(rem);

doc.Save("out.doc");

class RemoveIFFields : DocumentVisitor

{

private bool _delete = false;

public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)

{

if (fieldStart.FieldType == Aspose.Words.Fields.FieldType.FieldIncludeText)

{

_delete = true;

fieldStart.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)

{

if (fieldSeparator.FieldType == Aspose.Words.Fields.FieldType.FieldIncludeText)

{

_delete = false;

fieldSeparator.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)

{

if (fieldEnd.FieldType == Aspose.Words.Fields.FieldType.FieldIncludeText)

{

_delete = false;

fieldEnd.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitRun(Run run)

{

if (_delete)

run.Remove();

return VisitorAction.Continue;

}

public override VisitorAction VisitTableStart(Table table)

{

if (_delete)

table.Remove();

return VisitorAction.Continue;

}

public override VisitorAction VisitParagraphStart(Paragraph paragraph)

{

if (_delete)

paragraph.Remove();

return VisitorAction.Continue;

}

}

If you would like to remove field value, just replace _delete = true; with _delete = false; in VisitFieldSeparator method.

Best regards,