Format of a insert rtf field with mail merge

Sorry, it is unclear from your code where and when the rtf is inserted in the cell.

OK miklovan,I will try to lay it out.

firstly insert the cell and get reference

if(rtf is valid RTF)
    Cell insertedCell = builder.InsertCell();

Now insert rtf

InsertRtf with

public void InsertRtf(DocumentBuilder builder, string rtf)
{
    MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(rtf));
    Document rtfDoc = new Document(stream);
    Paragraph paragraph = builder.InsertParagraph();
    InsertDocument(paragraph, rtfDoc);
}

//using your provided code
public void InsertDocument(Node insertAfterNode, Document srcDoc)
{
    // We need to make sure that the specified node is either pargraph or table.
    if (!((insertAfterNode.NodeType == NodeType.Paragraph) || (insertAfterNode.NodeType == NodeType.Table)))
        throw new ArgumentException("The destination node should be either 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)
        {
            // Do not insert node if it is a last empty paragarph in the section.
            Paragraph para = srcNode as Paragraph;
            if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
                break;
            // 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;
        }
    }
    // Ensure that destination story ends with a paragraph.
    if (!(dstStory.LastChild is Paragraph))
        dstStory.AppendChild(new Paragraph(dstStory.Document));
}
//want to insert after rtf in one case
builder.MoveTo(insertedCell.LastParagraph.LastChild);
builder.Writeln( afterRTFTextOnlyString );

This is all I am trying to do here.

Thank You

Andrew

Ok, now I can see that this approach does not always work. Please try the following code:

builder.MoveToParagraph(insertedCell.LastParagraph.ParentNode.IndexOf(insertedCell.LastParagraph), -1);

It works in several tests that I have made up. Please let me know if it worked for you.

Best regards,

Hello miklovan,I have tried your code snippett today,and it doesn`t work for me.

Here the attachment shows that it inserts in the beginning of the document not at the end.

The text Insert is “Score:” this needs to be inserted in the lower part after a rtf insert of text.

Thank you

Also look at my adjacent submission document it has what I am working with currently.

Hello miklovan,Also I had some success with the code of :

builder.MoveTo(insertedCell.LastParagraph.LastChild);

Although it does not position after the rtf text but just before the end.

The text to insert is Score: pfxScorenum.

What changes need to be done to make the text sit after the rtf always.

Thanking you

Andrew

Hi Andrew,

I will try to find the best approach for you in several hours.

Hello Miklovan,This is code I am using to position and to insert the rtf text. Here is also the library code to insert the rtf. I have tried various methods for positioning the score text after the rtf but nothing has been totally possible here.

Thank You

Andrew

Hi Andrew,

Sorry for delay and thank you for additional info you’ve provided. However, it is not enough to reproduce the issue because there is too much code to accommodate in order to have it executed. Could you please compose a simple console or class library project that simulates the task? In other words, it should build a table, insert a sample RTF fragment into a cell and try to insert a text right after the RTF - is it your goal? It would greatly simplify replication and allow us to fully understand what you need to achieve.

Besides, is it acceptable that the text after the inserted RTF resides in a separate (next) paragraph? Please try the following approach then:

Cell insertedCell = builder.InsertCell();
builder.Write("RTF text");
Paragraph para = new Paragraph(doc);
insertedCell.InsertAfter(para, insertedCell.LastParagraph);
builder.MoveTo(para);
builder.Writeln("This text should be inserted at the end of the cell.");