Replace Word text with document, picture and table

Posted by Chris-the dude on Fri, 06 Dec 2019 10:48:54 +0100

When editing a document, you can quickly change some characters or paragraphs that need to be modified by finding and replacing them. In this article, you will learn how to replace a specified text string in a document with a Word document, picture, or table. The key points of the example are as follows:

  1. Replace text in Word with a document
  2. Replace text in Word with pictures
  3. Replace text in Word with a table

tool

  • Free Spire.Doc for .NET
    After downloading and installing, pay attention to adding the reference Spire.Doc.dll in the program (as shown below). The DLL file can be obtained in the Bin folder under the installation path.

    C code example

    [example 1] replace text in Word with document

    Test documentation:

using Spire.Doc;
using Spire.Doc.Interface;

namespace ReplaceTextWithDocument_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load source document
            Document document = new Document("Original.docx");

            //Load document for replacement
            IDocument replaceDocument = new Document("test.docx");

            //Replace the specified text in the source document with a document
            document.Replace("History", replaceDocument, false, true);

            //Save document
            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

Replacement result:

[example 2] replace text in Word with pictures

Test documentation:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace ReplaceTextWithImg_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate the object of the Document class and load the test Document
            Document doc = new Document();
            doc.LoadFromFile("testfile.docx");
            //Load replaced pictures
            Image image = Image.FromFile("g.png");

            //Get the first section
            Section sec= doc.Sections[0];

            //Find the specified text content in the document
            TextSelection[] selections = doc.FindAllString("Google", true, true);
            int index = 0;
            TextRange range = null;

            //Traverse document, remove text content, insert picture
            foreach (TextSelection selection in selections)
            {
                DocPicture pic = new DocPicture(doc);
                pic.LoadImage(image);
                range = selection.GetAsOneRange();
                index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                range.OwnerParagraph.ChildObjects.Insert(index, pic);
                range.OwnerParagraph.ChildObjects.Remove(range);
            }

            //Save document
            doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

Replacement result:

[example 3] replace the text in Word with a table

Test documentation:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace ReplaceTextWithTable_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate the object of the Document class and load the test Document
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //Find key string text
            Section section = doc.Sections[0];
            TextSelection selection = doc.FindString("Reference appendix", true, true);

            //Get the paragraph of the key string
            TextRange range = selection.GetAsOneRange();
            Paragraph paragraph = range.OwnerParagraph;
            Body body = paragraph.OwnerTextBody;
            int index = body.ChildObjects.IndexOf(paragraph);

            //Add a table with two rows and three columns
            Table table = section.AddTable(true);
            table.ResetCells(2, 3);
            range = table[0, 0].AddParagraph().AppendText("Tube number ( McFarland)");
            range = table[0, 1].AddParagraph().AppendText("0.5");
            range = table[0, 2].AddParagraph().AppendText("1");
            range = table[1, 0].AddParagraph().AppendText("0.25%BaCl2(ml)");
            range = table[1, 1].AddParagraph().AppendText("0.2");
            range = table[1, 2].AddParagraph().AppendText("0.4");

            //Remove paragraph, insert table 
            body.ChildObjects.Remove(paragraph);
            body.ChildObjects.Insert(index, table);

            //Save document
            doc.SaveToFile("result.doc", FileFormat.Doc);
            System.Diagnostics.Process.Start("result.doc");

        }
    }
}

Replacement result:

The above is all about "C ා replacing text strings in Word with documents, pictures and tables".
(end of this paper)

Topics: ASP.NET Google