ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Merge (Combine) multiple RTF files into one (single) using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="dnnyobi"> dnnyobi</a>,</p> <p>Refer below sample code.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string[] allRTFDocument = Directory.GetFiles(Server.MapPath(&#34;~/Files&#34;), &#34;*.rtf&#34;); Merge(allRTFDocument, Server.MapPath(&#34;~/Folder/Final.rtf&#34;), true); } } public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks) { object missing = System.Type.Missing; object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage; object outputFile = outputFilename; Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application(); try { Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.Add( ref missing , ref missing , ref missing , ref missing); Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection; int documentCount = filesToMerge.Length; int breakStop = 0; foreach (string file in filesToMerge) { breakStop++; selection.InsertFile(file, ref missing, ref missing, ref missing, ref missing); if (insertPageBreaks &amp;&amp; breakStop != documentCount) { selection.InsertBreak(ref pageBreak); } } wordDocument.SaveAs( ref outputFile , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing); wordDocument = null; } catch (Exception ex) { throw ex; } finally { wordApplication.Quit(ref missing, ref missing, ref missing); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then Dim allRTFDocument As String() = Directory.GetFiles(Server.MapPath(&#34;~/Files&#34;), &#34;*.rtf&#34;) Merge(allRTFDocument, Server.MapPath(&#34;~/Folder/Final.rtf&#34;), True) End If End Sub Public Shared Sub Merge(ByVal filesToMerge As String(), ByVal outputFilename As String, ByVal insertPageBreaks As Boolean) Dim missing As Object = System.Type.Missing Dim pageBreak As Object = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage Dim outputFile As Object = outputFilename Dim wordApplication As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application() Try Dim wordDocument As Microsoft.Office.Interop.Word.Document = wordApplication.Documents.Add(missing, missing, missing, missing) Dim selection As Microsoft.Office.Interop.Word.Selection = wordApplication.Selection Dim documentCount As Integer = filesToMerge.Length Dim breakStop As Integer = 0 For Each file As String In filesToMerge breakStop += 1 selection.InsertFile(file, missing, missing, missing, missing) If insertPageBreaks AndAlso breakStop &lt;&gt; documentCount Then selection.InsertBreak(pageBreak) End If Next wordDocument.SaveAs(outputFile, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing) wordDocument = Nothing Catch ex As Exception Throw ex Finally wordApplication.Quit(missing, missing, missing) End Try End Sub</pre> <p>&nbsp;</p>, 27 Aug 2019 06:01:21 GMT