RTF

Apr 7, 2009 at 3:09 PM

Thank you for the helpful merge functions.

Added an example below for adding support for using RTF text. Feel free to use and improve it. It's almost a copy of an example from Aspose homepage.
(http://www.aspose.com/community/forums/thread/158010/insert-rtf-text-into-a-table-using-executewithregions.aspx)

Keep up the good work
Jan S.

Changes in AdvancedMailMerge.cs:
SetMergeField:L415:
if (mergeField.Name.StartsWith("Image:", StringComparison.CurrentCultureIgnoreCase))
{
this.SetImageMergeField(mergeField, mergeValue);
}
else if (!string.IsNullOrEmpty(mergeValue) && mergeValue.Contains(\\rtf))
{
this.SetRTFMergeField(mergeField, mergeValue);
}
else
{
this.SetTextMergeField(mergeField, mergeValue);
}

private void SetRTFMergeField(MergeField mergeField, string mergeValue)
{
if (!string.IsNullOrEmpty(mergeValue) && mergeValue.Contains(\\rtf))
{
Node parentNode = mergeField.Nodes.ElementAt(3).ParentNode;
// Remove field nodes no longer needed
mergeField.Nodes.ElementAt(0).Remove();
mergeField.Nodes.ElementAt(1).Remove();
mergeField.Nodes.ElementAt(2).Remove();
mergeField.Nodes.ElementAt(3).Remove(); // Text
mergeField.Nodes.ElementAt(4).Remove();

// RTF documents uses ASCII encoding.
// Find ANSI codepage (\\ansicpg1252).
string CodePage = "", WindowsCodePage = "";
Regex re = new Regex(@"\\ansicpg\d+");
 
Match m = re.Match(mergeValue);
if (m.Success)
{
     // Get codepage (1252)
    Regex reCodepage = new Regex(@"\d+");
    Match mCodePage = reCodepage.Match(m.Value);
    if (mCodePage.Success)
    {
    CodePage = mCodePage.Value;
    }
}
 
byte[] rtfBytes = null;
try
{
if (!string.IsNullOrEmpty(CodePage))
{
WindowsCodePage = "Windows-" + CodePage; // "Windows-1252"
rtfBytes = Encoding.GetEncoding(WindowsCodePage).GetBytes(mergeValue);
}
}
catch (System.ArgumentException)
{
// GetEncoding failed
}
finally
{
if (rtfBytes== null || rtfBytes.Length == 0)
{
rtfBytes = Encoding.UTF8.GetBytes(mergeValue);
}
}
 
// Insert RTF document into destination document
MemoryStream rtfStream = new MemoryStream(rtfBytes);
Document rtfDoc = new Document(rtfStream);
InsertDocument(parentNode, rtfDoc);
}
}

public void InsertDocument(Node insertAfterNode, Document srcDoc)
{
// Make sure that the node is either a pargraph or table.
if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
 (!
insertAfterNode.NodeType.Equals(NodeType.Table)))
{
throw new ArgumentException("The destination node should be either a paragraph or table.");
}

// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;

// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);

// Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// Let's skip the node if it is a last empty paragarph in a section.
if (srcNode.NodeType.Equals(NodeType.Paragraph))
{
Paragraph para = (Paragraph)srcNode;

if (para.IsEndOfSection && !para.HasChildNodes)
continue;
}

// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.ImportNode(srcNode, true);

// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode; 
}
}
}

Coordinator
Apr 14, 2009 at 3:28 AM
Thanks for the feedback and the code. I will integrate this into the next release.

Cheers,
Anthony.
Coordinator
May 19, 2009 at 7:28 AM

Hi, could you please attach your code to an issue in the Issue tracker?

Thanks,
Anthony.

May 19, 2009 at 11:32 AM

Hi

The code file is attached now.

Regards
Jan