Dump rich text formatted data from sql server into Doc template

Hello,
I have created a template in microsoft word which is having a formatted table. Our requirement is to get the data from a database table and put it into the cells of that word template using aspose words. Our data is rich text formatted data, please see the attached .rtf file. We are new to Aspose Words so we dont know how to use it efficiently. Can you please tell us how to add the template and dump the data from stored procedure in sql and save it as a pdf. We will appreciate your help.
templateInAspose.zip (56.3 KB)

@spathak,

I believe, you will be able to meet this requirement by using the following workflow:

LoadOptions opts = new LoadOptions();
opts.LoadFormat = LoadFormat.Rtf;
// Load RTF bytes into Document
byte[] bytes = File.ReadAllBytes(MyDir + @"templateInAspose\VeryRichText.rtf");
MemoryStream stream = new MemoryStream(bytes);
Document srcDoc = new Document(stream, opts);
// Load destination Document
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
// Move cursor to any valid position
builder.MoveToDocumentEnd();
// Insert RTF document
builder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
// Save the final Document
dstDoc.Save(MyDir + @"templateInAspose\18.3.docx");

@awais.hafeez

Thank you so much for that help. At least it gave us some jump start. But we have a.doc template which consist of a table. We need to insert that rich text in that table last cell. Can you please tell us how to do that? We are following the code that you have provide.

Thank you

@spathak,

Please try using the following code:

LoadOptions opts = new LoadOptions();
opts.LoadFormat = LoadFormat.Rtf;
// Load RTF bytes into Document
byte[] bytes = File.ReadAllBytes(MyDir + @"templateInAspose\VeryRichText.rtf");
MemoryStream stream = new MemoryStream(bytes);
Document srcDoc = new Document(stream, opts);
// Load destination Document
Document dstDoc = new Document(MyDir + @"templateInAspose\table.docx");
DocumentBuilder builder = new DocumentBuilder(dstDoc);
// Get Table
Table tab = (Table) dstDoc.GetChildNodes(NodeType.Table, true)[0];
tab.LastRow.LastCell.RemoveAllChildren();
tab.LastRow.LastCell.EnsureMinimum();
// Move cursor to last Cell
builder.MoveTo(tab.LastRow.LastCell.FirstParagraph);
// Insert RTF document
builder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
// Save the final Document
dstDoc.Save(MyDir + @"templateInAspose\18.3.docx");

@awais.hafeez
Thank you so much, I appreciate your efforts to help us to fulfill our requirements. Your code really worked for us really well.
I have one more question, Can you please tell us that how we can replicate the table in a same doc based on some value passed from UI. As our doc file has a table, but we will create more table dynamically to fill more data based on some conditions.
Can you please suggest how can we achieve this?

Thank you

@spathak,

I think, you can meet this requirement by using the ‘mail merge’ and ‘mail merge with regions’ features of Aspose.Words. Please refer to the following articles:

About Mail Merge
How to Execute Mail Merge with Regions

@awais.hafeez

Thank you so much for the help. Instead of mail merge I found table.clone() option much suitable for me.
We are facing issues with page break. We want that all the content of one table should stay in one page. It should not split to other page. We also don’t want that all table to display on every other page. It should continue just after the other table but content of one table should remain on one page. For this we are using code :

AsposeWords.Document dstDoc = new AsposeWords.Document(new MemoryStream(Template));
Table table = (Table)dstDoc.GetChild(AsposeWords.NodeType.Table, 0, true);
Table ClonedTable = (Table)table.Clone(true);
int loopCounter = 1;
int itemCounter = 8;
For loop
{
    // Some code to add data to the cells in a table
    if (loopCounter < itemCounter)
    {
        AsposeWords.Paragraph parag = new AsposeWords.Paragraph(dstDoc);
        parag.ParagraphFormat.KeepWithNext = true;

        table.ParentNode.InsertAfter(parag, table);
        table = (Table)ClonedTable.Clone(true);
        parag.ParentNode.InsertAfter(table, parag);

    }

    ++loopCounter;
}

This code is not working fine for us. Can you please help me with this issue.
Attach is the similar template.
template.zip (14.2 KB)

Thanks

@spathak,

Please refer to the following article:
Keeping Tables and Rows from Breaking across Pages

Hope, this helps.

@awais.hafeez ,

Thank you so much for your help, it worked for us. I appreciate your constant guidance.