Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Apr 22, 2014 12:26 pm

Hi

I want to erase text between 2 specific pieces of text ie, if the text is:

[start]
this is some text
[end]

I want to remove everything between [start] and [end] so it finally looks like


[start]
[end]

I cannot do a replace on 'this is some text' as that will be different each time


Any ideas how to accomplish this

Thanks

Chris

cjacs
 
Posts: 10
Joined: Fri Feb 21, 2014 11:11 am

Wed Apr 23, 2014 2:39 am

Hello,

Thanks for your inquiry.
There are some codes for your reference. If there are any questions, welcome to get it back to us.
Code: Select all
static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("test.docx");
            foreach (Section sec in doc.Sections)
            {
                int StartIndex = GetIndex(sec, "[start]");
                int EndIndex = GetIndex(sec, "[end]");
                int i = StartIndex + 1;
                while (i < EndIndex)
                {
                    sec.Paragraphs.Remove(sec.Paragraphs[i]);
                    EndIndex -= 1;
                }
            }
            doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
}
public static int GetIndex(Section sec, string SpecialString)
{
            int index = 0;
            int i = 0;
            foreach (Paragraph para in sec.Paragraphs)
            {
                if (para.Text == SpecialString)
                {
                    index = i;
                }
                i++;
            }
            return index;
}

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Wed Apr 23, 2014 10:29 am

Hi Gary

Many thanks for your quick reply, that worked perfectly

Much appreciated


Chris

cjacs
 
Posts: 10
Joined: Fri Feb 21, 2014 11:11 am

Wed Apr 23, 2014 10:45 am

Gary

I want to use this method to remove text when on the same line, ie

[start] I want to remove this text [end]

to become

[start] [end]

Which doesn't work with your code, do you have an amendment that would do that please?

thank you for your help

Chris

cjacs
 
Posts: 10
Joined: Fri Feb 21, 2014 11:11 am

Thu Apr 24, 2014 2:44 am

Hello Chris,

There are some codes for your reference.
Code: Select all
 class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile(@"..\..\test.docx");
            DocumentObject start = doc.FindString("[start]",true,true).GetAsOneRange();
            DocumentObject end = doc.FindString("[end]", true, true).GetAsOneRange();
            RemoveRange(start,end);
            doc.SaveToFile(@"..\..\result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start(@"..\..\result.docx");
        }
        public static void RemoveRange(DocumentObject start, DocumentObject end)
        {
            HashSet<DocumentObject> endElements = new HashSet<DocumentObject>();
            DocumentObject parent = end;
            while (parent != null)
            {
                endElements.Add(parent);
                parent = parent.Owner;
            }

            parent = start.Owner;
            DocumentObject current = start;
            DocumentObject lastStart = start;
            while (parent != null)
            {
                ICompositeObject container = (parent as ICompositeObject);
                DocumentObjectCollection objs = container.ChildObjects;
                int index = objs.IndexOf(current) + 1;
                while (objs.Count > index)
                {
                    DocumentObject element = objs[index];
                    if (endElements.Contains(element))
                    {
                        parent = null;
                        lastStart = current;
                        break;
                    }
                    objs.RemoveAt(index);
                }

                if (parent != null)
                {
                    if (parent.DocumentObjectType == DocumentObjectType.Body)
                    {
                        lastStart = parent.Owner;
                        break;
                    }
                    current = parent;
                    parent = parent.Owner;
                }
            }

            parent = end.Owner;
            current = end;
            while (parent != null)
            {
                ICompositeObject container = (parent as ICompositeObject);
                DocumentObjectCollection objs = container.ChildObjects;
                int index = objs.IndexOf(current) - 1;
                while (index >= 0)
                {
                    DocumentObject element = objs[index];
                    if (lastStart == element)
                    {
                        parent = null;
                        break;
                    }
                    objs.RemoveAt(index);
                    index--;
                }

                if (parent != null)
                {
                    current = parent;
                    parent = parent.Owner;
                }
            }
        }
    }

If there are any questions, welcome to get it back to us.
Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc