How to Insert RTF String to Word Documents in C#, VB.NET

2017-03-21 07:52:46 Written by  support iceblue
Rate this item
(0 votes)

RTF is a document language used for encoding formatted text and graphics for easy transfer between applications. This article presents how to insert a piece of RTF encoded string to Word document using Spire.Doc.

Step 1: Initialize an instance of Document class, add a section to it.

Document doc = new Document();
Section section = doc.

Step 2: Add a paragraph to the section.

Paragraph para = section.AddParagraph();

Step 3: Declare a String variable to store the RTF string.

String rtfString = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}";

Step 4: Append RTF string to paragraph.

para.AppendRTF(rtfString);

Step 5: Save the file.

doc.SaveToFile("Output.docx");

Output:

How to Insert RTF String to Word Documents in C#, VB.NET

Full Code:

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

     static void Main(string[] args)
     {
         Document doc = new Document();
         Section section = doc.AddSection();
         Paragraph para = section.AddParagraph();

         String rtfString = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}";
         para.AppendRTF(rtfString);

         doc.SaveToFile("Output.docx");

     }

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

		Private Shared Sub Main(args As String())
			Dim doc As New Document()
			Dim section As Section = doc.AddSection()
			Dim para As Paragraph = section.AddParagraph()

			Dim rtfString As [String] = "{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}"
			para.AppendRTF(rtfString)

			doc.SaveToFile("Output.docx")

		End Sub

	End Class
End Namespace

Additional Info

  • tutorial_title: Insert RTF String to Word Documents
Last modified on Friday, 03 September 2021 03:34