Spire.Doc Knowledgebase - Page8
Spire.Doc for .NET

Replace Text with a Word document in C#

2018-07-24 07:10:57 Written by Koohji

Spire.Doc provides several overloaded Replace methods to replace text in different scenarios. This article is going to show you how to replace a specified text in a template document with another document using Spire.Doc.

The template document:

Replace Text with a Word document in C#

The document to replace text:

Replace Text with a Word document in C#

Detail steps:

Step 1: Load a template document.

Document document = new Document("Template.docx");

Step 2: Load another document to replace text.

IDocument replaceDocument = new Document("Document1.docx");

Step 3: Replace specified text with the other document.

document.Replace("Document 1", replaceDocument, false, true);  

Step 4: Save the file.

document.SaveToFile("Output.docx", FileFormat.Docx2013);

Output:

Replace Text with a Word document in C#

Full code:

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

namespace Replace_Text_With_Document
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a template document 
            Document document = new Document("Template.docx");

            //Load another document to replace text
            IDocument replaceDocument = new Document("Document1.docx");

            //Replace specified text with the other document
            document.Replace("Document 1", replaceDocument, false, true);

            //Save the file
            document.SaveToFile("Output.docx", FileFormat.Docx2013);
        }
    }
}

When processing a Word document, you may need to remove some paragraphs. For example, after you copied contents from the Internet with a lot of redundant paragraphs to your document, you need to delete the extra paragraphs and keep only those that are useful. The deletion can be easily achieved by Spire.Doc for .NET by programming with no need for other software. This article will show you the detailed steps of removing paragraphs in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Delete a Specific Paragraph in a Word Document

Spire.Doc for .NET provides a method RemoveAt() under ParagraphCollection to remove paragraphs.

The detailed steps of removing a specific paragraph are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Section[] property.
  • Remove the 4th paragraph using Section.Paragraphs.RemoveAt() method.
  • Save the document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System;
using Spire.Doc;

namespace RemoveParagraphs
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("Sample.docx");

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

            //Remove the first paragraph in the section
            section.Paragraphs.RemoveAt(3);
            
            //Save the document
            document.SaveToFile("RemoveParagraphs.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Remove Paragraphs in a Word Document

Delete All Paragraphs in a Word Document

To remove all paragraphs, you can use the method Clear() under ParagraphCollection provided by Spire.Doc for .NET.

The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word Document using Document.LoadFromFile() method.
  • Loop through all sections, and remove all paragraphs in each section using Section.Paragraphs.Clear() method.
  • Save the document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System;
using Spire.Doc;

namespace RemoveAllParagraphs
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Loop through all sections
            foreach (Section section in document.Sections)
            {
                //Remove all paragraphs in the section
                section.Paragraphs.Clear();
            }

            //Save the document
            document.SaveToFile("RemoveAllParagraphs.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Remove Paragraphs in a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

By using Spire.Doc, you can not only retrieve the style names of all paragraphs in a Word document, but also get the paragraphs with a specific style name. This is useful especially when you need to get the text in Title, Heading 1, Subtitle, etc.

Paragraph Style Names in Word Paragraph Style Names in Spire.Doc
Title Title
Heading 1 Heading1
Heading 2 Heading2
Heading 3 Heading3
Heading 4 Heading3
Subtitle Subtitle

Step 1: Load a sample Word file when initializing the Document object.

Document doc = new Document("sample.docx");

Step 2: Traverse the sections and paragraphs in the document and determine if the paragraph style name is "Heading1", if so, write the paragraph text on screen.

foreach (Section section in doc.Sections)
{
    foreach (Paragraph paragraph in section.Paragraphs)
    {
        if (paragraph.StyleName == "Heading1")
        {
            Console.WriteLine(paragraph.Text);
        }
    }
}

Output:

Get Paragraphs by Style Name in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System;
namespace GetParagh
{
 class Program
    {

     static void Main(string[] args)
     {
         Document doc = new Document("sample.docx");
         foreach (Section section in doc.Sections)
         {
             foreach (Paragraph paragraph in section.Paragraphs)
             {
                 if (paragraph.StyleName == "Heading1")
                 {
                     Console.WriteLine(paragraph.Text);
                 }
             }
         }
     }

    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace GetParagh
	Class Program

		Private Shared Sub Main(args As String())
			Dim doc As New Document("sample.docx")
			For Each section As Section In doc.Sections
				For Each paragraph As Paragraph In section.Paragraphs
					If paragraph.StyleName = "Heading1" Then
						Console.WriteLine(paragraph.Text)
					End If
				Next
			Next
		End Sub

	End Class
End Namespace
Page 9 of 57
page 9