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.

Mon Jan 24, 2011 9:51 pm

How can I replace text in word document with multiple paragraphs?

fangm001
 
Posts: 1
Joined: Wed Jan 05, 2011 4:10 pm

Tue Jan 25, 2011 5:13 am

Hi,
please try:
Code: Select all
using System;
using System.Collections.Generic;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace TextReplaceWithMultipleParagraph
{
    class TextRangeLocation : IComparable<TextRangeLocation>
    {
        public TextRangeLocation(TextRange text)
        {
            this.Text = text;
        }

        public TextRange Text { get; set; }

        public Paragraph Owner
        {
            get
            {
                return this.Text.OwnerParagraph;
            }
        }

        public int Index
        {
            get
            {
                return this.Owner.ChildObjects.IndexOf(this.Text);
            }
        }

        public int CompareTo(TextRangeLocation other)
        {
            return -(this.Index - other.Index);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document(@"..\..\Summary_of_Science.doc");

            //document.Replace("science", tempDoc, false, true);
            IList<Paragraph> replacement = new List<Paragraph>();
            Paragraph p1 = new Paragraph(document);
            p1.AppendText("P1");
            Paragraph p2 = new Paragraph(document);
            p2.AppendText("P2");
            Paragraph p3 = new Paragraph(document);
            p3.AppendText("P3");
            replacement.Add(p1);
            replacement.Add(p2);
            replacement.Add(p3);

            TextSelection[] selections = document.FindAllString("science", false, true);
            List<TextRangeLocation> locations = new List<TextRangeLocation>();
            foreach (TextSelection selection in selections)
            {
                locations.Add(new TextRangeLocation(selection.GetAsOneRange()));
            }
            locations.Sort();
            foreach (TextRangeLocation location in locations)
            {
                Replace(location, replacement);
            }
           
            document.SaveToFile("test.doc");
            System.Diagnostics.Process.Start("test.doc");
        }

        private static void Replace(TextRangeLocation location, IList<Paragraph> replacement)
        {
            //will be replaced
            TextRange textRange = location.Text;

            //textRange index
            int index = location.Index;

            //owener paragraph
            Paragraph paragraph = location.Owner;

            //owner text body
            Body sectionBody = paragraph.OwnerTextBody;

            //get the index of paragraph in section
            int paragraphIndex = sectionBody.ChildObjects.IndexOf(paragraph);           

            int replacementIndex = -1;
            if (index == 0)
            {
                //remove
                paragraph.ChildObjects.RemoveAt(0);

                replacementIndex = sectionBody.ChildObjects.IndexOf(paragraph);
            }
            else if (index == paragraph.ChildObjects.Count - 1)
            {
                paragraph.ChildObjects.RemoveAt(index);
                replacementIndex = paragraphIndex + 1;
            }
            else
            {

                //split owner paragraph
                Paragraph paragraph1 = (Paragraph)paragraph.Clone();
                while (paragraph.ChildObjects.Count > index)
                {
                    paragraph.ChildObjects.RemoveAt(index);
                }
                for (int i = 0, count = index + 1; i < count; i++)
                {
                    paragraph1.ChildObjects.RemoveAt(0);
                }
                sectionBody.ChildObjects.Insert(paragraphIndex + 1, paragraph1);

                replacementIndex = paragraphIndex + 1;
            }

            //insert replacement
            for (int i = 0; i < replacement.Count; i++)
            {
                sectionBody.ChildObjects.Insert(replacementIndex + i, replacement[i].Clone());
            }
        }

    }
}

a full demo is attached.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Return to Spire.Doc

cron