Free Support Forum - aspose.com

Rtf format in word

Hi,

i do have rtf format stored in the database, through some text entered in an editor.

Now i should bring that rtf format into merge field with original format.

I mean i have text Hi this is test1

and it is stored in data base in this format

{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\colortbl;\\red0\\green0\\blue0;}{\\fonttbl\n{\\f0\\fswiss\\fcharset1252 Times New Roman;}\n}{\\*\\generator CuteEditor 5.0;}\\viewkind4\\uc1\\pard\\cf1\\f0\\fs24\\qj\\b0\\i0\\ulnone Hi \\cf1\\f0\\fs24\\qj\\b1\\i0\\ulnone this \\cf1\\f0\\fs24\\qj\\b1\\i1\\ulnone is \\cf1\\f0\\fs24\\qj\\b1\\i1\\ul test1}"

So when retreving back to word with a merge field i should get Hi this is test1

but now i am getting the text same as in database field.

So waht to do for that.

Hi

Thanks for your interest in Aspose products. You should create rtf document and then insert this document into your template. See the following code.

public void TestMailMergeInsertRtf_102888()

{

Document doc = new Document(@"258_102888_raviteja\in.doc");

string[] names = { "Field1" };

string[] values = { "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\colortbl;\\red0\\green0\\blue0;}{\\fonttbl\n{\\f0\\fswiss\\fcharset1252 Times New Roman;}\n}{\\*\\generator CuteEditor 5.0;}\\viewkind4\\uc1\\pard\\cf1\\f0\\fs24\\qj\\b0\\i0\\ulnone Hi \\cf1\\f0\\fs24\\qj\\b1\\i0\\ulnone this \\cf1\\f0\\fs24\\qj\\b1\\i1\\ulnone is \\cf1\\f0\\fs24\\qj\\b1\\i1\\ul test1}}" };

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_102888);

doc.MailMerge.Execute(names, values);

doc.Save(@"258_102888_raviteja\out.doc");

}

void MailMerge_MergeField_102888(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "Field1")

{

// Use DocumentBuilder to navigate to merge field with the specified name.

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.DocumentFieldName);

//load rtf string into stream

MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(e.FieldValue.ToString()));

//create document

Document rtfDoc = new Document(stream);

//close stream

stream.Close();

// Content of the specified document are inserted after the paragraph, containing the merge field.

InsertDocument(builder.CurrentParagraph, rtfDoc);

// If the paragraph, containing merge field, does not contain anything else - delete it.

if (builder.CurrentParagraph.ToTxt().Trim() == "")

builder.CurrentParagraph.Remove();

// The field value text is not needed anymore, eraze it.

// Otherwise, it will be inserted at merge field location too.

e.Text = string.Empty;

}

}

The InsertDocument method you can find here.
http://www.aspose.com/Products/Aspose.Words/Api/InsertOneDocumentintoAnotheratAnyLocation.html

Also see the following link.
http://www.aspose.com/Products/Aspose.Words/Api/InsertOneDocumentintoAnotherduringMailMerge.html

I hope that this will help you.

Please let me know if you would like to know something else.

Best regards.

Hi how can be mergeFieldEventards can be highlited. imean what namespace should we add. Please let me know that

Sorry got the Mergefieldeventargs, but what abt the MemoryStream, what should we include

Hi

Thanks for your inquiry. You should add this line.

using System.IO;

Or use the following line

System.IO.MemoryStream stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(e.FieldValue.ToString()));

Best regards.