FieldNode

Hi,

can you say me why, when I insert cloned Nodes like in an example code, NodeSeparator and NodeEnd is not where is DocumentBuilder moved? You can see in DocumentExplorer that they Nodes are on the end paragraph not where is DocumentBuilder been on that moment, on node(you see in code I always move DocumentBuilder on Node who be cloned and insert)?

public class FieldNodes
{
    protected List getDocumentParagraphs(com.aspose.words.Document document) throws Exception
    {
        final List paragraphList = new ArrayList();
        document.accept(new DocumentVisitor()
        {
            
            @Override
            public int visitParagraphStart(Paragraph paragraph)
                    throws Exception
            {
                paragraphList.add(paragraph);
                return super.visitParagraphStart(paragraph);
            }
        });
        return paragraphList;
    }
    
    @Test
    public void test() throws Exception
    {
        String path = "/home/emisia/Desktop/bojan.doc";
        Document document = new Document(path);
        java.util.List list = getDocumentParagraphs(document);
        DocumentBuilder builder = new DocumentBuilder(document);
        for (Paragraph paragraph: list)
        {
            NodeCollection collection = paragraph.getChildNodes();
            for (Node node: collection)
            {
                builder.moveTo(node);
                builder.insertNode(node.deepClone(true));
            }
        }
        document.save("/home/emisia/Desktop/bojan11.doc");
    }
}

Thanks,
Bojan

Hi
Thanks for your request. In your code before inserting the cloned node, you move cursor to this node. So the cloned node is always inserted just before the current node. If you need to insert the cloned nodes at the end of the current paragraph, your code should look like this:

String path = "C:\\Temp\\bojan.doc";
Document document = new Document(path);
java.util.List <Paragraph> list = getDocumentParagraphs(document);
DocumentBuilder builder = new DocumentBuilder(document);
for (Paragraph paragraph: list)
{
    Node[] collection = paragraph.getChildNodes().toArray();
    for (Node node: collection)
    {
        builder.moveTo(paragraph);
        builder.insertNode(node.deepClone(true));
    }
}
document.save("C:\\Temp\\out.doc");

Hope this helps.
Best regards,

Thanks,

but my problem is that the some of Nodes are not inserted before the current node, please see the attached document(second) in DocumentExplorer and you will see that the all Nodes FieldSeparator and FieldEnd are not before the nodes who cloned.

All nodes FieldEnd and FieldSeparator are on the end of paragraph and that is a problem.

I want to cloned all nodes and that the every cloned node be just before the original node, just like my code is.

So my code is ok, you said “In your code before inserting the cloned node, you move cursor to this node. So the cloned node is always inserted just before the current node.” and I want this.

Thanks,
Bojan

Hi
Thank you for additional information. I am not sure what is the purpose of inserting cloned nodes right after the original node. However, regarding FieldSeparator and FieldEnd nodes. They are moved to the end upon saving the document. Aspose.Words does this to make a document valid. Two FieldSeparator nodes following each other can cause incorrect processing of fields. Because you will not be able to determine which of field the separator belongs to.
Best regards,