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 Jan 17, 2017 1:35 pm

Hi,
i seacrh how to create a TextSelection with all text between 2 tags.
I can easily find the tags :
Code: Select all
                Regex regEx = new Regex("\\?my_tag\\?");
                TextSelection[] selections = doc.FindAllPattern(regEx);
                if (selections!=null && selections.Count()==2)
                {
                    //select text ....

                    //delete selection
                }

LPCR
 
Posts: 2
Joined: Tue Jan 17, 2017 1:09 pm

Wed Jan 18, 2017 6:59 am

Hi LPCR,

Thanks for you inquiry.
You could find the TextSelection you want to change, and then replace it with new text. Please see the code as reference which I have tested below.

Code: Select all
            string s1 = "begin";
            string s2 = "end";
            Regex regEx = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + s2 + "))");
            //Get the selections between "begin" and "end"
            TextSelection[] selections = doc.FindAllPattern(regEx);
            if (selections != null)
            {     
                TextRange range = selections[0].GetAsOneRange();
                //Replace with new text
                range.Text = "this is replace content";             
            }

            doc.SaveToFile("replace.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("replace.docx");

Hope it will help. If i misunderstanding, please contact us and give more information.

Sincerely,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Wed Jan 18, 2017 9:53 am

Thanks for your response,
but in my case the 2 tags are not in the same line.
they can be in different pages. So change the regex is not enougth

LPCR
 
Posts: 2
Joined: Tue Jan 17, 2017 1:09 pm

Thu Jan 19, 2017 9:51 am

Hello LPCR,

After an investigation, I generate the code below for your reference.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile("insertText.docx");

            string s1 = "begin";
            string s2 = "end";
            StringBuilder sb = new StringBuilder();
            TextSelection[] selections1 = doc.FindAllString(s1, true, true);
            TextSelection[] selections2 = doc.FindAllString(s2, true, true);
           
            TextRange tag1Range = selections1[0].GetAsOneRange();
            TextRange tag2Range = selections2[0].GetAsOneRange();

            ParagraphCollection pc = doc.Sections[0].Paragraphs;

            //get the paragraph index of 2 tags
            int pIndexBegin = pc.IndexOf(tag1Range.OwnerParagraph);
            int pIndexEnd = pc.IndexOf(tag2Range.OwnerParagraph);
           
            //get the follow text of tag1 in the paragraph where tag1 in
            DocumentObjectCollection tag1objs = pc[pIndexBegin].ChildObjects;
            TextRange range1 = selections1[0].GetAsOneRange();
            int childIndex1 = tag1objs.IndexOf(range1);
            for (int i = childIndex1 + 1; i < tag1objs.Count; i++)
            {
                TextRange bRange = tag1objs[i] as TextRange;
                string str = bRange.Text;

                sb.Append(str);

            }

            //get the text of paragraphs between 2 tags
            for (int i = pIndexBegin + 1; i < pIndexEnd; i++) {
                sb.Append(pc[i].Text);
            }

            //get the pre text of tag2 in the paragraph where tag2 in
            DocumentObjectCollection tag2objs = pc[pIndexEnd].ChildObjects;
            TextRange range2 = selections2[0].GetAsOneRange();
            int childIndex2 = tag2objs.IndexOf(range2);
            for (int i = 0; i < childIndex2; i++)
            {
                TextRange bRange = tag2objs[i] as TextRange;
                if (bRange != null) {
                    string str = bRange.Text;
                    sb.Append(str);
                    if (str == s2)
                    {
                        break;
                    }
                }

            }

If there is any questions, please let us know.

Sincerely,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Mon Jan 23, 2017 8:57 am

Hello LPCR,

How is the issue going? Is the code I provided helpful to you? It will be much appreciated, if your give us some feed back.
Any questions, please let us know. We are always at your service.

Sincerely,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Wed Mar 15, 2017 1:05 pm

Hello Simon,

I have the same problem as LPCR, but I'm not sure I understand the reference code you posted. The question was for a TextSelection, but it looks like this code puts all the 'selected' tekst in a stringbuilder.

Is there a way to get all this in a TextSelection so I can delete or modify the whole selection? And is it not possible for the FindPattern results to span several lines or even pages (if not, are there any plans to support this in the future)?
Regex is able to return results spanning multiple lines, so I'd expect FindPattern to behave the same.

Regards,
Saskia Brand

brand
 
Posts: 1
Joined: Wed Mar 15, 2017 1:00 pm

Thu Mar 16, 2017 7:37 am

Dear Brand,

Thanks for your inquiry.
Please use the method FindPatternInLine to find the text spanning several paragraphs or even pages.
Code: Select all
            Regex regEx = new Regex(@"(?<=start)[.\s\S]*(?=end)");
            TextSelection[] selections = doc.FindPatternInLine(regEx);

If there is any question, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Mar 21, 2017 8:27 am

Dear Brand,

Did you test the method I mentioned ? Has your issue been resolved ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc