How to load a RTF document in C# and VB.Net

  1. Load from a file
    
    DocumentCore dc = DocumentCore.Load(@"d:\Book.rtf");
    
    The dc object represents a document loaded into memory. The file format is detected automatically from the file extension: ".rtf".

    To not rely on the file extension and guarantee that the file contents is really RTF, you may specify RtfLoadOptions parameter.

    
    DocumentCore dc = DocumentCore.Load(@"d:\Book.rtf", new RtfLoadOptions());
    

    RtfLoadOptions
    Specifies to load a document in Rich Text (RTF) format and allows to set the loading properties.
    All properties already set to convenient defaults, but you may change any of them:

    Property Default Description
    Encoding Encoding.ASCII An encoding to load a RTF document.
    PreserveUnsupportedFeatures true Indicates whether to preserve file format features that are not directly supported.
    
                DocumentCore dc = DocumentCore.Load(@"d:\Book.rtf", new RtfLoadOptions());
    
  2. Load from a Stream
    
                // Let us say we already have a RTF document as array of bytes.
                DocumentCore dc = null;
                using (MemoryStream rtfStream = new MemoryStream(rtfBytes))
                {
                    dc = DocumentCore.Load(rtfStream, new DocxLoadOptions());
                }
                // Here we can do with our document 'dc' anything we need.
    
 

Complete code

using System;
using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            LoadRtfFromFile();
            //LoadRtfFromStream();
        }
        // From a file
        static void LoadRtfFromFile()
        {
            string filePath = @"d:\Book.rtf";
            // The file format is detected automatically from the file extension: ".rtf".
            // But as shown in the example below, we can specify RtfLoadOptions as 2nd parameter
            // to explicitly set that a loadable document has RTF format.
            DocumentCore dc = DocumentCore.Load(filePath);
        }
        // From a Stream    
        static void LoadRtfFromStream()
        {
            // Get document bytes.
            byte[] fileBytes = File.ReadAllBytes(@"d:\Book.rtf");

            DocumentCore dc = null;            
            // Create a MemoryStream
            using (MemoryStream ms = new MemoryStream(fileBytes))
            {
                // Load a document from the MemoryStream.
                // Specifying RtfLoadOptions we explicitly set that a loadable document is RTF.
                dc = DocumentCore.Load(ms, new RtfLoadOptions());
            }
        }
    }
}
        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        LoadRtfFromFile()
        'LoadRtfFromStream();
    End Sub
    ' From a file
    Public Sub LoadRtfFromFile()
        Dim filePath As String = "d:\Book.rtf"
        ' The file format is detected automatically from the file extension: ".rtf".
        ' But as shown in the example below, we can specify RtfLoadOptions as 2nd parameter
        ' to explicitly set that a loadable document has RTF format.
        Dim dc As DocumentCore = DocumentCore.Load(filePath)
    End Sub
    ' From a Stream
    Public Sub LoadRtfFromStream()
        ' Get document bytes.
        Dim fileBytes() As Byte = File.ReadAllBytes("d:\Book.rtf")

        Dim dc As DocumentCore = Nothing
        ' Create a MemoryStream
        Using ms As New MemoryStream(fileBytes)
            ' Load a document from the MemoryStream.
            ' Specifying RtfLoadOptions we explicitly set that a loadable document is RTF.
            dc = DocumentCore.Load(ms, New RtfLoadOptions())
        End Using
    End Sub
End Module
© SautinSoft 2017