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

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

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

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

    
    dc.Save(@"d:\Book.txt", new TxtSaveOptions());
    

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

    
                dc.Save(@"d:\Book.txt", new TxtSaveOptions()
                {
                    Encoding = System.Text.Encoding.UTF8,
                    ParagraphBreak=Environment.NewLine
                });;
    
  2. Save to a Stream:
                // Let's save our document to a MemoryStream.
                using (MemoryStream ms = new MemoryStream())
                {
                    dc.Save(ms, new TxtSaveOptions());
                }
    
 

Complete code

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

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

        /// <summary>
        /// Creates a new document and saves it as Text file.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/examples/save-document-as-text-net-csharp-vb.php
        /// </remarks>
        static void SaveToTextFile()
        {
            // Assume we already have a document 'dc'.
            DocumentCore dc = new DocumentCore();
            dc.Content.End.Insert("Hey Guys and Girls!");

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

            dc.Save(filePath, new TxtSaveOptions()
            {
                Encoding = System.Text.Encoding.UTF8,
                ParagraphBreak = Environment.NewLine
            });

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
        }

        /// <summary>
        /// Creates a new document and saves it as Text using MemoryStream.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/examples/save-document-as-text-net-csharp-vb.php
        /// </remarks>
        static void SaveToTextStream()
        {
            // There variables are necessary only for demonstration purposes.
            byte[] fileData = null;
            string filePath = @"Result-stream.txt";

            // 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 TxtSaveOptions());
                fileData = ms.ToArray();
            }
            File.WriteAllBytes(filePath, fileData);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        SaveToTextFile()
        SaveToTextStream()
    End Sub

    ''' <summary>
    ''' Creates a new document and saves it as Text file.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://www.sautinsoft.com/products/document/examples/save-document-as-text-net-csharp-vb.php
    ''' </remarks>
    Sub SaveToTextFile()
        ' 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.txt"

        dc.Save(filePath, New TxtSaveOptions() With {
            .Encoding = System.Text.Encoding.UTF8,
            .ParagraphBreak = Environment.NewLine
        })

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePath) With {.UseShellExecute = True})
    End Sub

    ''' <summary>
    ''' Creates a new document and saves it as Text using MemoryStream.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://www.sautinsoft.com/products/document/examples/save-document-as-text-net-csharp-vb.php
    ''' </remarks>
    Sub SaveToTextStream()
        ' There variables are necessary only for demonstration purposes.
        Dim fileData() As Byte = Nothing
        Dim filePath As String = "Result-stream.txt"

        ' 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 TxtSaveOptions())
            fileData = ms.ToArray()
        End Using
        File.WriteAllBytes(filePath, fileData)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePath) With {.UseShellExecute = True})
    End Sub
End Module

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.

© SautinSoft 2002 - 2019