Inserting Rtf into a specific place in a document - font problem

Hi

This is the code that I am using to insert Rtf into another document at a specific place but then change some of the settings like bold or italic of the paragraph that is inserted:

public void InsertRtfDescription(DocumentBuilder builder, string description)
{
    MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(description));
    Document rtf = new Document(stream, null, LoadFormat.Rtf, null);

    foreach (Node srcNode in rtf.FirstSection.Body)
    {
        Node importedNode = builder.Document.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
        builder.CurrentParagraph.ParentNode.InsertBefore(importedNode, builder.CurrentParagraph);
    }
}

What I would like to be able to do after inserting the rtf, or even before, is to be able to format the rtf with bold or italic text for example, but still retain the fonts and font sizes that it has been set as (that’s why I am using KeepSourceFormatting above).

I have tried the following:

  1. setting the style of the rtf document but I needed to do that in a DocumentBuilder and could not find a way that it would save the style.
  2. setting the startParagraph, inserting the rtf and then getting the currentParagraph but I noticed that because I am using InsertBefore, the startParagraph and currentParagraph setting is equal, so I can’t loop through the paragraphs and set the setting I need.
  3. tried using InsertAfter but it moves all the rtf Inserts to the end of the document which is not what I want either.
  4. also tried using AppendChild but it does the same thing as 3 above.

I’m not sure if I am missing something really simple?
Any help would be greatly appreciated.

Thanks in advance.

Kammie

Hi
Thanks for your inquiry. Maybe you can use code as below:

public void InsertRtfDescription(DocumentBuilder builder, string description, bool isBold, bool isItalic)
{
    MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(description));
    Document rtf = new Document(stream, null, LoadFormat.Rtf, null);
    Node currentNode = builder.CurrentParagraph;
    foreach (Node srcNode in rtf.FirstSection.Body)
    {
        Node importedNode = builder.Document.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
        if (importedNode.NodeType == NodeType.Paragraph)
        {
            (importedNode as Paragraph).ParagraphFormat.Style.Font.Bold = isBold;
            (importedNode as Paragraph).ParagraphFormat.Style.Font.Italic = isItalic;
        }
        currentNode.ParentNode.InsertBefore(importedNode, currentNode);
        currentNode = importedNode;
    }
}

Hope this helps.
Best regards.

Hi Alexey

Thanks for the above code, it definitely works better than anything I’ve tried thus far.

I’m just having 1 more little problem now with this. Sometimes, the rtf that is created already has a word in the middle of the paragraph in Bold.
eg.

This word is in bold. This is not in bold again.

Obviously what this then does is splits the paragraph styles into 2 respectively. One style that is bold and the other that has a bold format in it. What your code seems to do is only change the font of the 1st half of the sentence, leave the first bold word as bold and then continues importing the 2nd half of the paragraph unbolded. ie. it shows as follows:
This word is in bold. This is not in bold again.

I’ve tried editing your code as follows inside of the IF statement:

for (int i = 0; i < (importedNode as Paragraph).ParagraphFormat.Style.Styles.Count; i++)
{
     (importedNode as Paragraph).ParagraphFormat.Style.Styles[i].Font.Bold = true;
}

but realised that this changes the entire documents font to bold.

Then tried the following:

foreach (Style style in (importedNode as Paragraph))
{
    style.Font.Bold = true;
}

but this again changes the font of the entire document and doesn’t even insert the rtf text into the 2nd document.

I’ve tried a couple of other things which I can’t remember anymore either, but it either didn’t do anything, did what the style loop above does or did what your code did.

Any help on this would be appreciated again.

Thanks in advance.

Regards,
Kammie

Hi
Thanks for your inquiry. Paragraph can contain multiple runs with different format. You can loop through all runs and change formatting. See the following code for example:

public void InsertRtfDescription(DocumentBuilder builder, string description, bool isBold, bool isItalic)
{
    MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(description));
    Document rtf = new Document(stream, null, LoadFormat.Rtf, null);
    Node currentNode = builder.CurrentParagraph;
    foreach (Node srcNode in rtf.FirstSection.Body)
    {
        Node importedNode = builder.Document.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
        if (importedNode.IsComposite)
        {
            // Get collection of runs from current node
            NodeCollection runs = (importedNode as CompositeNode).GetChildNodes(NodeType.Run, true);
            // Loop through all runs in the current node and change font
            foreach (Run run in runs)
            {
                run.Font.Bold = isBold;
                run.Font.Italic = isItalic;
            }
        }
        currentNode.ParentNode.InsertBefore(importedNode, currentNode);
    }
}

Hope this helps.
Best regards.