C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa

Tuesday, 19 July 2022 05:40 Written by  support iceblue
Rate this item
(0 votes)

Rich Text Format (RTF) is a proprietary text file format which can serve as an exchange format between word processing programs from different manufacturers on different operating systems. Rich text documents include page formatting options, such as custom page margins, line spacing, and tab widths. With rich text, it's easy to create columns, add tables, and format your documents in a way that makes them easier to read. This article will demonstrate how to convert Word Doc/Docx to RTF files and convert RTF to Word Doc/Docx with the help of 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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word Doc/Docx to RTF

The following steps show you how to convert Word to RTF using Spire.Doc for .NET.

  • Create a Document instance.
  • Load a Word sample document using Document.LoadFromFile() method.
  • Save the document as an RTF file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ConvertToRtf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Word document instance
            Document document = new Document();

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

            // Save the document to RTF
            document.SaveToFile("Result.rtf", FileFormat.Rtf);
        }

    }
}

C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa

Convert RTF to Word Doc/Docx

The steps to convert an RTF document to Word Doc/Docx is very similar with that of the above example:

  • Create a Document instance.
  • Load an RTF document using Document.LoadFromFile() method.
  • Save the RTF document to Doc/Docx format using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System;

public class RtfToDocDocx
{

    public static void Main(String[] args)
    {
        // Create a Document instance
        Document document = new Document();
        // Load an RTF document
        document.LoadFromFile("input.rtf", FileFormat.Rtf);
        // Save the document to Doc
        document.SaveToFile("toDoc.doc", FileFormat.Doc);
        // Save the document to Docx
        document.SaveToFile("toDocx.docx", FileFormat.Docx2013);
    }
}

C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa

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.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 19 July 2022 05:13