We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

System.ArgumentException: Cannot insert a node of this type at this location

Hi,
i have a word document, containing a template for a list. This document is loaded and then i extract each line of the list. With this i want to be able to take one line (not depending on with list level) and add it to a document.
My aim is to build any list, with cray structures, defined in the template.
This is my start:

_listLevel = 0;
_listTemplate = new Collection();
using (FileStream templateStream = new FileStream(listTemplate, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
    _partToInsert = new Document(templateStream);
    Section section = (Section)_partToInsert.ChildNodes[0];
    Body body = (Body)section.ChildNodes[0];
    foreach (Paragraph para in body.ChildNodes)
    {
        _listTemplate.Add(para);
    }
    _partToInsert.RemoveAllChildren();
}
_inserter = new DocumentBuilder(_partToInsert);
_inserter.InsertNode(_listTemplate[_listLevel]);

Idea is to load the complete Template into the _partToInsert Document, then extract the
single lines of it. After i got every line, i clear _partToInsert and use
DocumentBuilder.InsertNode() to add the line i need at this point. I repeat this step
for every line i want to add independend of list level.
But when calling InsertNode() method i get the System.ArgumentException telling me that
i cannot insert this node at this location.
Why is this the case and how can i fix it?
Greetings,
Crazkur
PS: i attached the ListTemplate i use

Hi Crazkur,
Thanks for your inquiry. Please use the following code to meet this requirement.

int _listLevel = 0;
Collection _listTemplate = new Collection();
Document _partToInsert = new Document(MyDir + @"ListTemplate.docx");
Section section = (Section)_partToInsert.ChildNodes[0];
Body body = (Body)section.ChildNodes[0];
foreach (Paragraph para in body.ChildNodes)
{
    _listTemplate.Add(para);
}
_partToInsert.RemoveAllChildren();
_partToInsert.EnsureMinimum();
_partToInsert.FirstSection.Body.InsertAfter(_listTemplate[_listLevel], _partToInsert.FirstSection.Body.FirstParagraph);
_partToInsert.Save(MyDir + @"17.2.0.docx");

Hope, this helps.
Best regards,

Hi,

thanks again for your help Awais
So basicly its not possible to add those rows using DocumentBuilder?
In this case: How can i append a row to my existing list?

Greetings,

Crazkur

Hi,

Thanks for your inquiry. The problem occurs because DocumentBuilder.InsertNode method can insert a “text level” node inside the current paragraph before the cursor. But the node your truing to insert is a Paragraph which is a Block Level node like Table.

Best regards,

Okay, now i understand a little more how this node system is working.
Now to insert that row:
If its a paragraph, does that means, i need to split it up to “text level” nodes when i want to add it?
Or is there any better possibility?

Greetings,
Crazkur

Hi,
Thanks for your inquiry. Your Paragraphs have Field objects. We have logged an issue to provide the following overload of DocumentBuilder.InsertField method:
public Field InsertField(Field);
The ID of this issue is WORDSNET-14877. Your thread has also been linked to the appropriate issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.
After the resolution of above issue you can meet this requirement like this:

int _listLevel = 0;
Collection _listTemplate = new Collection();
Document _partToInsert = new Document(MyDir + @"ListTemplate.docx");
Section section = (Section)_partToInsert.ChildNodes[0];
Body body = (Body)section.ChildNodes[0];
foreach (Paragraph para in body.ChildNodes)
{
    _listTemplate.Add((Paragraph)para.Clone(true));
}
_partToInsert.RemoveAllChildren();
DocumentBuilder _inserter = new DocumentBuilder(_partToInsert);
if (_listTemplate[_listLevel].NodeType.Equals(NodeType.Paragraph))
{
    foreach (Node node in _listTemplate[_listLevel].GetChildNodes(NodeType.Any, true))
    {
        if (node.NodeType.Equals(NodeType.FieldStart))
        {
            Field field = ((FieldStart)node).GetField();
            _inserter.InsertField(field); // this won’t work for now
                                            // Skip all nodes until we reach a node after this Field’s ‘End’ node
        }
        _inserter.InsertNode(node);
    }
}
_partToInsert.Save(MyDir + @"17.2.0.docx");

Best regards,