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.

Wed Apr 08, 2020 7:30 pm

Hi, I have a question that I can't find on the documentation.
I started to use it, and I need to insert a Head Text, and bellow, a Paragraph Text in a determinated position, that I will find by string on the document.

Example:
Title (in a different size)
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.


I tried:
Code: Select all
document.Replace("Replace1", "Text I want"));


This work succefully, but, I need to insert a Head texting, and text paragraph.
I find a class calls Paragraph, and I saw that have text proprieties that I can use to do a Head Text.

Can someone help me?

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Thu Apr 09, 2020 6:48 am

Hi,

Thanks for your post.
As for "Head Text", does it refer to the text with Heading style? Do you want to replace a given text with a paragraph that contains the text with Heading style?
If the answer to all of the above is yes, here I do a demo for you.
Input:
input.png

Output:
output.png

Full code:
Code: Select all
//Create word document
            Document document = new Document();

            //Load the document from disk.
            document.LoadFromFile(folder+"sample.docx");

            //Find text that will be replaced.
            String sourceText = "Title";
            TextSelection[] selections = document.FindAllString(sourceText, false, true);
            //Create a new paragrph that needs to replace "Title" with
            Paragraph newParagrph = new Paragraph(document);
            newParagrph.AppendText("New heading paragrph");
            //Set heading style
            newParagrph.ApplyStyle(BuiltinStyle.Heading1);

            for (int i = 0; i < selections.Length; i++)
            {
                TextRange textRange = selections[i].GetAsOneRange();
                Paragraph paragraph = textRange.OwnerParagraph;
                int textRangeIndex = paragraph.ChildObjects.IndexOf(textRange);
                Body body = paragraph.OwnerTextBody;
                int paragraphIndex = body.ChildObjects.IndexOf(paragraph);
                //sample1 case:
                if (paragraph.Text.Trim() == sourceText)
                {
                    body.ChildObjects.Remove(paragraph);
                    body.ChildObjects.Insert(paragraphIndex, newParagrph.Clone());
                }
                //sample2 case:
                else if (paragraph.ChildObjects.FirstItem.Equals(textRange))
                {
                    paragraph.ChildObjects.Remove(textRange);
                    body.ChildObjects.Insert(paragraphIndex, newParagrph.Clone());
                }
                //sample3 case:
                else if (textRangeIndex > 0 && textRangeIndex < paragraph.ChildObjects.Count-1)
                {
                    paragraph.ChildObjects.Remove(textRange);
                    int count = paragraph.ChildObjects.Count;
                    Paragraph newParagraph2 = new Paragraph(document);
                    for (int j = textRangeIndex; j < count; j++)
                    {
                        newParagraph2.ChildObjects.Add(paragraph.ChildObjects[j].Clone());
                    }

                    for (int j = textRangeIndex; j < count; j++)
                    {
                        paragraph.ChildObjects.RemoveAt(textRangeIndex);
                    }
                 
                    body.ChildObjects.Insert(paragraphIndex+1, newParagrph.Clone());
                    body.ChildObjects.Insert(paragraphIndex + 2, newParagraph2);
                }
                //sample4 case:
                else if (paragraph.ChildObjects.LastItem.Equals(textRange))
                {
                    paragraph.ChildObjects.Remove(textRange);
                    body.ChildObjects.Insert(paragraphIndex + 1, newParagrph.Clone());
                }
            }
           

            String output="output.docx";
            document.SaveToFile(output, FileFormat.Docx2013);

If I mistake your requirement, please describe it in detail.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Apr 13, 2020 7:44 pm

That's it!
Thanks fella

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Tue Apr 14, 2020 1:21 am

Hi,

Thanks for your feedback.
I am glad to know that it helps.
If you need any help, please feel free to contact us.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc