Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Tue Apr 20, 2021 2:41 am

Hi Team,

I've a document:

@Key: blablala
[paragraph]
@Key1: clalalala
[paragraph]
[paragraph]
@Key: dlalalala
[paragraph]
@Key: 789

I got index of @Key1.

1. How can I get index of the next @Key from @Key1?

2. How can I count number of @Key from @Key1?

Thank you very much!

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Tue Apr 20, 2021 7:28 am

Hello,

Thanks for your inquiry!

Kindly note that our Spire.Doc cannot get the index of the word “@key” directly, but can get the TextRange where the “@key” is located.

Please refer to the following code to get the TextRange of the next “@key” from the “@key1” and the number of “@Key” after“@Key1”.
Code: Select all
            //Create word document
            Document document = new Document();

            //Load the document from disk.
            document.LoadFromFile(@"E:\testdoc\key.docx");

            //Find key1
            TextSelection[] textSelections = document.FindAllString("@key1", false, true);

            //get each index of key1 in the para,section,document
            int key1TextIndex = 0;
            int key1ParaIndex = 0;
            int key1SectIndex = 0;
            foreach (TextSelection selection in textSelections)
            {
                Paragraph key1Para =  selection.GetAsOneRange().OwnerParagraph;
                key1TextIndex = key1Para.GetIndex(selection.GetAsOneRange());
                key1ParaIndex = key1Para.OwnerTextBody.GetIndex(key1Para);
                key1SectIndex = document.GetIndex(key1Para.OwnerTextBody.Owner);
            }

            List<int> key = new List<int>();    //save the conform index of "@key"finds
            //find key
            TextSelection[] keyFinds = document.FindAllString("@key", false, true);
            for(int i =0; i < keyFinds.Length; i++)
            {
                TextSelection keyfind = keyFinds[i];
                Paragraph keyFindPara = keyfind.GetAsOneRange().OwnerParagraph;
               
                //key key1 in the same section
                if(document.GetIndex(keyFindPara.OwnerTextBody.Owner).Equals(key1SectIndex))
                {
                    //key paragraph under the key1
                    if(keyFindPara.OwnerTextBody.GetIndex(keyFindPara) > key1ParaIndex)
                    {
                        key.Add(i);
                    }else if (keyFindPara.OwnerTextBody.GetIndex(keyFindPara).Equals(key1ParaIndex)){
                        //key key1 in the same paragraph, but key textrange under the key1
                        if (keyFindPara.GetIndex(keyfind.GetAsOneRange()) > key1TextIndex)
                        {
                            key.Add(i);
                        }
                    }
                }
                //key section under the key1
                else if(document.GetIndex(keyFindPara.OwnerTextBody.Owner) > key1SectIndex)
                {
                    key.Add(i);
                }

            }

            TextRange theFirstKey = keyFinds[key[0]].GetAsOneRange();
            int count = key.Count;


If the code does not meet your needs, please describe your needs in detail, and provide us with your input file for reference. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 408
Joined: Wed Nov 04, 2020 2:29 am

Tue Apr 20, 2021 7:58 am

it works extremely effective!!!

Thank you very much!!!

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Tue Apr 20, 2021 8:37 am

Hello,

You are welcome.

Glad to hear that the code is working.

If you encounter any issues related to our product in the future, just feel free to contact us.

Have a nice day!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 408
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc