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.

Thu Jul 22, 2021 9:46 pm

Good morning, I want to create a certain amount of lines in the file starting from the variable pattern that exists in it, Example

In document variables:
Type.*.Size / Type.*.Height

And the result after the replace would turn (result after replace variables)

1/2
3/4
5/6

I managed to create the logic that makes the substitution respecting the loop and using the method "C# add new text strings after the searched text string in word document"
result:
1/2 3/4 5/6

But it is one in front of the other, is there a way to replace this by creating lines?

mestrinel
 
Posts: 7
Joined: Wed Apr 10, 2019 6:12 pm

Fri Jul 23, 2021 5:42 am

Hello,

Thanks for your inquiry.
Please refer to the following code to meet your needs.
Code: Select all
            Document document = new Document();
            document.LoadFromFile("test.docx");
            Body body = document.Sections[0].Body;

            TextSelection[] textSelections = document.FindAllString("Type.*.Size / Type.*.Height", false, true);
            foreach (TextSelection selection in textSelections)
            {
                Paragraph ownerParagraph = selection.GetAsOneRange().OwnerParagraph;
                int index = body.ChildObjects.IndexOf(ownerParagraph);               
                for (int i=5; i >=1; i = i-2)
                {
                    Paragraph paragraph = new Paragraph(document);
                    paragraph.AppendText(i + "/" + (i + 1));
                    body.ChildObjects.Insert(index, paragraph);
                }
                body.ChildObjects.Remove(ownerParagraph);
            }

            document.SaveToFile("result.docx", FileFormat.Docx);

If this cannot help you, please provide your input file and your desired output. You can send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1241
Joined: Mon Oct 19, 2020 3:04 am

Tue Jul 27, 2021 12:24 am

Hello, thanks for response. Almost that, for the purpose it worked correctly, but I need (if possible) a little tip, the values ​​to be passed are in different variables and not just one, when I insert the variable {{Window.*.Height}} it has the behavior I expected, but the variable {{WINDOW.*.WIDTH}} has to go ahead of it like this in the attached document. Sort of putting together a list of heights/widths in lines.


Below is the dictionary I am using:

Code: Select all
IDictionary<string, string> listParameters = new Dictionary<string, string>();
listParameters.Add("Window.1.Height", "1.1");
listParameters.Add("Window.2.Height", "1.2");
listParameters.Add("Window.3.Height", "1.3");
listParameters.Add("Window.1.Width", "2.1");
listParameters.Add("Window.2.Width", "2.2");
listParameters.Add("Window.3.Width", "2.3");


My Code:

Code: Select all
document.LoadFromStream(_stream, FileFormat.Docx);

                string wordText = document.GetText();

                var listParamWord = Regex.Matches(wordText, @"{{(\S+)}}");

                foreach (var _paramWord in listParamWord)
                {
                    if (_paramWord.ToString().Contains("*"))
                    {
                        bool bContinue = true;
                        int k = 0;
                       
                        TextSelection[] selections = document.FindAllString(_paramWord.ToString(), true, true);
                        Body body = document.Sections[0].Body;

                        if (selections != null)
                        {
                            while (bContinue)
                            {
                                k = ++k;
                                string sReplace = _paramWord.ToString().Replace("{{", "").Replace("}}", "").Replace("*", k.ToString());

                                if (_Dictonary.Keys.Contains(sReplace))
                                {
                                    foreach (TextSelection selection in selections)
                                    {
                                        Paragraph ownerParagraph = selection.GetAsOneRange().OwnerParagraph;
                                        int index = body.ChildObjects.IndexOf(ownerParagraph);
                                        if (index >= 0)
                                        {
                                            Paragraph paragraph = new Paragraph(document);
                                            paragraph.AppendText(_Dictonary[sReplace]);
                                            body.ChildObjects.Insert(index, paragraph);
                                            //body.ChildObjects.Remove(ownerParagraph);
                                        }
                                    }
                                }
                                else
                                    bContinue = false;
                            }
                        }
                    }
                }


attached the word I'm using for testing.


My result now:
Code: Select all
1.1
1.2
1.3
2.1
2.2
2.3
 /


expectation:

Code: Select all
1.1 / 2.1
1.2 / 2.2
1.3 / 2.3
You do not have the required permissions to view the files attached to this post.

mestrinel
 
Posts: 7
Joined: Wed Apr 10, 2019 6:12 pm

Tue Jul 27, 2021 3:00 am

Hello,

Thanks for your feedback.
Please refer to the following modified code to meet your needs.
Code: Select all
            IDictionary<string, string> listParameters = new Dictionary<string, string>();
            listParameters.Add("Window.1.Height", "1.1");
            listParameters.Add("Window.2.Height", "1.2");
            listParameters.Add("Window.3.Height", "1.3");
            listParameters.Add("WINDOW.1.WIDTH", "2.1");
            listParameters.Add("WINDOW.2.WIDTH", "2.2");
            listParameters.Add("WINDOW.3.WIDTH", "2.3");

            Document document = new Document();
            document.LoadFromFile("test.docx", FileFormat.Docx);

            string wordText = document.GetText();

            var listParamWord = Regex.Matches(wordText, @"{{(\S+)}}");
            var isFirstMatch = true;

            Body body = document.Sections[0].Body;
            List <Paragraph> paragraphs = new List<Paragraph> ();
            Paragraph ownerParagraph=null;
            foreach (var _paramWord in listParamWord)
            {
                if (_paramWord.ToString().Contains("*"))
                {
                    bool bContinue = true;
                    int k = 0;

                    TextSelection[] selections = document.FindAllString(_paramWord.ToString(), true, true);

                    if (selections != null)
                    {
                        while (bContinue)
                        {
                            k = ++k;
                            string sReplace = _paramWord.ToString().Replace("{{", "").Replace("}}", "").Replace("*", k.ToString());

                            if (listParameters.Keys.Contains(sReplace))
                            {
                                foreach (TextSelection selection in selections)
                                {
                                    ownerParagraph = selection.GetAsOneRange().OwnerParagraph;
                                    int index = body.ChildObjects.IndexOf(ownerParagraph);
                                    if (index >= 0 && isFirstMatch==true)
                                    {
                                        Paragraph paragraph = new Paragraph(document);
                                        paragraph.AppendText(listParameters[sReplace]);
                                        paragraphs.Add(paragraph);
                                    }
                                    else if(index >= 0 && isFirstMatch == false)
                                    {
                                        paragraphs[k-1].AppendText(" / "+listParameters[sReplace]);
                                    }
                                }
                            }
                            else
                                bContinue = false;
                        }
                    }
                }
                isFirstMatch = false;
            }

            foreach(Paragraph newParagraph in paragraphs)
            {
                TextSelection[] selections = document.FindAllString(listParamWord[0].ToString(), true, true);
                if (selections != null)
                {
                    ownerParagraph = selections[0].GetAsOneRange().OwnerParagraph;
                    int index = body.ChildObjects.IndexOf(ownerParagraph);
                    body.ChildObjects.Insert(index, newParagraph);
                }
            }
            body.ChildObjects.Remove(ownerParagraph);
            document.SaveToFile("result.docx");

If there are any other issues related to our products, just feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1241
Joined: Mon Oct 19, 2020 3:04 am

Fri Jul 30, 2021 10:54 am

Hello,

Greetings from E-iceblue!
Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1241
Joined: Mon Oct 19, 2020 3:04 am

Sun Aug 01, 2021 2:32 pm

Hi Brian, how are you? Yes the code worked, thanks for the feedback. But performing a deeper test I noticed that when the variable is inside a tab the code does not work, does it have to be adapted to some other different code or is it a limitation of the tool itself?
I attach the test document.
You do not have the required permissions to view the files attached to this post.

mestrinel
 
Posts: 7
Joined: Wed Apr 10, 2019 6:12 pm

Mon Aug 02, 2021 2:13 am

Hello,

Thanks for your feedback.
Please refer to the following modified code to meet your needs.
Code: Select all
            IDictionary<string, string> listParameters = new Dictionary<string, string>();
            listParameters.Add("Window.1.Height", "1.1");
            listParameters.Add("Window.2.Height", "1.2");
            listParameters.Add("Window.3.Height", "1.3");
            listParameters.Add("WINDOW.1.WIDTH", "2.1");
            listParameters.Add("WINDOW.2.WIDTH", "2.2");
            listParameters.Add("WINDOW.3.WIDTH", "2.3");

            Document document = new Document();
            document.LoadFromFile("test.docx", FileFormat.Docx);

            string wordText = document.GetText();

            var listParamWord = Regex.Matches(wordText, @"{{(\S+)}}");
            var isFirstMatch = true;
            int t = -1;
            int i = -1;
            int y = -1;

            Body body = document.Sections[0].Body;
            List<Paragraph> paragraphs = new List<Paragraph>();
            Paragraph ownerParagraph = null;
            foreach (var _paramWord in listParamWord)
            {
                if (_paramWord.ToString().Contains("*"))
                {
                    bool bContinue = true;
                    int k = 0;

                    TextSelection[] selections = document.FindAllString(_paramWord.ToString(), true, true);

                    if (selections != null)
                    {
                        while (bContinue)
                        {
                            k = ++k;
                            string sReplace = _paramWord.ToString().Replace("{{", "").Replace("}}", "").Replace("*", k.ToString());

                            if (listParameters.Keys.Contains(sReplace))
                            {
                                foreach (TextSelection selection in selections)
                                {
                                    ownerParagraph = selection.GetAsOneRange().OwnerParagraph;
                                    if (ownerParagraph.Owner is Section)
                                    {
                                        int index = body.ChildObjects.IndexOf(ownerParagraph);
                                        if (index >= 0 && isFirstMatch == true)
                                        {
                                            Paragraph paragraph = new Paragraph(document);
                                            paragraph.AppendText(listParameters[sReplace]);
                                            paragraphs.Add(paragraph);
                                        }
                                        else if (index >= 0 && isFirstMatch == false)
                                        {
                                            paragraphs[k - 1].AppendText(" / " + listParameters[sReplace]);
                                        }
                                    }
                                    if (ownerParagraph.Owner is TableCell)
                                    {
                                        Table table = ownerParagraph.Owner.Owner.Owner as Table;
                                        TableRow tableRow = ownerParagraph.Owner.Owner as TableRow;
                                        TableCell tableCell = ownerParagraph.Owner as TableCell;
                                        t = body.Tables.IndexOf(table);
                                        i = body.Tables[t].Rows.IndexOf(tableRow);
                                        y = body.Tables[t].Rows[i].Cells.IndexOf(tableCell);
                                        int index = body.Tables[t].Rows[i].Cells[y].ChildObjects.IndexOf(ownerParagraph);
                                        if (index >= 0 && isFirstMatch == true)
                                        {
                                            Paragraph paragraph = new Paragraph(document);
                                            paragraph.AppendText(listParameters[sReplace]);
                                            paragraphs.Add(paragraph);
                                        }
                                        else if (index >= 0 && isFirstMatch == false)
                                        {
                                            paragraphs[k - 1].AppendText(" / " + listParameters[sReplace]);
                                        }
                                    }
                                }
                            }
                            else
                                bContinue = false;
                        }
                    }
                }
                isFirstMatch = false;
            }

            foreach (Paragraph newParagraph in paragraphs)
            {
                TextSelection[] selections = document.FindAllString(listParamWord[0].ToString(), true, true);
                if (selections != null)
                {
                    if (t == -1 || i == -1 || y == -1)
                    {
                        ownerParagraph = selections[0].GetAsOneRange().OwnerParagraph;
                        int index = body.ChildObjects.IndexOf(ownerParagraph);
                        body.ChildObjects.Insert(index, newParagraph);
                    }
                    else
                    {
                        ownerParagraph = selections[0].GetAsOneRange().OwnerParagraph;
                        int index = body.Tables[t].Rows[i].Cells[y].ChildObjects.IndexOf(ownerParagraph);
                        body.Tables[t].Rows[i].Cells[y].ChildObjects.Insert(index, newParagraph);
                    }
                }
            }
            if (t == -1 || i == -1 || y == -1)
            {
                body.ChildObjects.Remove(ownerParagraph);
            }
            else
            {
                body.Tables[t].Rows[i].Cells[y].ChildObjects.Remove(ownerParagraph);
            }
            document.SaveToFile("result.docx");


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1241
Joined: Mon Oct 19, 2020 3:04 am

Wed Aug 04, 2021 2:34 pm

Hi Brian, thanks so much for your help. In our simple test file we were doing this solution worked, however observing your code I checked the following logic:

Code: Select all
TextSelection[] selections = document.FindAllString(listParamWord[0].ToString(), true, true);


All code:
Code: Select all
foreach (Paragraph newParagraph in paragraphs)
{
   TextSelection[] selections = document.FindAllString(listParamWord[0].ToString(), true, true);
   if (selections != null)
   {
      if (t == -1 || i == -1 || y == -1)
      {
         ownerParagraph = selections[0].GetAsOneRange().OwnerParagraph;
         int index = body.ChildObjects.IndexOf(ownerParagraph);
         body.ChildObjects.Insert(index, newParagraph);
      }
      else
      {
         ownerParagraph = selections[0].GetAsOneRange().OwnerParagraph;
         int index = body.Tables[t].Rows[i].Cells[y].ChildObjects.IndexOf(ownerParagraph);
         body.Tables[t].Rows[i].Cells[y].ChildObjects.Insert(index, newParagraph);
      }
   }
}
if (t == -1 || i == -1 || y == -1)
{
   body.ChildObjects.Remove(ownerParagraph);
}
else
{
   body.Tables[t].Rows[i].Cells[y].ChildObjects.Remove(ownerParagraph);
}


But we have several pages and several variables, not always the first variable that comes from the file will be this list treatment, it can be just a simple replace.

I've uploaded a part of our structure with a few more variables that we use. In addition to the option doors that will also have this control to replace Doors.1.height and etc.

I tried to change the code to get the first parameter of the list to replace when executed and it worked, but when it goes to execute other options such as doors, faucets and other items that may have dimensions, it gets lost. We have the following pattern, all {{variables}} within the braces {{}} are simple and straightforward replacements, but the {{variables.*.text}} that contain * between dots are lists that can have multiple lines 1, 2,3,4,5,6... How would this logic look?

Attached, the list of parameters and the word, thanks again.
You do not have the required permissions to view the files attached to this post.

mestrinel
 
Posts: 7
Joined: Wed Apr 10, 2019 6:12 pm

Thu Aug 05, 2021 1:46 pm

Hello,

Thanks for your feedback.
Please refer to the following modified code to meet your needs.
Code: Select all
            IDictionary<string, string> listParameters = new Dictionary<string, string>();
            listParameters.Add("CShed.Columnframings", "Columnframings");
            listParameters.Add("CShed.Mullionframings", "Mullionframings");
            listParameters.Add("TRUSS.FRAME", "FRAME");
            listParameters.Add("CShed.Rafterframings", "Rafterframings");
            listParameters.Add("CShed.Purlinframings", "Purlinframings");
            listParameters.Add("CShed.Girtframings", "Girtframings");
            listParameters.Add("CShed.Fasciaframings", "Fasciaframings");
            listParameters.Add("CShed.Bracingframings", "Bracingframings");
            listParameters.Add("CShed.Bridgebeamframings", "Bridgebeamframings");
            listParameters.Add("SkylightType", "SkylightType");
            listParameters.Add("SkylightColour", "SkylightColour");
            listParameters.Add("SpinawayType", "SpinawayType");
            listParameters.Add("CShed.SpinawayColours", "SpinawayColours");
            listParameters.Add("Window.1.Height", "1.1");
            listParameters.Add("Window.2.Height", "1.2");
            listParameters.Add("Window.3.Height", "1.3");
            listParameters.Add("Window.1.Width", "2.1");
            listParameters.Add("Window.2.Width", "2.2");
            listParameters.Add("Window.3.Width", "2.3");
            listParameters.Add("Doors.1.Height", "11");
            listParameters.Add("Doors.2.Height", "12");
            listParameters.Add("Doors.3.Height", "13");
            listParameters.Add("Doors.1.Width", "211");
            listParameters.Add("Doors.2.Width", "222");
            listParameters.Add("Doors.3.Width", "233");
            listParameters.Add("Extra1", "Extra1");
            listParameters.Add("Extra2", "Extra2");
            listParameters.Add("Extra3", "Extra3");
            listParameters.Add("Pizza.1.Colour", "Blue");
            listParameters.Add("Pizza.2.Colour", "Red");
            listParameters.Add("Pizza.3.Colour", "Silver");

            Document document = new Document();
            document.LoadFromFile("NewTest.docx", FileFormat.Docx);

            foreach (var item in listParameters)
            {
                int t = -1, r = -1, c = -1;

                int i = 1;
                if (item.Key.Contains("." + i.ToString() + "."))
                {
                    string v = item.Key.Replace(i.ToString(), "*").ToString();
                    TextSelection[] textSelections = document.FindAllString(v, true, true);
                    if(textSelections != null)
                    {
                        Paragraph ownerParagraph = textSelections[0].GetAsOneRange().Owner as Paragraph;

                        if (ownerParagraph.Owner is TableCell)
                        {
                            Table table = ownerParagraph.Owner.Owner.Owner as Table;
                            TableRow tableRow = ownerParagraph.Owner.Owner as TableRow;
                            TableCell tableCell = ownerParagraph.Owner as TableCell;
                            t = document.Sections[0].Tables.IndexOf(table);
                            r = document.Sections[0].Tables[t].Rows.IndexOf(tableRow);
                            c = document.Sections[0].Tables[t].Rows[r].Cells.IndexOf(tableCell);
                            int index = document.Sections[0].Tables[t].Rows[r].Cells[c].ChildObjects.IndexOf(ownerParagraph);
                            for (; i<=3;i++ )
                            {
                                Paragraph cloneParagraph = ownerParagraph.Clone() as Paragraph;
                                cloneParagraph.Replace("*",i.ToString(),true,true);
                                document.Sections[0].Tables[t].Rows[r].Cells[c].ChildObjects.Insert(index+i,cloneParagraph);
                            }
                            document.Sections[0].Tables[t].Rows[r].Cells[c].ChildObjects.Remove(ownerParagraph);
                        }

                    }
                }
                document.Replace(new Regex("{{" + item.Key +@"\s*}}"), item.Value);

            }

            document.SaveToFile("result.docx");


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1241
Joined: Mon Oct 19, 2020 3:04 am

Thu Aug 19, 2021 12:08 pm

Brian, thank you I'm testing and so far everything is working.

mestrinel
 
Posts: 7
Joined: Wed Apr 10, 2019 6:12 pm

Fri Aug 20, 2021 2:17 am

You are welcome.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1241
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc