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

  1. Save to a file:
    
                // The file format will be detected automatically from the file extension: ".rtf".
                dc.Save(@"d:\Book.rtf");
    

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

    
    dc.Save(@"d:\Book.rtf", new RtfSaveOptions());
    

    In RtfSaveOptions you can also set various options which affects to the saving process.

  2. Save to a Stream:
                // Let's save our document to a MemoryStream.
                using (MemoryStream ms = new MemoryStream())
                {
                    dc.Save(ms, new RtfSaveOptions());
                }
    
 

Complete code

using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        
        static void Main(string[] args)
        {
            SaveToRtfFile();
            SaveToRtfStream();
        }

        static void SaveToRtfFile()
        {
            // Assume we already have a document 'dc'.
            DocumentCore dc = new DocumentCore();
            dc.Content.End.Insert("Hey Guys and Girls!");

            string filePath = @"Result-file.rtf";

            dc.Save(filePath, new RtfSaveOptions());
        }

        static void SaveToRtfStream()
        {
            // There variables are necessary only for demonstration purposes.
            byte[] fileData = null;
            string filePath = @"Result-stream.rtf";

            // Assume we already have a document 'dc'.
            DocumentCore dc = new DocumentCore();
            dc.Content.End.Insert("Hey Guys and Girls!");

            // Let's save our document to a MemoryStream.
            using (MemoryStream ms = new MemoryStream())
            {
                dc.Save(ms, new RtfSaveOptions());
                fileData = ms.ToArray();
            }
            File.WriteAllBytes(filePath, fileData);
        }
    }
}

Download.

        
            Imports System.IO
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        SaveToRtfFile()
        SaveToRtfStream()
    End Sub

    Public Sub SaveToRtfFile()

        ' Assume we already have a document 'dc'.
        Dim dc As New DocumentCore()
        dc.Content.End.Insert("Hey Guys and Girls!")

        Dim filePath As String = "Result-file.rtf"

        dc.Save(filePath, New RtfSaveOptions())
    End Sub

    Public Sub SaveToRtfStream()

        ' There variables are necessary only for demonstration purposes.
        Dim fileData() As Byte = Nothing
        Dim filePath As String = "Result-stream.rtf"

        ' Assume we already have a document 'dc'.
        Dim dc As New DocumentCore()
        dc.Content.End.Insert("Hey Guys and Girls!")

        ' Let's save our document to a MemoryStream.
        Using ms As New MemoryStream()
            dc.Save(ms, New RtfSaveOptions())
            fileData = ms.ToArray()
        End Using
        File.WriteAllBytes(filePath, fileData)
    End Sub
End Module

Download.

© SautinSoft 2019