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.

Sun May 29, 2022 8:01 am

Hello
I have a template file named "temp.docx". I named the first paragraph of each section in this file G_1, G_2, G_3, G_4 and separated it with the string -----SECTION_BREAK-----.

I named the file that I wanted to work on as "main.docx". In this file, there are places that said G_1, G_2, G_3, G_4.

I want to split the text in the "temp.docx" file from the string -----SECTION_BREAK----- and paste the G_1 section in the "temp.docx" file where it says G_1 in the "main.docx" file. Likewise, I want to paste the G_2 section in the "temp.docx" file where it says G_2 in the "main.docx" file. Of course, I would appreciate it if it also shows the "pasting options". Or have it automatically merge the formatting.

Is something like this possible?

My best regards.

jimjerry
 
Posts: 2
Joined: Sun May 29, 2022 7:24 am

Mon May 30, 2022 3:36 am

Hello Jim,

Thanks for your inquiry.
Spire.Doc can do that for you. Here is a simple demo for you. Please give it a try. If there is any other quesions, just feel free to contact us.
Code: Select all
        static void Main(string[] args)
        {
            Document temp = new Document();
            temp.LoadFromFile("temp.docx");
            Document main = new Document();
            main.LoadFromFile("main.docx");

            string[] paraKeys = { "G_1", "G_2", "G_3", "G_4" };
            string breakKey = "-----SECTION_BREAK-----";

            TextSelection[] breakSelections = temp.FindAllString(breakKey, false, true);
            TextSelection parSelection, mainParSelection;
            TextBodyPart body;
            Paragraph p1, p2, mainPara;
            int i = 0;
            foreach (string str in paraKeys)
            {
                parSelection = temp.FindString(str, false, true);
                p1 = parSelection.GetAsOneRange().OwnerParagraph;
                if (i >= breakSelections.Length)
                {
                    p2 = null;
                }
                else
                {
                    p2 = breakSelections[i].GetAsOneRange().OwnerParagraph;
                }
                //Extract contents from temp.docx
                body = new TextBodyPart(main);
                extractContentBetweenPargraphs(temp, body, p1, p2);
                //Find the target position in main.docx for inserting the contents
                mainParSelection = main.FindString(str, false, true);
                mainPara = mainParSelection.GetAsOneRange().OwnerParagraph;
                //Insert a bookmark in the target paragraph
                BookmarkStart bookmarkStart = new BookmarkStart(main, str);
                BookmarkEnd bookmarkEnd = new BookmarkEnd(main, str);
                mainPara.ChildObjects.Insert(0, bookmarkStart);
                mainPara.ChildObjects.Add(bookmarkEnd);
                //
                BookmarksNavigator navigator = new BookmarksNavigator(main);
                navigator.MoveToBookmark(str);
                //Insert the extracted contents into main.docx
                //1. Keep format of temp.docx
                //navigator.ReplaceBookmarkContent(body, true);

                //2. Merge format
                navigator.ReplaceBookmarkContent(body, false);
                //Remove the temporary bookmark
                mainPara.ChildObjects.Remove(bookmarkStart);
                mainPara.ChildObjects.Remove(bookmarkEnd);

                i++;
            }
            //Save file
            main.SaveToFile("output.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("output.docx");
        }
        public static void extractContentBetweenPargraphs(Document doc, TextBodyPart body,Paragraph p1, Paragraph p2)
        {
           
            bool flag = false;
            foreach (Section section in doc.Sections)
            {
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    if (obj == p2)
                    {
                        break;
                    }
                    if (flag)
                    {
                        body.BodyItems.Add(obj.Clone()) ;
                    }
                    if (obj == p1)
                    {
                        flag = true;
                    }
                }
            }
        }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Mon May 30, 2022 2:10 pm

Hi Andy,

Thank you very much for your reply. It's incredible how talented Spire.Doc is. Component definitely deserves all the praise.

I wish you continued success. Kind regards,

Jim.

jimjerry
 
Posts: 2
Joined: Sun May 29, 2022 7:24 am

Tue May 31, 2022 1:43 am

Hi Jim,

Thank you for your endorsement of Spire.Doc.
We wish you all the best too.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc

cron