Как объединить несколько RTF файлов на C# и .NET


Полный код

using System;
using System.IO;
using static SautinSoft.HtmlToRtf;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {
			// Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/
	
            // Merge multiple RTF files
            // If you need more information about "HTML to RTF .Net" email us at:
            // support@sautinsoft.com		
            MergeFiles();
        }

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

            DirectoryInfo htmlDir = new DirectoryInfo(@"..\..\..\");

            // Array with several RTF files.            
            string[] rtfFiles = new string[] { "footer.rtf", "footer.rtf", "footer.rtf" };
            string singleRtf = String.Empty;

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

            foreach (string rtfFile in rtfFiles)
            {
                string rtfFilePath = Path.Combine(htmlDir.FullName, rtfFile);

                // Copy 1st RTF to 'singleRtf'
                if (String.IsNullOrEmpty(singleRtf))
                    singleRtf = File.ReadAllText(rtfFilePath);

                // Merge 2nd, 3rd ....
                else
                    singleRtf = h.MergeRtfString(singleRtf, File.ReadAllText(rtfFilePath));
            }

            // Save 'singleRtf' to a file only for demonstration purposes.
            string singleRtfPath = "Single.rtf";
            File.WriteAllText(singleRtfPath, singleRtf);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singleRtfPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.HtmlToRtf

Namespace Sample
	Friend Class Test

		Shared Sub Main(ByVal args() As String)
			' Get your free 100-day key here:   
            ' https://sautinsoft.com/start-for-free/
	
			' Merge multiple RTF files
			' If you need more information about "HTML to RTF .Net" email us at:
			' support@sautinsoft.com		
			MergeFiles()
		End Sub

		Public Shared Sub MergeFiles()
			Dim h As New SautinSoft.HtmlToRtf()

			Dim htmlDir As New DirectoryInfo("..\..\..\")

			' Array with several RTF files.            
			Dim rtfFiles() As String = {"footer.rtf", "footer.rtf", "footer.rtf"}
			Dim singleRtf As String = String.Empty

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

			For Each rtfFile As String In rtfFiles
				Dim rtfFilePath As String = Path.Combine(htmlDir.FullName, rtfFile)

				' Copy 1st RTF to 'singleRtf'
				If String.IsNullOrEmpty(singleRtf) Then
					singleRtf = File.ReadAllText(rtfFilePath)

					' Merge 2nd, 3rd ....
				Else
					singleRtf = h.MergeRtfString(singleRtf, File.ReadAllText(rtfFilePath))
				End If
			Next rtfFile

			' Save 'singleRtf' to a file only for demonstration purposes.
			Dim singleRtfPath As String = "Single.rtf"
			File.WriteAllText(singleRtfPath, singleRtf)
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(singleRtfPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.