Insert subdoc in table starts on new line (creates empty line above inserted text)

Hi,
Inserting a sub document inside my main document works well in situations where I point to a bookmark for example. However if I insert a subdoc inside a table it always inserts a CR/LF first and inserts the text of the subdoc on the second line in my table Row. This is not what I would like to see.
I’ve used the insert funtion from the Aspose Site and only modyfied the formatting line keepsource/use destination.
Below the code that I use.
Any help is appriciated,
Ron
This is how I call the function

InsertDocument(mainTable.LastRow.FirstCell.FirstParagraph, subDoc, ImportFormatMode.UseDestinationStyles);
///
/// Inserts content of the external document after the specified node.
// Section breaks and section formatting of the inserted document are ignored.
///
/// Node in the destination document after which the content
/// should be inserted. This node should be a block level node (paragraph or table).
/// The document to insert.
public void InsertDocument(Node insertAfterNode, Document srcDoc, ImportFormatMode DocFormat)
{
    // StreamWriter WORDLOG = new StreamWriter(new FileStream(@"C:\DocRunner\drLog\insertdoclog.log", System.IO.FileMode.Append));
    // 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.
    // Set section start of the first section of the source document.
    // RL This line was modified by me to provide more flexibility.
    // NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);
    NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, DocFormat);
    // 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;
        }
    }
}

Hi Ron,

Thanks for your inquiry. This is expected because InsertDocument inserts a sub document after Paragraph or Table node. In your case, document was inserted after the first Paragraph of table Cell. You can just remove this paragraph after inserting a document:

InsertDocument(mainTable.LastRow.FirstCell.FirstParagraph, subDoc, ImportFormatMode.UseDestinationStyles);
mainTable.LastRow.FirstCell.FirstParagraph.Remove();

Please let me know if you need more assistance, I will be glad to help you.
Best regards,

Hi,
Thanks for the fast response. One more little thing when I use the line to remove the paragraph I have some trouble a few lines further where I check if I have to insert a new row in the table or jump to the next cell. Because we just deleted the current paragraph I’m getting an error “Object reference not set to an instance of an object.” on the line

if (currentCell.NextSibling != null)

because CurrentParagraph was not found.
How do I select A paragraph in the cell I just inserted the subdoc so currentParagraph has a value ?
Thanks,
Ron

Aspose.Words.Tables.Cell currentCell = (Aspose.Words.Tables.Cell) builder.CurrentParagraph.GetAncestor(NodeType.Cell);
// Check whether there is next cell in the current row
if (currentCell.NextSibling != null)
{
    // If so move document builder cursor to the next cell
    builder.MoveTo((currentCell.NextSibling as Aspose.Words.Tables.Cell).FirstParagraph);
}
// Otherwise move cursor to the next row

Hi,
Forget about my last question. I solved it by adding the following functionality.

if (mainTable.LastRow.FirstCell.FirstParagraph != mainTable.LastRow.FirstCell.LastParagraph)
{
    // Only remove the first blanc par if there was a new par insserted (not a blanc doc)
    mainTable.LastRow.FirstCell.FirstParagraph.Remove();
    builder.MoveTo(mainTable.LastRow.FirstCell.LastParagraph);
}
Anyway thanks
for the fast response,

Ron

Hi Ron,

It is perfect that you already resolved the problem. Actually you can get current cell before removing a paragraph. In this case you will not get NullRefference exception.
Please let me know if you need more assistance, I will be glad to help you.
Best regards,