How to merge two RTF documents in memory using C# and VB.NET


Complete code

using System;
using System.IO;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {
            // Merge two RTF documents in memory.
            // If you need more information about "HTML to RTF .Net" email us at:
            // support@sautinsoft.com.
            MergeRtfsInMemory();
        }

        public static void MergeRtfsInMemory()
        {
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

            // After purchasing the license, please insert your serial number here to activate the component.
            // h.Serial = "XXXXXXXXX";

            // Now we've both RTF documents stored in memory in String objects.
            string rtfString1 = File.ReadAllText(@"..\..\footer.rtf");
            string rtfString2 = File.ReadAllText(@"..\..\footer.rtf");

            // Let's divide RTF documents using page break.
            h.MergeOptions.PageBreakBetweenDocuments = true;

            string rtfSingle = h.MergeRtfString(rtfString1, rtfString2);

            // Save 'rtfSingle' to a file for demonstration purposes and show it.
            if (!String.IsNullOrEmpty(rtfSingle))
            {
                string singleRtfFile = "Single.rtf";
                File.WriteAllText(singleRtfFile, rtfSingle);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singleRtfFile) { UseShellExecute = true });
            }
        }
    }
}

Download

Imports System
Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        ' Merge two RTF documents in memory.
        ' If you need more information about "HTML to RTF .Net" email us at:
        ' support@sautinsoft.com.
        MergeRtfsInMemory()
    End Sub

    Public Sub MergeRtfsInMemory()
        Dim h As New SautinSoft.HtmlToRtf()

        ' After purchasing the license, please insert your serial number here to activate the component.
        'h.Serial = "XXXXXXXXX"

        ' Now we've both RTF documents stored in memory in String objects.
        Dim rtfString1 As String = File.ReadAllText("..\footer.rtf")
        Dim rtfString2 As String = File.ReadAllText("..\footer.rtf")

        ' Let's divide RTF documents using page break.
        h.MergeOptions.PageBreakBetweenDocuments = True

        Dim rtfSingle As String = h.MergeRtfString(rtfString1, rtfString2)

        ' Save 'rtfSingle' to a file for demonstration purposes and show it.
        If Not String.IsNullOrEmpty(rtfSingle) Then
            Dim singleRtfFile As String = "Single.rtf"
            File.WriteAllText(singleRtfFile, rtfSingle)
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(singleRtfFile) With {.UseShellExecute = True})
        End If
    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.