We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert Table in RTF Word Document to C# .NET DataTable Object | Execute Mail Merge | RTF File to String | Rich Text Box

Is it possible to read rtf file and apply it to System.Data.DataTable? Old code has RichTextBox which I dont want to use it now. Is it possible to do it with Aspose.

public static System.Data.DataTable GetDataTableContentFromRtf(string filename)
    var doc = new RichTextBox();
    var idFieldFound = false;
    var content = doc.Text;
    var rows = content.Split('\n');
    if (rows.Length == 1)
        return null;

    var firstRow = true;
    var dt = new System.Data.DataTable();
    for (var i = 0; i < rows.Length; i++)
        if (string.IsNullOrWhiteSpace(rows[i]))

        var columns = rows[i].Split('\t');
        if (firstRow)
            for (var j = 0; j < columns.Count(); j++)
                if (!string.IsNullOrEmpty(columns[j]))
                    if (columns[j] == ConfigConstants.UniqueFieldColumn) idFieldFound = true;
            if (!idFieldFound)
            firstRow = false;
            var row = dt.NewRow();
            for (var k = 0; k < dt.Columns.Count - 1; k++) row[k] = columns[k];
            if (!idFieldFound) row[ConfigConstants.UniqueFieldColumn] = Guid.NewGuid().ToString();
    return dt;

testingMerge.zip (710 Bytes)


Please check if the following code is acceptable for you?

// Load RTF document with Aspose.Words for .NET
Document doc = new Document("E:\\Temp\\testingMerge\\testingMerge.rtf");
// Save it to a temporary RTF Stream
MemoryStream rtfStream = new MemoryStream();
doc.Save(rtfStream, SaveFormat.Rtf);
rtfStream.Position = 0;
// Convert RTF Stream to RTF String
string rtfString = Encoding.UTF8.GetString(rtfStream.ToArray());
// Store RTF String in DataTable
// Or even Write RTF String to File on Disk
File.WriteAllText("E:\\Temp\\testingMerge\\20.6.txt", rtfString); 

If not then please elaborate your inquiry further by providing complete details of your usecase. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

36482.zip (35.0 KB)
Hi, The above suggestion doesnt work. Acually I wanted to exract the rtf content and replace the content in doc file. In the project attached you will find the rtf file and the docx file for input. I also attached the output docx file.

In code the output file is uniqueFileName. You can ignore the logic after that.


The attched code GetDataTableContentFromRtf input file should be rtf file. Anyway it doesnt work.


Please try the following C# Code that will convert Table in RTF Word Document into a DataTable object and then execute Mail Merge operation using the data of DataTable object:

DataTable dataTable = new DataTable("Data");
Document rtfDoc = new Document("E:\\Temp\\UnitTestProject1\\36482.rtf");
Table table = rtfDoc.FirstSection.Body.Tables[0];

Row headerRow = table.FirstRow;
for (int i = 0; i < headerRow.Cells.Count; i++)
    dataTable.Columns.Add(new DataColumn(headerRow.Cells[i].ToString(SaveFormat.Text).Trim()));

for (int i = 1; i < table.Rows.Count; i++)
    DataRow dataRow = dataTable.NewRow();
    for (int j = 0; j < table.Rows[i].Cells.Count; j++)
        dataRow[j] = table.Rows[i].Cells[j].ToString(SaveFormat.Text).Trim();

Document doc = new Document("E:\\Temp\\UnitTestProject1\\36482.docx");

@awais.hafeez Thanks, what if the document has more tables?
Here we retrieve only onlytable, Table table = rtfDoc.FirstSection.Body.Tables[0];


You can get a collection of all the Tables in RTF Word document by using the following code:

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
    // build DataTable object against each Table found in RTF Word Document