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;
doc.LoadFile(filename);
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]))
continue;
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;
dt.Columns.Add(columns[j]);
}
if (!idFieldFound)
dt.Columns.Add(ConfigConstants.UniqueFieldColumn);
firstRow = false;
}
else
{
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();
dt.Rows.Add(row);
}
}
return dt;
}
@SangeeN,
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.
https://www.dropbox.com/s/uo6smhf9sw3a7ky/UnitTestProject1.zip?dl=0
The attched code GetDataTableContentFromRtf input file should be rtf file. Anyway it doesnt work.
@SangeeN,
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();
dataTable.Rows.Add(dataRow);
}
Document doc = new Document("E:\\Temp\\UnitTestProject1\\36482.docx");
doc.MailMerge.Execute(dataTable);
doc.Save("E:\\Temp\\UnitTestProject1\\20.7.docx");
@awais.hafeez Thanks, what if the document has more tables?
Here we retrieve only onlytable, Table table = rtfDoc.FirstSection.Body.Tables[0];
@SangeeN,
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
}