├── .github
└── workflows
│ └── gitleaks.yaml
├── Find-and-highlight-text
├── Console-App-.NET-Core
│ ├── Find-and-highlight-text.sln
│ └── Find-and-highlight-text
│ │ ├── Find-and-highlight-text.csproj
│ │ ├── Program.cs
│ │ └── Template.docx
└── Images
│ └── Find-and-highlight-text.png
├── Find-and-replace-several-paragraphs
├── Console-App-.NET-Core
│ ├── Find-and-replace-several-paragraphs.sln
│ └── Find-and-replace-several-paragraphs
│ │ ├── Find-and-replace-several-paragraphs.csproj
│ │ ├── Program.cs
│ │ ├── Source.docx
│ │ └── Template.docx
└── Images
│ └── Replace-several-paragraphs.png
├── Find-and-replace-text
├── Console-App-.NET-Core
│ ├── Replace-misspelt-word.sln
│ └── Replace-misspelt-word
│ │ ├── Program.cs
│ │ ├── Replace-misspelt-word.csproj
│ │ └── Template.docx
└── Images
│ └── Replace-misspelt-word.png
├── README.md
├── Replace-text-with-document
├── Console-App-.NET-Core
│ ├── Replace-text-with-document.sln
│ └── Replace-text-with-document
│ │ ├── Introduction.docx
│ │ ├── Panda Facts.docx
│ │ ├── Panda Mysteries.docx
│ │ ├── Program.cs
│ │ ├── Replace-text-with-document.csproj
│ │ └── Template.docx
└── Images
│ └── Replace-text-with-document.png
├── Replace-text-with-image
├── Console-App-.NET-Core
│ ├── Replace-text-with-image.sln
│ └── Replace-text-with-image
│ │ ├── Mountain-200.png
│ │ ├── Mountain-300.png
│ │ ├── Program.cs
│ │ ├── Replace-text-with-image.csproj
│ │ ├── Road-150.png
│ │ └── Template.docx
└── Images
│ └── Replace-text-with-image.png
├── Replace-text-with-merge-field
├── Console-App-.NET-Core
│ ├── Replace-text-with-merge-field.sln
│ └── Replace-text-with-merge-field
│ │ ├── Program.cs
│ │ ├── Replace-text-with-merge-field.csproj
│ │ └── Template.docx
└── Images
│ └── Replace-text-with-merge-field.png
└── Replace-text-with-table
├── Console-App-.NET-Core
├── Replace-text-with-table.sln
└── Replace-text-with-table
│ ├── Program.cs
│ ├── Replace-text-with-table.csproj
│ ├── Suppliers.xml
│ └── Template.docx
└── Images
└── Replace-text-with-table.png
/.github/workflows/gitleaks.yaml:
--------------------------------------------------------------------------------
1 | name: Secret Value found!!
2 | on:
3 | push:
4 | public:
5 | jobs:
6 | scan:
7 | name: gitleaks
8 | runs-on: ubuntu-latest
9 | steps:
10 | - name: Checkout
11 | uses: actions/checkout@v3
12 | - name: Install the gitleaks
13 | run: wget https://github.com/zricethezav/gitleaks/releases/download/v8.15.2/gitleaks_8.15.2_linux_x64.tar.gz
14 | shell: pwsh
15 | - name: Extract the tar file
16 | run: tar xzvf gitleaks_8.15.2_linux_x64.tar.gz
17 | - name: Generate the report
18 | id: gitleaks
19 | run: $GITHUB_WORKSPACE/gitleaks detect -s $GITHUB_WORKSPACE -f json -r $GITHUB_WORKSPACE/leaksreport.json
20 | shell: bash
21 | continue-on-error: true
22 | - name: Setup NuGet.exe
23 | if: steps.gitleaks.outcome != 'success'
24 | uses: nuget/setup-nuget@v1
25 | with:
26 | nuget-version: latest
27 | - name: Install the dotnet
28 | if: steps.gitleaks.outcome != 'success'
29 | uses: actions/setup-dotnet@v3
30 | with:
31 | dotnet-version: '3.1.x'
32 | - name: Install the report tool packages
33 | if: steps.gitleaks.outcome != 'success'
34 | run: |
35 | nuget install "Syncfusion.Email" -source ${{ secrets.NexusFeedLink }} -ExcludeVersion
36 | dir $GITHUB_WORKSPACE/Syncfusion.Email/lib/netcoreapp3.1
37 | dotnet $GITHUB_WORKSPACE/Syncfusion.Email/lib/netcoreapp3.1/GitleaksReportMail.dll ${{ secrets.CITEAMCREDENTIALS }} "$GITHUB_REF_NAME" ${{ secrets.NETWORKCREDENTIALS }} ${{ secrets.NETWORKKEY }} "$GITHUB_WORKSPACE" ${{ secrets.ORGANIZATIONNAME }}
38 | exit 1
--------------------------------------------------------------------------------
/Find-and-highlight-text/Console-App-.NET-Core/Find-and-highlight-text.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2000
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-and-highlight-text", "Find-and-highlight-text\Find-and-highlight-text.csproj", "{2EA43EB8-ED84-4955-A4B2-2FB81775B7E0}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {2EA43EB8-ED84-4955-A4B2-2FB81775B7E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {2EA43EB8-ED84-4955-A4B2-2FB81775B7E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {2EA43EB8-ED84-4955-A4B2-2FB81775B7E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {2EA43EB8-ED84-4955-A4B2-2FB81775B7E0}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {31E53056-19DA-4252-88BD-E686ABA7DCBC}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Find-and-highlight-text/Console-App-.NET-Core/Find-and-highlight-text/Find-and-highlight-text.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Find-and-highlight-text/Console-App-.NET-Core/Find-and-highlight-text/Program.cs:
--------------------------------------------------------------------------------
1 | using Syncfusion.DocIO;
2 | using Syncfusion.DocIO.DLS;
3 | using Syncfusion.Drawing;
4 | using System;
5 | using System.IO;
6 | using System.Text.RegularExpressions;
7 |
8 | namespace Find_and_highlight_text
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | //Creates new Word document instance for Word processing
15 | using (WordDocument document = new WordDocument())
16 | {
17 | //Opens the input Word document
18 | Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
19 | document.Open(docStream, FormatType.Docx);
20 | docStream.Dispose();
21 | //Finds all occurrence of the text in the Word document.
22 | TextSelection[] textSelections = document.FindAll("Adventure", true, true);
23 | for (int i = 0; i < textSelections.Length; i++)
24 | {
25 | //Sets the highlight color for the searched text as Yellow.
26 | textSelections[i].GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
27 | }
28 | //Saves the resultant file in the given path
29 | docStream = File.Create(Path.GetFullPath(@"Result.docx"));
30 | document.Save(docStream, FormatType.Docx);
31 | docStream.Dispose();
32 | }
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Find-and-highlight-text/Console-App-.NET-Core/Find-and-highlight-text/Template.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Find-and-highlight-text/Console-App-.NET-Core/Find-and-highlight-text/Template.docx
--------------------------------------------------------------------------------
/Find-and-highlight-text/Images/Find-and-highlight-text.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Find-and-highlight-text/Images/Find-and-highlight-text.png
--------------------------------------------------------------------------------
/Find-and-replace-several-paragraphs/Console-App-.NET-Core/Find-and-replace-several-paragraphs.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2000
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-and-replace-several-paragraphs", "Find-and-replace-several-paragraphs\Find-and-replace-several-paragraphs.csproj", "{141DEA2F-99C0-4A99-BDF8-DA65EA7AC77C}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {141DEA2F-99C0-4A99-BDF8-DA65EA7AC77C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {141DEA2F-99C0-4A99-BDF8-DA65EA7AC77C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {141DEA2F-99C0-4A99-BDF8-DA65EA7AC77C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {141DEA2F-99C0-4A99-BDF8-DA65EA7AC77C}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {EAAFB339-C64E-4B70-88E2-BACEB6706A22}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Find-and-replace-several-paragraphs/Console-App-.NET-Core/Find-and-replace-several-paragraphs/Find-and-replace-several-paragraphs.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Find-and-replace-several-paragraphs/Console-App-.NET-Core/Find-and-replace-several-paragraphs/Program.cs:
--------------------------------------------------------------------------------
1 | using Syncfusion.DocIO;
2 | using Syncfusion.DocIO.DLS;
3 | using System;
4 | using System.IO;
5 |
6 | namespace Find_and_replace_several_paragraphs
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | //Creates new Word document instance for Word processing
13 | using (WordDocument document = new WordDocument())
14 | {
15 | //Opens the input Word document
16 | Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
17 | document.Open(docStream, FormatType.Docx);
18 | docStream.Dispose();
19 | docStream = File.OpenRead(Path.GetFullPath(@"../../../Source.docx"));
20 | WordDocument subDocument = new WordDocument(docStream, FormatType.Docx);
21 | docStream.Dispose();
22 | //Gets the content from another Word document to replace
23 | TextBodyPart replacePart = new TextBodyPart(subDocument);
24 | foreach (TextBodyItem bodyItem in subDocument.LastSection.Body.ChildEntities)
25 | {
26 | replacePart.BodyItems.Add(bodyItem.Clone());
27 | }
28 | string placeholderText = "Suppliers/Vendors of Northwind" + "Customers of Northwind"
29 | + "Employee details of Northwind traders" + "The product information"
30 | + "The inventory details" + "The shippers" + "Purchase Order transactions"
31 | + "Sales Order transaction" + "Inventory transactions" + "Invoices" + "[end replace]";
32 | //Finds the text that extends to several paragraphs and replaces it with desired content.
33 | document.ReplaceSingleLine(placeholderText, replacePart, false, false);
34 | subDocument.Dispose();
35 | //Saves the resultant file in the given path
36 | docStream = File.Create(Path.GetFullPath(@"Result.docx"));
37 | document.Save(docStream, FormatType.Docx);
38 | docStream.Dispose();
39 | }
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Find-and-replace-several-paragraphs/Console-App-.NET-Core/Find-and-replace-several-paragraphs/Source.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Find-and-replace-several-paragraphs/Console-App-.NET-Core/Find-and-replace-several-paragraphs/Source.docx
--------------------------------------------------------------------------------
/Find-and-replace-several-paragraphs/Console-App-.NET-Core/Find-and-replace-several-paragraphs/Template.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Find-and-replace-several-paragraphs/Console-App-.NET-Core/Find-and-replace-several-paragraphs/Template.docx
--------------------------------------------------------------------------------
/Find-and-replace-several-paragraphs/Images/Replace-several-paragraphs.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Find-and-replace-several-paragraphs/Images/Replace-several-paragraphs.png
--------------------------------------------------------------------------------
/Find-and-replace-text/Console-App-.NET-Core/Replace-misspelt-word.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2000
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-misspelt-word", "Replace-misspelt-word\Replace-misspelt-word.csproj", "{33B8C0CE-26D5-4BA8-B8D0-C6D2238E1B76}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {33B8C0CE-26D5-4BA8-B8D0-C6D2238E1B76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {33B8C0CE-26D5-4BA8-B8D0-C6D2238E1B76}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {33B8C0CE-26D5-4BA8-B8D0-C6D2238E1B76}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {33B8C0CE-26D5-4BA8-B8D0-C6D2238E1B76}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {041BB1EF-E0CC-4E59-89D9-85C45B21C89C}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Find-and-replace-text/Console-App-.NET-Core/Replace-misspelt-word/Program.cs:
--------------------------------------------------------------------------------
1 | using Syncfusion.DocIO;
2 | using Syncfusion.DocIO.DLS;
3 | using System;
4 | using System.IO;
5 |
6 | namespace Find_and_replace_text
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | //Creates new Word document instance for Word processing
13 | using (WordDocument document = new WordDocument())
14 | {
15 | //Opens the input Word document
16 | Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
17 | document.Open(docStream, FormatType.Docx);
18 | docStream.Dispose();
19 | //Finds all the occurrence of a misspelt word and replaces with desired word in the Word document.
20 | document.Replace("Cyles", "Cycles", true, true);
21 | //Saves the resultant file in the given path
22 | docStream = File.Create(Path.GetFullPath(@"Result.docx"));
23 | document.Save(docStream, FormatType.Docx);
24 | docStream.Dispose();
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Find-and-replace-text/Console-App-.NET-Core/Replace-misspelt-word/Replace-misspelt-word.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Find-and-replace-text/Console-App-.NET-Core/Replace-misspelt-word/Template.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Find-and-replace-text/Console-App-.NET-Core/Replace-misspelt-word/Template.docx
--------------------------------------------------------------------------------
/Find-and-replace-text/Images/Replace-misspelt-word.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Find-and-replace-text/Images/Replace-misspelt-word.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Word Find and Replace examples
2 |
3 | This repository contains examples that illustrates how to find and replace text in Word documents programmatically in C# and VB.NET using Syncfusion® [.NET Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) (DocIO) without Microsoft Word or Office interop dependencies.
4 |
5 | ## Find and replace text in Word documents using C#
6 | The Syncfusion® [.NET Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) (DocIO) provides comprehensive APIs to find and replace text in a Word document with any desired text, image, hyperlink, paragraph, table, and part of a document or an entire document. It provides options to find text by matching case and whole words. You can find each occurrence one by one or all the occurrences of a text in the document. It saves your efforts and time by helping you to automate find and replace a pattern of text in a Word document using [Regex](https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). It allows you to save the resultant document as Word document (DOCX, WordML), PDF, image, HTML and more.
7 |
8 | ## Key Features
9 |
10 | - [Find and highlight text in C#](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#find-text-in-a-word-document-and-format) - Find text from a Word document and format or highlight it.
11 | - [Find and replace text in C#](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#find-and-replace-text-with-other-text) - Find text from a Word document and replace it with another text.
12 | - [Replace text with image in C#](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#find-and-replace-text-with-an-image) - Find a placeholder text from a Word document and replace it with any desired image.
13 | - [Replace text with merge field in C#](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#find-and-replace-a-pattern-of-text-with-a-merge-field) - Find a pattern of text from a Word document and replace it with merge fields.
14 | - [Replace text with table in C#](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#find-and-replace-text-with-a-table) - Find a placeholder text from a Word document and replace it with a table.
15 | - [Replace text with document in C#](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#find-and-replace-text-in-word-document-with-another-document) - Find text from a Word document and replace it with another Word document.
16 | - [Find and replace several paragraphs in C#](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#find-and-replace-text-extending-to-several-paragraphs) - Find text that extends to several paragraphs in a Word document and replace it with another Word document.
17 |
18 | ## Screenshots
19 |
20 | **Find and highlight text**
21 |
22 |
23 |
24 |
25 |
26 | **Find and replace text**
27 |
28 |
29 |
30 |
31 |
32 | **Replace text with table**
33 |
34 |
35 |
36 |
37 |
38 | ## Syncfusion® .NET Word Library
39 | The Syncfusion® DocIO is a [.NET Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) allows you to add advanced Word document processing functionalities to any .NET application and does not require Microsoft Word application to be installed in the machine. It is a non-UI component that provides a full-fledged document instance model similar to the Microsoft Office COM libraries to iterate with the document elements explicitly and perform necessary manipulation.
40 |
41 | Take a moment to peruse the [documentation](https://help.syncfusion.com/file-formats/docio/getting-started?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), where you can find basic Word document processing options along with the features like [mail merge](https://help.syncfusion.com/file-formats/docio/working-with-mail-merge?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [merge](https://help.syncfusion.com/file-formats/docio/word-document/merging-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [split](https://help.syncfusion.com/file-formats/docio/word-document/split-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [compare documents](https://help.syncfusion.com/file-formats/docio/word-document/compare-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [find and replace](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) text in the Word document, [protect](https://help.syncfusion.com/file-formats/docio/working-with-security?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) the Word documents, and most importantly, the [PDF](https://help.syncfusion.com/file-formats/docio/word-to-pdf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [Image](https://help.syncfusion.com/file-formats/docio/word-to-image?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) conversions with code examples.
42 |
43 | Compatible Microsoft Word Versions
44 | ----------------------------------
45 |
46 | * Microsoft Word 97-2003
47 | * Microsoft Word 2007
48 | * Microsoft Word 2010
49 | * Microsoft Word 2013
50 | * Microsoft Word 2016
51 | * Microsoft Word 2019
52 | * Microsoft 365
53 |
54 | Supported File Formats
55 | ----------------------
56 |
57 | * Creates, reads, and edits popular text file formats like [DOC](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#doc-to-docx-and-docx-to-doc), DOT, [DOCM](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#macros), DOTM, [DOCX](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#doc-to-docx-and-docx-to-doc), [DOTX](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#templates), [HTML](https://help.syncfusion.com/file-formats/docio/html?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [RTF](https://help.syncfusion.com/file-formats/docio/rtf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [TXT](https://help.syncfusion.com/file-formats/docio/text?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [XML (WordML)](https://help.syncfusion.com/file-formats/docio/word-file-formats#word-processing-xml-xml?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
58 | * Converts Word documents also to [PDF](https://help.syncfusion.com/file-formats/docio/word-to-pdf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Image](https://help.syncfusion.com/file-formats/docio/word-to-image?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [ODT](https://help.syncfusion.com/file-formats/docio/word-to-odt?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) files.
59 |
60 | ## How to run the examples
61 | - Download this project to a location in your disk.
62 | - Open the solution file using Visual Studio.
63 | - Rebuild the solution to install the required NuGet packages.
64 | - Run the application.
65 |
66 | ## Resources
67 |
68 | - **Product page:** [Syncfusion® Word Framework](https://www.syncfusion.com/document-processing/word-framework/net?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
69 | - **Documentation:** [Find and Replace in Word document](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
70 | - **GitHub Examples:** [Syncfusion® Word library examples](https://github.com/SyncfusionExamples/DocIO-Examples?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
71 | - **Online demo:** [Syncfusion® Word library - Online demos](https://ej2.syncfusion.com/aspnetcore/Word/SalesInvoice#/material3?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
72 |
73 | ## Support and feedback
74 | - For any other queries, reach our [Syncfusion® support team](https://support.syncfusion.com/agent/tickets/create?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or post the queries through the [community forums](https://www.syncfusion.com/forums?utm_source=github&utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
75 | - Request new feature through [Syncfusion® feedback portal](https://www.syncfusion.com/feedback/home?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
76 |
77 | ## License
78 | This is a commercial product and requires a paid license for possession or use. Syncfusion's licensed software, including this component, is subject to the terms and conditions of [Syncfusion's EULA](https://www.syncfusion.com/license/studio/22.2.5/syncfusion_essential_studio_eula.pdf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). You can purchase a licnense [here](https://www.syncfusion.com/sales/products?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or start a free 30-day trial [here](https://www.syncfusion.com/account/manage-trials/start-trials?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
79 |
--------------------------------------------------------------------------------
/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2000
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-document", "Replace-text-with-document\Replace-text-with-document.csproj", "{080721FC-4F7C-4409-9750-7AC97CB53AA8}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {080721FC-4F7C-4409-9750-7AC97CB53AA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {080721FC-4F7C-4409-9750-7AC97CB53AA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {080721FC-4F7C-4409-9750-7AC97CB53AA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {080721FC-4F7C-4409-9750-7AC97CB53AA8}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {55701B0C-BFC5-49E4-824C-9316C67AFF96}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Introduction.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Introduction.docx
--------------------------------------------------------------------------------
/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Panda Facts.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Panda Facts.docx
--------------------------------------------------------------------------------
/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Panda Mysteries.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Panda Mysteries.docx
--------------------------------------------------------------------------------
/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Program.cs:
--------------------------------------------------------------------------------
1 | using Syncfusion.DocIO;
2 | using Syncfusion.DocIO.DLS;
3 | using System;
4 | using System.IO;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace Replace_text_with_document
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | //Creates new Word document instance for Word processing
14 | using (WordDocument document = new WordDocument())
15 | {
16 | //Opens the input Word document
17 | Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
18 | document.Open(docStream, FormatType.Docx);
19 | docStream.Dispose();
20 | //Finds all the content placeholder text in the Word document.
21 | TextSelection[] textSelections = document.FindAll(new Regex(@"\[(.*)\]"));
22 | for (int i = 0; i < textSelections.Length; i++)
23 | {
24 | //Replaces the content placeholder text with desired Word document.
25 | docStream = File.OpenRead(Path.GetFullPath(@"../../../" + textSelections[i].SelectedText.TrimStart('[').TrimEnd(']') + ".docx"));
26 | WordDocument subDocument = new WordDocument(docStream, FormatType.Docx);
27 | docStream.Dispose();
28 | document.Replace(textSelections[i].SelectedText, subDocument, true, true);
29 | subDocument.Dispose();
30 | }
31 | //Saves the resultant file in the given path
32 | docStream = File.Create(Path.GetFullPath(@"Result.docx"));
33 | document.Save(docStream, FormatType.Docx);
34 | docStream.Dispose();
35 | }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Replace-text-with-document.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Template.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-document/Console-App-.NET-Core/Replace-text-with-document/Template.docx
--------------------------------------------------------------------------------
/Replace-text-with-document/Images/Replace-text-with-document.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-document/Images/Replace-text-with-document.png
--------------------------------------------------------------------------------
/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2000
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-image", "Replace-text-with-image\Replace-text-with-image.csproj", "{D6B8FDF6-358D-4146-B065-86556AC4336A}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {D6B8FDF6-358D-4146-B065-86556AC4336A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {D6B8FDF6-358D-4146-B065-86556AC4336A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {D6B8FDF6-358D-4146-B065-86556AC4336A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {D6B8FDF6-358D-4146-B065-86556AC4336A}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {6D38ABDC-E1CE-4E51-BD5A-0C0AA890FD87}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Mountain-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Mountain-200.png
--------------------------------------------------------------------------------
/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Mountain-300.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Mountain-300.png
--------------------------------------------------------------------------------
/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Program.cs:
--------------------------------------------------------------------------------
1 | using Syncfusion.DocIO;
2 | using Syncfusion.DocIO.DLS;
3 | using System;
4 | using System.IO;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace Find_and_replace_text
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | //Creates new Word document instance for Word processing
14 | using (WordDocument document = new WordDocument())
15 | {
16 | //Opens the input Word document
17 | Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
18 | document.Open(docStream, FormatType.Docx);
19 | docStream.Dispose();
20 | //Finds all the image placeholder text in the Word document.
21 | TextSelection[] textSelections = document.FindAll(new Regex("^//(.*)"));
22 | for (int i = 0; i < textSelections.Length; i++)
23 | {
24 | //Replaces the image placeholder text with desired image.
25 | Stream imageStream = File.OpenRead(Path.GetFullPath(@"../../../" + textSelections[i].SelectedText + ".png"));
26 | WParagraph paragraph = new WParagraph(document);
27 | WPicture picture = paragraph.AppendPicture(imageStream) as WPicture;
28 | imageStream.Dispose();
29 | TextSelection newSelection = new TextSelection(paragraph, 0, 1);
30 | TextBodyPart bodyPart = new TextBodyPart(document);
31 | bodyPart.BodyItems.Add(paragraph);
32 | document.Replace(textSelections[i].SelectedText, bodyPart, true, true);
33 | }
34 | //Saves the resultant file in the given path
35 | docStream = File.Create(Path.GetFullPath(@"Result.docx"));
36 | document.Save(docStream, FormatType.Docx);
37 | docStream.Dispose();
38 | }
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Replace-text-with-image.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Road-150.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Road-150.png
--------------------------------------------------------------------------------
/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Template.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-image/Console-App-.NET-Core/Replace-text-with-image/Template.docx
--------------------------------------------------------------------------------
/Replace-text-with-image/Images/Replace-text-with-image.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-image/Images/Replace-text-with-image.png
--------------------------------------------------------------------------------
/Replace-text-with-merge-field/Console-App-.NET-Core/Replace-text-with-merge-field.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2000
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-merge-field", "Replace-text-with-merge-field\Replace-text-with-merge-field.csproj", "{76550820-5AD8-47E0-B303-EB3E7E3E0540}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {76550820-5AD8-47E0-B303-EB3E7E3E0540}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {76550820-5AD8-47E0-B303-EB3E7E3E0540}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {76550820-5AD8-47E0-B303-EB3E7E3E0540}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {76550820-5AD8-47E0-B303-EB3E7E3E0540}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {E4CAA026-21A2-4892-B8AF-90CF262B272D}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Replace-text-with-merge-field/Console-App-.NET-Core/Replace-text-with-merge-field/Program.cs:
--------------------------------------------------------------------------------
1 | using Syncfusion.DocIO;
2 | using Syncfusion.DocIO.DLS;
3 | using System;
4 | using System.IO;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace Replace_text_with_merge_field
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | //Creates new Word document instance for Word processing
14 | using (WordDocument document = new WordDocument())
15 | {
16 | //Opens the input Word document
17 | Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
18 | document.Open(docStream, FormatType.Docx);
19 | docStream.Dispose();
20 | //Finds all the placeholder text enclosed within '«' and '»' in the Word document
21 | TextSelection[] textSelections = document.FindAll(new Regex("«([(?i)image(?-i)]*:*[a-zA-Z0-9 ]*:*[a-zA-Z0-9 ]+)»"));
22 | string[] searchedPlaceholders = new string[textSelections.Length];
23 | for (int i = 0; i < textSelections.Length; i++)
24 | {
25 | searchedPlaceholders[i] = textSelections[i].SelectedText;
26 | }
27 | for (int i = 0; i < searchedPlaceholders.Length; i++)
28 | {
29 | //Replaces the placeholder text enclosed within '«' and '»' with desired merge field
30 | WParagraph paragraph = new WParagraph(document);
31 | paragraph.AppendField(searchedPlaceholders[i].TrimStart('«').TrimEnd('»'), FieldType.FieldMergeField);
32 | TextSelection newSelection = new TextSelection(paragraph, 0, paragraph.Items.Count);
33 | TextBodyPart bodyPart = new TextBodyPart(document);
34 | bodyPart.BodyItems.Add(paragraph);
35 | document.Replace(searchedPlaceholders[i], bodyPart, true, true, true);
36 | }
37 | //Saves the resultant file in the given path
38 | docStream = File.Create(Path.GetFullPath(@"Result.docx"));
39 | document.Save(docStream, FormatType.Docx);
40 | docStream.Dispose();
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/Replace-text-with-merge-field/Console-App-.NET-Core/Replace-text-with-merge-field/Replace-text-with-merge-field.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Replace-text-with-merge-field/Console-App-.NET-Core/Replace-text-with-merge-field/Template.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-merge-field/Console-App-.NET-Core/Replace-text-with-merge-field/Template.docx
--------------------------------------------------------------------------------
/Replace-text-with-merge-field/Images/Replace-text-with-merge-field.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-merge-field/Images/Replace-text-with-merge-field.png
--------------------------------------------------------------------------------
/Replace-text-with-table/Console-App-.NET-Core/Replace-text-with-table.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2000
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-table", "Replace-text-with-table\Replace-text-with-table.csproj", "{81F5164B-D978-4DFF-BD33-A26C8BCB0D06}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {81F5164B-D978-4DFF-BD33-A26C8BCB0D06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {81F5164B-D978-4DFF-BD33-A26C8BCB0D06}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {81F5164B-D978-4DFF-BD33-A26C8BCB0D06}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {81F5164B-D978-4DFF-BD33-A26C8BCB0D06}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {F02AF11F-D633-437B-B7FC-F37198D5742F}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Replace-text-with-table/Console-App-.NET-Core/Replace-text-with-table/Program.cs:
--------------------------------------------------------------------------------
1 | using Syncfusion.DocIO;
2 | using Syncfusion.DocIO.DLS;
3 | using System;
4 | using System.IO;
5 | using System.Xml;
6 |
7 | namespace Replace_text_with_table
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | //Creates new Word document instance for Word processing
14 | using (WordDocument document = new WordDocument())
15 | {
16 | //Opens the input Word document
17 | Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
18 | document.Open(docStream, FormatType.Docx);
19 | docStream.Dispose();
20 | //Creates a new table
21 | WTable table = new WTable(document);
22 | table.ResetCells(1, 6);
23 | table[0, 0].Width = 52f;
24 | table[0, 0].AddParagraph().AppendText("Supplier ID");
25 | table[0, 1].Width = 128f;
26 | table[0, 1].AddParagraph().AppendText("Company Name");
27 | table[0, 2].Width = 70f;
28 | table[0, 2].AddParagraph().AppendText("Contact Name");
29 | table[0, 3].Width = 92f;
30 | table[0, 3].AddParagraph().AppendText("Address");
31 | table[0, 4].Width = 66.5f;
32 | table[0, 4].AddParagraph().AppendText("City");
33 | table[0, 5].Width = 56f;
34 | table[0, 5].AddParagraph().AppendText("Country");
35 | //Imports data to the table
36 | ImportDataToTable(table);
37 | //Applies the built-in table style (Medium Shading 1 Accent 1) to the table
38 | table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent1);
39 | //Replaces the table placeholder text with a new table
40 | TextBodyPart bodyPart = new TextBodyPart(document);
41 | bodyPart.BodyItems.Add(table);
42 | document.Replace("[Suppliers table]", bodyPart, true, true, true);
43 | //Saves the resultant file in the given path
44 | docStream = File.Create(Path.GetFullPath(@"Result.docx"));
45 | document.Save(docStream, FormatType.Docx);
46 | docStream.Dispose();
47 | }
48 | }
49 |
50 | ///
51 | /// Imports the data from XML file to the table.
52 | ///
53 | ///
54 | /// reader
55 | /// Unexpected xml tag + reader.LocalName
56 | private static void ImportDataToTable(WTable table)
57 | {
58 | FileStream fs = new FileStream(@"../../../Suppliers.xml", FileMode.Open, FileAccess.Read);
59 | XmlReader reader = XmlReader.Create(fs);
60 | if (reader == null)
61 | throw new Exception("reader");
62 | while (reader.NodeType != XmlNodeType.Element)
63 | reader.Read();
64 | if (reader.LocalName != "SuppliersList")
65 | throw new XmlException("Unexpected xml tag " + reader.LocalName);
66 | reader.Read();
67 | while (reader.NodeType == XmlNodeType.Whitespace)
68 | reader.Read();
69 | while (reader.LocalName != "SuppliersList")
70 | {
71 | if (reader.NodeType == XmlNodeType.Element)
72 | {
73 | switch (reader.LocalName)
74 | {
75 | case "Suppliers":
76 | //Adds new row to the table for importing data from next record.
77 | WTableRow tableRow = table.AddRow(true);
78 | ImportDataToRow(reader, tableRow);
79 | break;
80 | }
81 | }
82 | else
83 | {
84 | reader.Read();
85 | if ((reader.LocalName == "SuppliersList") && reader.NodeType == XmlNodeType.EndElement)
86 | break;
87 | }
88 | }
89 | reader.Dispose();
90 | fs.Dispose();
91 | }
92 | ///
93 | /// Imports the data to the table row.
94 | ///
95 | /// The reader.
96 | ///
97 | /// reader
98 | /// Unexpected xml tag + reader.LocalName
99 | private static void ImportDataToRow(XmlReader reader, WTableRow tableRow)
100 | {
101 | if (reader == null)
102 | throw new Exception("reader");
103 | while (reader.NodeType != XmlNodeType.Element)
104 | reader.Read();
105 | if (reader.LocalName != "Suppliers")
106 | throw new XmlException("Unexpected xml tag " + reader.LocalName);
107 | reader.Read();
108 | while (reader.NodeType == XmlNodeType.Whitespace)
109 | reader.Read();
110 | while (reader.LocalName != "Suppliers")
111 | {
112 | if (reader.NodeType == XmlNodeType.Element)
113 | {
114 | switch (reader.LocalName)
115 | {
116 | case "SupplierID":
117 | tableRow.Cells[0].AddParagraph().AppendText(reader.ReadElementContentAsString());
118 | break;
119 | case "CompanyName":
120 | tableRow.Cells[1].AddParagraph().AppendText(reader.ReadElementContentAsString());
121 | break;
122 | case "ContactName":
123 | tableRow.Cells[2].AddParagraph().AppendText(reader.ReadElementContentAsString());
124 | break;
125 | case "Address":
126 | tableRow.Cells[3].AddParagraph().AppendText(reader.ReadElementContentAsString());
127 | break;
128 | case "City":
129 | tableRow.Cells[4].AddParagraph().AppendText(reader.ReadElementContentAsString());
130 | break;
131 | case "Country":
132 | tableRow.Cells[5].AddParagraph().AppendText(reader.ReadElementContentAsString());
133 | break;
134 | default:
135 | reader.Skip();
136 | break;
137 | }
138 | }
139 | else
140 | {
141 | reader.Read();
142 | if ((reader.LocalName == "Suppliers") && reader.NodeType == XmlNodeType.EndElement)
143 | break;
144 | }
145 | }
146 | }
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/Replace-text-with-table/Console-App-.NET-Core/Replace-text-with-table/Replace-text-with-table.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Replace-text-with-table/Console-App-.NET-Core/Replace-text-with-table/Suppliers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 1
5 | Exotic Liquids
6 | Charlotte Cooper
7 | Purchasing Manager
8 | 49 Gilbert St.
9 | London
10 | EC1 4SD
11 | UK
12 | (171) 555-2222
13 |
14 |
15 | 2
16 | New Orleans Cajun Delights
17 | Shelley Burke
18 | Order Administrator
19 | P.O. Box 78934
20 | New Orleans
21 | LA
22 | 70117
23 | USA
24 | (100) 555-4822
25 | #CAJUN.HTM#
26 |
27 |
28 | 3
29 | Grandma Kelly's Homestead
30 | Regina Murphy
31 | Sales Representative
32 | 707 Oxford Rd.
33 | Ann Arbor
34 | MI
35 | 48104
36 | USA
37 | (313) 555-5735
38 | (313) 555-3349
39 |
40 |
41 | 4
42 | Tokyo Traders
43 | Yoshi Nagase
44 | Marketing Manager
45 | 9-8 Sekimai
46 | Musashino-shi
47 | Tokyo
48 | 100
49 | Japan
50 | (03) 3555-5011
51 |
52 |
53 | 5
54 | Cooperativa de Quesos 'Las Cabras'
55 | Antonio del Valle Saavedra
56 | Export Administrator
57 | Calle del Rosal 4
58 | Oviedo
59 | Asturias
60 | 33007
61 | Spain
62 | (98) 598 76 54
63 |
64 |
65 | 6
66 | Mayumi's
67 | Mayumi Ohno
68 | Marketing Representative
69 | 92 Setsuko
70 | Chuo-ku
71 | Osaka
72 | 545
73 | Japan
74 | (06) 431-7877
75 | Mayumi's (on the World Wide Web)#http://www.microsoft.com/accessdev/sampleapps/mayumi.htm#
76 |
77 |
78 | 7
79 | Pavlova, Ltd.
80 | Ian Devling
81 | Marketing Manager
82 | 74 Rose St.
83 | Moonie Ponds
84 | Melbourne
85 | Victoria
86 | 3058
87 | Australia
88 | (03) 444-2343
89 | (03) 444-6588
90 |
91 |
92 | 8
93 | Specialty Biscuits, Ltd.
94 | Peter Wilson
95 | Sales Representative
96 | 29 King's Way
97 | Manchester
98 | M14 GSD
99 | UK
100 | (161) 555-4448
101 |
102 |
103 | 9
104 | PB Knäckebröd AB
105 | Lars Peterson
106 | Sales Agent
107 | Kaloadagatan 13
108 | Göteborg
109 | S-345 67
110 | Sweden
111 | 031-987 65 43
112 | 031-987 65 91
113 |
114 |
115 | 10
116 | Refrescos Americanas LTDA
117 | Carlos Diaz
118 | Marketing Manager
119 | Av. das Americanas 12.890
120 | São Paulo
121 | 5442
122 | Brazil
123 | (11) 555 4640
124 |
125 |
126 | 11
127 | Heli Süßwaren GmbH & Co. KG
128 | Petra Winkler
129 | Sales Manager
130 | Tiergartenstraße 5
131 | Berlin
132 | 10785
133 | Germany
134 | (010) 9984510
135 |
136 |
137 | 12
138 | Plutzer Lebensmittelgroßmärkte AG
139 | Martin Bein
140 | International Marketing Mgr.
141 | Bogenallee 51
142 | Frankfurt
143 | 60439
144 | Germany
145 | (069) 992755
146 | Plutzer (on the World Wide Web)#http://www.microsoft.com/accessdev/sampleapps/plutzer.htm#
147 |
148 |
149 | 13
150 | Nord-Ost-Fisch Handelsgesellschaft mbH
151 | Sven Petersen
152 | Coordinator Foreign Markets
153 | Frahmredder 112a
154 | Cuxhaven
155 | 27478
156 | Germany
157 | (04721) 8713
158 | (04721) 8714
159 |
160 |
161 | 14
162 | Formaggi Fortini s.r.l.
163 | Elio Rossi
164 | Sales Representative
165 | Viale Dante, 75
166 | Ravenna
167 | 48100
168 | Italy
169 | (0544) 60323
170 | (0544) 60603
171 | #FORMAGGI.HTM#
172 |
173 |
174 | 15
175 | Norske Meierier
176 | Beate Vileid
177 | Marketing Manager
178 | Hatlevegen 5
179 | Sandvika
180 | 1320
181 | Norway
182 | (0)2-953010
183 |
184 |
185 | 16
186 | Bigfoot Breweries
187 | Cheryl Saylor
188 | Regional Account Rep.
189 | 3400 - 8th Avenue
190 | Suite 210
191 | Bend
192 | OR
193 | 97101
194 | USA
195 | (503) 555-9931
196 |
197 |
198 | 17
199 | Svensk Sjöföda AB
200 | Michael Björn
201 | Sales Representative
202 | Brovallavägen 231
203 | Stockholm
204 | S-123 45
205 | Sweden
206 | 08-123 45 67
207 |
208 |
209 | 18
210 | Aux joyeux ecclésiastiques
211 | Guylène Nodier
212 | Sales Manager
213 | 203, Rue des Francs-Bourgeois
214 | Paris
215 | 75004
216 | France
217 | (1) 03.83.00.68
218 | (1) 03.83.00.62
219 |
220 |
221 | 19
222 | New England Seafood Cannery
223 | Robb Merchant
224 | Wholesale Account Agent
225 | Order Processing Dept.
226 | 2100 Paul Revere Blvd.
227 | Boston
228 | MA
229 | 02134
230 | USA
231 | (617) 555-3267
232 | (617) 555-3389
233 |
234 |
235 | 20
236 | Leka Trading
237 | Chandra Leka
238 | Owner
239 | 471 Serangoon Loop, Suite #402
240 | Singapore
241 | 0512
242 | Singapore
243 | 555-8787
244 |
245 |
246 | 21
247 | Lyngbysild
248 | Niels Petersen
249 | Sales Manager
250 | Lyngbysild
251 | Fiskebakken 10
252 | Lyngby
253 | 2800
254 | Denmark
255 | 43844108
256 | 43844115
257 |
258 |
259 | 22
260 | Zaanse Snoepfabriek
261 | Dirk Luchte
262 | Accounting Manager
263 | Verkoop
264 | Rijnweg 22
265 | Zaandam
266 | 9999 ZZ
267 | Netherlands
268 | (12345) 1212
269 | (12345) 1210
270 |
271 |
272 | 23
273 | Karkki Oy
274 | Anne Heikkonen
275 | Product Manager
276 | Valtakatu 12
277 | Lappeenranta
278 | 53120
279 | Finland
280 | (953) 10956
281 |
282 |
283 | 24
284 | G'day, Mate
285 | Wendy Mackenzie
286 | Sales Representative
287 | 170 Prince Edward Parade
288 | Hunter's Hill
289 | Sydney
290 | NSW
291 | 2042
292 | Australia
293 | (02) 555-5914
294 | (02) 555-4873
295 | G'day Mate (on the World Wide Web)#http://www.microsoft.com/accessdev/sampleapps/gdaymate.htm#
296 |
297 |
298 | 25
299 | Ma Maison
300 | Jean-Guy Lauzon
301 | Marketing Manager
302 | 2960 Rue St. Laurent
303 | Montréal
304 | Québec
305 | H1J 1C3
306 | Canada
307 | (514) 555-9022
308 |
309 |
310 | 26
311 | Pasta Buttini s.r.l.
312 | Giovanni Giudici
313 | Order Administrator
314 | Via dei Gelsomini, 153
315 | Salerno
316 | 84100
317 | Italy
318 | (089) 6547665
319 | (089) 6547667
320 |
321 |
322 | 27
323 | Escargots Nouveaux
324 | Marie Delamare
325 | Sales Manager
326 | 22, rue H. Voiron
327 | Montceau
328 | 71300
329 | France
330 | 85.57.00.07
331 |
332 |
333 | 28
334 | Gai pâturage
335 | Eliane Noz
336 | Sales Representative
337 | Bat. B
338 | 3, rue des Alpes
339 | Annecy
340 | 74000
341 | France
342 | 38.76.98.06
343 | 38.76.98.58
344 |
345 |
346 | 29
347 | Forêts d'érables
348 | Chantal Goulet
349 | Accounting Manager
350 | 148 rue Chasseur
351 | Ste-Hyacinthe
352 | Québec
353 | J2S 7S8
354 | Canada
355 | (514) 555-2955
356 | (514) 555-2921
357 |
358 |
--------------------------------------------------------------------------------
/Replace-text-with-table/Console-App-.NET-Core/Replace-text-with-table/Template.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-table/Console-App-.NET-Core/Replace-text-with-table/Template.docx
--------------------------------------------------------------------------------
/Replace-text-with-table/Images/Replace-text-with-table.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SyncfusionExamples/Word-Find-and-Replace-Examples/a0284080534c37b9553f517a96ba951672d079a0/Replace-text-with-table/Images/Replace-text-with-table.png
--------------------------------------------------------------------------------