I created an RTF file using Word 2007. I want to insert merge fields that can be parsed and merged with database info at a later stage. The document contained the phrase 'D开发者_如何学编程ear [salutation] [surname] How are you?'. I then edited the [surname] part to say [lastname]. If I now view the rtf source it contains loads of unwanted characters as follows:
Dear [salutation] [}{\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid6575321 last}{\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid2040086 name}{\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid2434881 ]}{\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid2040086 \r\n\par How are you?
This means that when I try merging then the [lastname] is too mangled to be found for merging. Does anyone know what's going on here, and how I can prevent Word from embedding all this unwanted stuff? Thanks.
In the end I used the System.Windows.Forms.RichTextBox to solve the problem, as below:
public class RTF
{
/// <summary>
/// Merge the merge data with the target RTF document
/// </summary>
/// <param name="byteStream">Original RTF document</param>
/// <param name="mergeDatatable">Merge Data (as per sproc_GetDocumentMergeData)</param>
/// <returns>String representation of the RTF document</returns>
public static string GetMergedRTFDocument(byte[] byteStream,DataTable mergeDatatable)
{
System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox();
MemoryStream stream = new MemoryStream(byteStream);
rtb.LoadFile(stream, System.Windows.Forms.RichTextBoxStreamType.RichText); // Use for RTF
int selstart = 0;
string findTerm = "";
DataRow mergerow = mergeDatatable.Rows[0];
foreach (DataColumn col in mergeDatatable.Columns)
{
findTerm = "[" + col.ColumnName + "]";
selstart = rtb.Find(findTerm);
while (selstart > -1)
{
rtb.SelectionStart = selstart;
rtb.SelectedText = mergerow[col].ToString();
selstart = rtb.Find(findTerm);
}
}
return rtb.Rtf;
}
}
You can use Regular expression expressions to complete this merge process. I have created a blog post about how this can be done.
Using regular expression to merge database content into Rich Text format template documents
An example written in PHP can be found on Github: https://github.com/olekrisek/PHP_RTF_RegExMerge
精彩评论