Hi thanks for the help,
I looked into the article "Inserting RTF preformatted text into a doc" and it actually replace a text in the document by another document which contains rtf text. But I could find, if I use this, it will replace all the matching text which i dont need to be replaced. for eg: suppose the doc contains a table of four cells , first and third cell contains same text or some part is same, lets say first and thrid cell contains text "hello world", the format of the two cell texts are different, say one is bold and other is italic and we have to replace the text in the first cell only [with bold "hello world"] , it will replace the text in third cell too.
Also, when i replaced the specified string in a doc with a rtf string it works, but for my requirement, what I tried to do is that,
create a form with tablelayout of having two rows and two columns, place richtextboxes in all cells, in a button click call the mothod BuildDocument(), traverse through all the cells, create the docuement using rtf text in the richtextboxes. But the original formatted text is not saved in the final word doc
Please see the below code.
int currentColCount = 0;
int currentRowCount = 0;
private void BuildDocument()
{
//Create new document
Document doc = new Document();
//Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
//traverse through table cells
for (int rowCount = 0; rowCount < tbl1.RowCount; rowCount++)
{
for (int colCount = 0; colCount < tbl1.ColumnCount; colCount++)
{
//Specify default width of cell
builder.CellFormat.Width = 100;
//Spacify height
builder.RowFormat.Height = 100;
//specify border style
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
//Generate table.
builder.InsertCell();
//Text will be at the top of cell
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Top;
//Text wil be in the left corner
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
currentRowCount = rowCount;
currentColCount = colCount;
//insert text
RichTextBox rtfControl = (RichTextBox)tbl1.GetControlFromPosition(colCount, rowCount);
builder.Write(rtfControl.Text);
//Create regex
Regex regex = new Regex(rtfControl.Text);
//Replace placeholder with rtf using ReplaceEvaluator
doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceEvaluatorForm), false);
}
//End row
builder.EndRow();
//End table
builder.EndTable();
}
doc.Save(@"C:\Documents and Settings\abdul.ali\Desktop\DocumentBuild.doc");
}
private ReplaceAction ReplaceEvaluatorForm(object sender, ReplaceEvaluatorArgs e)
{
//Get MatchNode
Paragraph parentParagraph = e.MatchNode.ParentNode as Paragraph;
RichTextBox rtfControl = (RichTextBox)tbl1.GetControlFromPosition(currentColCount, currentRowCount);
string rtfString = rtfControl.Rtf;
rtfControl = null;
//Convert string to byte array
byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
//Sreate stream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
Document rtfDoc = new Document(rtfStream);
rtfStream.Close();
//Insert rtd document into destination document
InsertDocument(parentParagraph, rtfDoc);
rtfDoc = null;
//Return Replace action (remove palaceholder)
return ReplaceAction.Replace;
}
Please advice to get the correct formatted data on the final Doc. Is this because of improper closing of rows and tables?
Regards,
Ali