Free Support Forum - aspose.com

Can't convert the if field to the plain text of it value if the content of the if field contains table

Hi

I find that if a IF Field contents contain tables, then after I remove the IF Field, the tables are kept there.

For example:

{ IF 1 > 2 "Table1" "Table2" }

(I also uploaded the document)

What I need is to convert the “IF Field” to the table of Table2, but actually, I get three tables, one is Table1 and the other two are Table2.

Is this the bug?

My codes are:

class Program

{

static void Main(string[] args)

{

License license = new Aspose.Words.License();

license.SetLicense("Aspose.Words.lic");

Document doc = new Aspose.Words.Document(@"testdoc2.doc");

doc.UpdateFields();

RemoveIFFields ifRem = new RemoveIFFields();

doc.Accept(ifRem);

doc.Save("results.doc");

}

}

public class RemoveIFFields : DocumentVisitor

{

private bool _delete = false;

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

{

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

{

_delete = true;

fieldStart.Remove();

}

return VisitorAction.Continue;

}

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

{

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

{

_delete = false;

fieldSeparator.Remove();

}

return VisitorAction.Continue;

}

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

{

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

{

_delete = false;

fieldEnd.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitRun(Run run)

{

if (_delete)

run.Remove();

return VisitorAction.Continue;

}

}

Hi

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

Thanks for your request. In this case, please try using the following code:

public class RemoveIFFields : DocumentVisitor

{

private bool _delete = false;

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

{

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

{

_delete = true;

fieldStart.Remove();

}

return VisitorAction.Continue;

}

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

{

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

{

_delete = false;

fieldSeparator.Remove();

}

return VisitorAction.Continue;

}

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

{

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

{

_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;

}

}

Also please see the following link to learn about DocumentVisitor:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.documentvisitor.html

In additional, you can inspect structure of Word document using DocumentExplorer (Aspose.Words demo application). Please follow the link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/document-tree-navigation.html

Best regards,

Hi

Thanks for your help, and the code works for if conditional field that contain tables!.

But after making more investigation, I find there are still some issues:

1.)

If the conditional field contains equation field, there is a pair of brackets left after the field is converted.

For example, if I have:

Field1 =1, Field2=2, Field3 = “Field5”, Field4 = 4, Field5 = 5 …

And the field is:

{ IF { ={ MERGEFIELD Field2 } + { MERGEFIELD Field4 } } = { MERGEFIELD Field6 } “Field2 + Field4 is equal Field6" "Field2 + Field4 is NOT equal Field6" }

Then using the codes above, I will get:

{}Field2 + Field4 is equal Field6

There is a reluctant {} at the beginning of the result.

2.)

If a Field is very complex which contains many IF Fields and Formula Fields, it seems that the code will not work.

A field, no matter how complex the content of it is (that is to say, it can contain other fields that contain field inside also), the nodes after the field separator of it, always is the value of that field, isn't it?

So if this is true, does this mean we can just keep the nodes between the field separator and field end, regardless of it content?

If this is not true, how can we evaluate these very complex fields? Such as below:

{ IF { IF { MERGEFIELD Field1 } < { MERGEFIELD Field2 } “3” “0” } > { = { MERGEFIELD Field5 } – { MERGEFIELD Field4 } } “{ = { IF { = = { MERGEFIELD { MERGEFIELD Field3 } } + 90 } < 91 “F” “33” } + 11 }” “Test Field” }

3.)

As mentioned in your link, there is a DocumentExplorer demo, but I can't find it in that link.

Thanks!

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

Thank you for additional information. MS Word document field looks like the following:

[FieldStart] here is field code [FieldSeparator] here is field value [FieldEnd]

So you should just remove all except field value.

DocumentExplorer is included to Aspose.Words installation package. You can find it here:

C:\Program Files\Aspose\Aspose.Words\Demos\CSharp\DocumentExplorer

Do you really need to remove IF fields? Maybe in your case it is enough just call UpdateFields() to evaluate IF field.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/field-update.html

Best regards,

Hi

Yes, Converting all the Fields such as IF, Formula and so on to pain text is very important to us, because we don’t wan’t end user to change these field and to be interrupted by how these fields are calculated.

I will try to remove these field by removing the FieldSeparator according to your suggestion.

Great thanks to you!
Best regards

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

Thanks for your request. If so, maybe it would be enough just to protect your document. In this case, the end used will not be able to change anything in the document:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/protecting-documents.html

Best regards,