Insert a Paragraph to Word Document in WPF with C#, VB.NET

2016-01-22 03:37:12 Written by  support iceblue
Rate this item
(0 votes)

Using Spire.Doc for WPF, programmers can easily create, open, modify and save Word documents in WPF applications. In this article, we’ll introduce how to insert a paragraph to desired position in an existing Word document.

To begin with, you need to download Spire.Doc for WPF and add the Spire.Doc.Wpf.dll and Spire.License.dll as the references to your WPF project. Then add a button in MainWindow and double click the button to write code.

Here are code snippets in the button click event:

Step 1: Initialize a new instance of Document class and load the sample Word document.

Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);

Step 2: Initialize a new instance of Paragraph class and append some text to it.

Paragraph paraInserted = new Paragraph(document);
TextRange textRange1 = paraInserted.AppendText("Hello, this is a new paragraph.");

Step 3: Set the text formatting of the paragraph.

textRange1.CharacterFormat.TextColor = System.Drawing.Color.Purple;
textRange1.CharacterFormat.FontSize = 11;
textRange1.CharacterFormat.FontName = "Calibri Light";

Step 4: Insert the paragraph at the first section at index 1, which indicates the position of the second paragraph in the section.

document.Sections[0].Paragraphs.Insert(1, paraInserted);

Step 5: Save the file.

document.SaveToFile("result.docx", FileFormat.Docx);

Output:

Insert a Paragraph to Word Document in WPF with C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            document.LoadFromFile("sample.docx", FileFormat.Docx);

            Paragraph paraInserted = new Paragraph(document);
            TextRange textRange1 = paraInserted.AppendText("Hello, this is a new paragraph.");
            textRange1.CharacterFormat.TextColor = System.Drawing.Color.Purple;
            textRange1.CharacterFormat.FontSize = 11;
            textRange1.CharacterFormat.FontName = "Calibri Light";

            document.Sections[0].Paragraphs.Insert(1, paraInserted);
            document.SaveToFile("result.docx", FileFormat.Docx);
        }


    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Windows

Namespace WpfApplication1
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim document As New Document()
			document.LoadFromFile("sample.docx", FileFormat.Docx)

			Dim paraInserted As New Paragraph(document)
			Dim textRange1 As TextRange = paraInserted.AppendText("Hello, this is a new paragraph.")
			textRange1.CharacterFormat.TextColor = System.Drawing.Color.Purple
			textRange1.CharacterFormat.FontSize = 11
			textRange1.CharacterFormat.FontName = "Calibri Light"

			document.Sections(0).Paragraphs.Insert(1, paraInserted)
			document.SaveToFile("result.docx", FileFormat.Docx)
		End Sub


	End Class
End Namespace

Additional Info

  • tutorial_title: Insert a Paragraph to Word Document in WPF
Last modified on Friday, 24 September 2021 09:52