Spire.Doc
Thursday, 07 August 2014 03:54

Replace Text in Word with Table in C#

Written by support iceblue

This topic is just another request from one of our users on Spire.Doc Forum. In order to let more people know about this function, we’re going to present the whole procedure through a sample demo in the article. Additionally, we would like to remind you that we offer free customized demo for both pay users and test users.

As a professional .NET Word component, Spire.Doc enables developers to replace specified paragraph with a newly created table or an existing table. In this example, the paragraph 3 in main body of the sample word file will be replaced by a newly-built table.

Test file:

Replace Text in Word with Table in C#

Code snippets for replacing text with table:

Step 1: Create a new word document and load the test file.

Document doc = new Document();
doc.LoadFromFile(@"..\..\test.docx");

Step 2: Return TextSection by finding the key text string "classical antiquity science".

Section section = doc.Sections[0];     
TextSelection selection = doc.FindString("classical antiquity science", true, true);

Step 3: Return TextRange from TextSection, then get OwnerParagraph through TextRange.

TextRange range = selection.GetAsOneRange();
Paragraph paragraph = range.OwnerParagraph;

Step 4: Return the zero-based index of the specified paragraph.

Body body = paragraph.OwnerTextBody;
int index = body.ChildObjects.IndexOf(paragraph);

Step 5: Create a new table.

Table table = section.AddTable(true);
table.ResetCells(3, 3);

Step 6: Remove the paragraph and insert table into the collection at the specified index.

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

Step 7: Save and launch the file.

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

Result:

Replace Text in Word with Table in C#

Full C# code:

         static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile(@"..\..\test.docx");
            Section section = doc.Sections[0];
            TextSelection selection = doc.FindString("classical antiquity science", true, true);
            TextRange range = selection.GetAsOneRange();
            Paragraph paragraph = range.OwnerParagraph;
            Body body = paragraph.OwnerTextBody;
            int index = body.ChildObjects.IndexOf(paragraph);
            
            Table table = section.AddTable(true);
            table.ResetCells(3, 3);
            body.ChildObjects.Remove(paragraph);
            body.ChildObjects.Insert(index, table);

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

        }

Word bookmarks are widely used for point out a specified location or give brief information of the paragraph. If you add an image into the bookmark position, the bookmarks will be more obviously and clearly. This article will show you how to insert an image at bookmark position in C# with the help of Spire.Doc.

Spire.Doc offers an instance of BookmarksNavigator to find the bookmarks, and then developers use AppendPicture to add an image. Here comes to the steps:

Step 1: Load a word documents with bookmarks.

Document document = new Document();
document.LoadFromFile("Test.docx");

Step 2: Create an instance of BookmarksNavigator and find the bookmark where you want to insert an image.

//Create an instance of BookmarksNavigator
BookmarksNavigator bn = new BookmarksNavigator(document);
//Find a bookmark and its name is Spire
bn.MoveToBookmark("Spire", true, true);

Step 3: Insert an image at the position of bookmarks you found.

//Add a section and named it section0
Section section0 = document.AddSection();
//Add a paragraph for section0
Paragraph paragraph = section0.AddParagraph();
Image image = Image.FromFile("step.png");
//Add a picture into paragraph
DocPicture picture = paragraph.AppendPicture(image);
//Add a paragraph with picture at the position of bookmark
bn.InsertParagraph(paragraph);
document.Sections.Remove(section0);

Step 4: Save the new document and process it.

string output = "sample3.docx";
document.SaveToFile(output, FileFormat.Docx);
System.Diagnostics.Process.Start(output);

Spire.Doc also offers the following properties to set the image position based on developers' requirements.

picture.TextWrappingStyle
picture.HorizontalAlignment
picture.HorizontalOrigin
picture.HorizontalPosition
picture.VerticalAlignment
picture.VerticalOrigin
picture.VerticalPosition

Effective screenshot:

Insert an image at bookmark in word documents

Full codes:

        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("Test.docx");
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("Spire", true, true);
            Section section0 = document.AddSection();
            Paragraph paragraph = section0.AddParagraph();
            Image image = Image.FromFile("step.png");
            DocPicture picture = paragraph.AppendPicture(image);
            bn.InsertParagraph(paragraph);
            document.Sections.Remove(section0);
            string output = "sample.docx";
            document.SaveToFile(output, FileFormat.Docx);
            System.Diagnostics.Process.Start(output);

        }

Bookmarks give convenience when users want go to specified location and it is clearly to know the contents brief information. Spire.Doc for .NET has a powerful function of operating the word elements of bookmarks. Developers can add bookmarks, Edit/replace bookmarks and remove bookmarks in word documents. Now Spire.Doc starts to support preserve bookmarks in DOCX to PDF conversion. This article will show you how to preserve bookmarks in C# when converting word document into PDF file format.

Download and install Spire.Doc for .NET (Version 5.2.20 or above) and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to finding hyperlinks in C#.

Step 1: Load a word documents with bookmarks.

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

Step 2: Create an instance of ToPdfParameterList

ToPdfParameterList toPdf = new ToPdfParameterList();

Step 3: Set CreateWordBookmarks to true to use word bookmarks when create the bookmarks.

toPdf.CreateWordBookmarks = true;

Step 4: Save the PDF file.

doc.SaveToFile("test.Pdf",toPdf);

Effective screenshot of preserve the bookmarks in result PDF page:

Preserve bookmarks in PDF from word

Full codes:

[C#]
Document doc = new Document();
doc.LoadFromFile("test.docx", FileFormat.Docx);
ToPdfParameterList toPdf = new ToPdfParameterList();
toPdf.CreateWordBookmarks = true;
doc.SaveToFile("test.Pdf", toPdf);
System.Diagnostics.Process.Start("test.Pdf");
Page 1 of 24