Replaces all found matches of string "whatReplaceInRtf" by the string "wherewithReplaceInRtf" and returns a new RTF document

Namespace:  SautinSoft
Assembly:  SautinSoft.HtmlToRtf (in SautinSoft.HtmlToRtf.dll) Version: 4.6.10.19

Syntax

      
 C#  Visual Basic 
public string MergeAndReplaceRtfString(
	string rtfSource,
	string whatReplaceInRtf,
	string wherewithReplaceInRtf
)
Public Function MergeAndReplaceRtfString ( _
	rtfSource As String, _
	whatReplaceInRtf As String, _
	wherewithReplaceInRtf As String _
) As String

Parameters

rtfSource
String
Source RTF document where to search matches of "whatReplaceInRtf"
whatReplaceInRtf
String
String in Text or RTF format which will be replaced by the new string "wherewithReplaceInRtf"
wherewithReplaceInRtf
String
String in RTF format which will be used to replace all matches of string "whatReplaceInRtf"

Return Value

A new RTF document based on rtfSource
null - in case of operation failed

Remarks



Examples

CopyInsert one RTF document inside another RTF in memory using C#
using System;
using System.IO;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {

            //Insert one RTF document inside another RTF in memory             
            //If you need more information about HTML-to-RTF Pro DLL .Net email us at:
            //support[at]sautinsoft.com

            SautinSoft.HtmlToRtf objH = new SautinSoft.HtmlToRtf();
            //objH.Serial = "XXXXXXXXXXXXXXX";

            string firstRtf="";
            string secondRtf="";
            string singleRtf="";
            string stringToReplace = "{replace me}";

            //1. Read contents of the RTF files
            firstRtf=ReadFileString(@"d:\report1.rtf");
            secondRtf=ReadFileString(@"d:\report2.rtf");


            /// <summary>
            /// Replaces all matches of string "whatReplaceInRtf" by the string "wherewithReplaceInRtf" and returns a new RTF document
            /// </summary>
            //In this example replaces all matches of the string "{replace me}" by the string from variable 'secondRtf'
            singleRtf = objH.MergeAndReplaceRtfString(firstRtf,stringToReplace, secondRtf);

            //New RTF string based on rtfSource
            //null - operation failed
            if(singleRtf!="")
            {

                int ret=WriteToFile(@"d:\single.rtf",singleRtf);
                if (ret==0)
                    System.Diagnostics.Process.Start(@"d:\single.rtf");
            }
        }
        public static string ReadFileString(string path)
        {
            FileStream fo = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bHtmlIn = new byte[((int)fo.Length)];
            try
            {
                fo.Read(bHtmlIn, 0, (int)fo.Length);
                fo.Close();
                char[] arCharRes = new char[bHtmlIn.Length];
                for (int i = 0; i < bHtmlIn.Length; i++)
                {
                    arCharRes[i] = (char)bHtmlIn[i];
                }
                return new string(arCharRes);
            }
            catch
            {
                return "";
            }
        }
        public static int WriteToFile(string fileName, string contents)
        {
            try
            {
                StreamWriter sw = new StreamWriter(fileName);
                sw.Write(contents);
                sw.Close();                
            }
            catch
            {
                return 2;
            }
            return 0;
        }

    }
}
CopyInsert one RTF document inside another RTF in memory using VB.Net
Imports System
Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        'Insert one RTF document inside another RTF in memory             
        'If you need more information about HTML-to-RTF Pro DLL .Net email us at:
        'support[at]sautinsoft.com


        Dim objH As New SautinSoft.HtmlToRtf
        'objH.Serial = "XXXXXXXXXXXXXXX";


        Dim firstRtf As String = ""
        Dim secondRtf As String = ""
        Dim singleRtf As String = ""
        Dim stringToReplace As String = "{replace me}"

        '1. Read contents of the RTF files
        firstRtf = ReadFromFile("d:\report1.rtf")
        secondRtf = ReadFromFile("d:\report2.rtf")


        ''' <summary>
        ''' Replaces all matches of string "whatReplaceInRtf" by the string "wherewithReplaceInRtf" and returns a new RTF document
        ''' </summary>
        'In this example replaces all matches of the string "{replace me}" by the string from variable 'secondRtf'
        singleRtf = objH.MergeAndReplaceRtfString(firstRtf, stringToReplace, secondRtf)

        'New RTF string based on rtfSource
        'null - operation failed
        If singleRtf <> "" Then

            Dim ret As Integer = WriteToFile("d:\single.rtf", singleRtf)
            If ret = 0 Then
                System.Diagnostics.Process.Start("d:\single.rtf")
            End If
        End If
    End Sub
    Public Function ReadFromFile(ByVal fileName As String) As String

        Dim fileString As String = ""
        Try
            Dim fs As System.IO.FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim b(CInt(Fix(fs.Length)) - 1) As Byte
            If fs.Read(b, 0, CInt(Fix(fs.Length))) > 0 Then
                Dim arCharRes(fs.Length - 1) As Char
                For i As Integer = 0 To fs.Length - 1
                    arCharRes(i) = ChrW(b(i))
                Next i
                fileString = New String(arCharRes)
            End If
            fs.Close()
            Return fileString
        Catch
            Return ""
        End Try
    End Function
    Public Function WriteToFile(ByVal fileName As String, ByVal fileStr As String) As Integer
        Try
            Dim sw As New StreamWriter(fileName, False)
            sw.Write(fileStr)
            sw.Close()
        Catch
            Return 2
        End Try
        Return 0
    End Function
End Module

See Also