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

Free Support Forum - aspose.com

Removing a field

I seem to be having an issue with removing a field where fieldstart and fieldend are seperated by paragraph marks.

In the document explorer (attached file), the code below gets to the run outlined and stops thereby leaving the orphaned FieldEnd.

Below is the code that I'm using, which does work for fields where the FieldStart and FieldEnd are not seperated by paragraphs.

        /// 
        /// Removes the field.
        /// 
        /// <param name="fieldStart">The field start.
        private void RemoveField(FieldStart fieldStart)
        {
            Node node = fieldStart;
            bool isRemoving = true;
            while (node != null && isRemoving)
            {
                if (node.NodeType == NodeType.FieldEnd)
                    isRemoving = false;
            <span style="color: rgb(43, 145, 175);">Node</span> nextNode = node.NextPreOrder(node.Document);
            node.Remove();
            node = nextNode;
        }</pre><pre style="background: white; color: black; font-family: Consolas;">   	}</pre><p> Any assistance that you can provide is greatly appreciated.</p><p>Thanks</p><p>Darel</p>

Hi

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

// Open document.

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

RemoveFields rem = new RemoveFields(FieldType.FieldIf); // or any other FieldType

doc.Accept(rem);

doc.Save("out.doc");

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,

Thanks, however I just need to remove an individual field not all fields. In my case I have located the field and got the fieldStart. Is there a way to do this?

Thanks,

Darel

Hi Darel,

Thanks for your inquiry.

You can use code like below to remove a field based off its FieldStart node. Please note that it's simplified and will not work if the field contains field.

private static void RemoveField(Aspose.Words.Fields.FieldStart fieldStart)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

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;

}

}

Thanks,

Thanks Adam... The code you provided is the same code in my post that doesn't work if there are paragraph markers within the field. In my original post you can see the document explorer where I maked the last nod that is removed, you will note it is before the fieldEnd.

Darel

Hi Darel,

Thanks for this additional information. My apologises, I should of read the first few posts as well. Please try the code below which should achieve what you are looking for, if you have any troubles I will be glad to assist further.

private static void RemoveField(FieldStart fieldStart)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

Node node = fieldStart;

bool isRemoving = true;

while (node != null && isRemoving)

{

if (node.NodeType == NodeType.FieldEnd)

isRemoving = false;

Node nextNode = node.IsComposite ? node.NextSibling : node.NextPreOrder(node.Document);

if(node.IsComposite && ((CompositeNode)node).GetChildNodes(NodeType.FieldEnd, false).Count > 0)

{

CompositeNode composite = (CompositeNode)node;

while(composite.HasChildNodes && composite.FirstChild.NodeType != NodeType.FieldEnd)

{

composite.FirstChild.Remove();

nextNode = composite.FirstChild;

}

}

else

{

node.Remove();

}

node = nextNode;

}

}

Thanks,