Redundant Section Break is Added When remove IF Field

When I’m converting the IF Field to the plain text of its value, I always get a redundant section break.

The code is below, the file is uploaded.

class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, object> dataSource = new Dictionary<string, object>();
        dataSource["Field1"] = 1;
        dataSource["Field2"] = 2.5;

        License license = new Aspose.Words.License();
        license.SetLicense("Aspose.Total.lic");
        Document doc = new Aspose.Words.Document(@"Header1.doc");

        doc.Accept(new RemoveFields(FieldType.FieldIf));
        doc.Save("results.doc");
    }

    public class RemoveFields : DocumentVisitor
    {
        private int _level = 0;
        private bool Delete
        {
            get
            {
                return _level > 0;
            }
        }

        private FieldType removedType;

        public RemoveFields(FieldType type)
        {
            removedType = type;
        }

        public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)
        {
            if (fieldStart.FieldType == removedType)
            {
                _level++;
            }
            if (Delete)
            {
                fieldStart.Remove();
            }
            return VisitorAction.Continue;
        }

        public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)
        {
            if (Delete)
            {
                fieldSeparator.Remove();
            }
            if (fieldSeparator.FieldType == removedType)
            {
                if (_level == 1)
                {
                    _level–;
                }
                else if (_level <= 0)
                {
                    throw new Exception();
                }
            }
            return VisitorAction.Continue;
        }

        public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)
        {
            if (fieldEnd.FieldType == removedType)
            {
                if (_level > 0)
                {
                    _level–;
                }
                fieldEnd.Remove();
            }
            else
            {
                if (Delete)
                {
                    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;
        }
    }
}

The Header1.doc is:
Header1(SectionBreakHere)
{ IF 1 = 2 “Some Text Never Be Shown” “” }(SectionBreakHere)
{ IF True = True “Header 7 test test(SectionBreakHere)” “” }

Then, when the file is processed, it is:
Header1(SectionBreakHere)
(SectionBreakHere)

(SectionBreakHere)
Header 7
test
test(SectionBreakHere)
there is a redundant (SectionBreakHere) before "Header 7 "
Is there anything wrong with my code?

Thanks!

Hello

Thanks for your request. Please try using the following code:

Document doc = new Aspose.Words.Document("Header1.doc");
doc.Accept(new RemoveIFFields());
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.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;
    }
    public override VisitorAction VisitSectionStart(Section section)
    {
        Section nextSection = (Section) section.NextSibling;
        if (_delete && nextSection != null)
        {
            nextSection.PrependContent(section);
            section.Remove();
        }
        return VisitorAction.Continue;
    }
}

Best regards,

Hello

Thank you for the help! But the code doesn’t work.

  1. In the function “VisitFieldStart”, it try to compare the “FieldType” with the type of “FieldDocProperty”, but what I’m trying to implement is to convert all the IF conditional field into the plain text of its value. Is this a typo and should be “FieldIf”?
  2. "VisitFieldSeparator"and “VisitFieldEnd” are similar to point 1.

Regardless Points 1 and 2, I changed them to compare with “FieldIf”, but the result is still wrong. Is this something to do with the section breaks? I add “Section Break (Continuous)” not the “(Section Break (Next Page))”.

The result is very strange and I can’t understand why the result is like this.

I changed the “Header1.doc” to:

Header1(Section Break (Continuous))

{IF 1 = 2 “Some Text Never Be Shown” “” }(Section Break (Continuous))

{IF True = True "

Header 7

test

test(Section Break (Continuous))

New If Start{IF True = True “Inner IF Value” “” }

New If End" “”}

But the result of the code above (change to use “FieldIF”) is:

Header1(Section Break (Continuous))

(Section Break (Continuous))

(Section Break (Continuous))

New If Start Inner IF Value

New If End" “”

Header 7

test

test

New If Start Inner IF Value

New If End

Thanks!

Hello

Thank you for additional information. Please try using the following code:

public class RemoveFields : DocumentVisitor
{
    private int _level;
    private readonly FieldType _removedType;
    private bool Delete
    {
        get
        {
            return _level > 0;
        }
    }
    public RemoveFields(FieldType type)
    {
        _removedType = type;
    }
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        if (fieldStart.FieldType == _removedType)
        {
            _level++;
            fieldStart.Remove();
        }
        else
        {
            if (Delete)
                fieldStart.Remove();
        }
        return VisitorAction.Continue;
    }
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        if (fieldSeparator.FieldType == _removedType)
        {
            _level--;
            fieldSeparator.Remove();
        }
        else
        {
            if (Delete)
                fieldSeparator.Remove();
        }
        return VisitorAction.Continue;
    }
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        if (fieldEnd.FieldType == _removedType)
        {
            fieldEnd.Remove();
        }
        else
        {
            if (Delete)
                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;
    }
    public override VisitorAction VisitSectionEnd(Section section)
    {
        if (Delete)
        {
            Section nextSection = (Section)section.NextSibling;
            if (nextSection != null)
            {
                nextSection.PrependContent(section);
                section.Remove();
            }
        }
        return VisitorAction.Continue;
    }
}

Best regards,

Hello
Your code works! It’s great!
Thank you for the help!