Aspose.words adds a paragraph into new doc

Hello,
when inserting a bookmark-text into a NEW(empty) document, a paragraph is always added before the text.
Any idea whats happening here?
Thx in advance,
Michael

Hello!
Thank you for your interest in Aspose.Words.
Note that an empty document contains one section with one paragraph in it. This paragraph contains the only section break character. You probably insert contents after it, not before. Please try the following:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello World!");
doc.Save("Hello.doc");

This way you can insert bookmark start and bookmark end nodes as well. Please let me know if this helps or provide your code snippet if not.
Regards,

Hello Klepus,
thank you for the quick response… sorry for my late one…
I checked and as you said, the content is inserted after it. I added the code here-under. Can you tell me how i can define the destinationNode so that the insert is before it?
Thanx a lot,
Michael

Document tmpDoc = new Document();
CompositeNode dstNode = tmpDoc.getFirstSection().getBody();
protected void appendBookmarkContent(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode)
{
    try
    {
        Paragraph startPara = (Paragraph)srcBookmark.getBookmarkStart().getParentNode();
        Paragraph endPara = (Paragraph)srcBookmark.getBookmarkEnd().getParentNode();
        if (startPara != null && endPara != null)
        {
            Node endNode = endPara.getNextSibling();
            for (Node curNode = startPara; curNode != endNode; curNode = curNode.getNextSibling())
            {
                Node newNode = importer.importNode(curNode, true);
                dstNode.appendChild(newNode);
            }
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Forgot something dstNode has to be the last section, because multiple inserts can happen and they have to come one after another…so

CompositeNode dstNode = tmpDoc.getLastSection().getBody();

Michael

Hi
Thanks for your request. You can use insertBefore method. For example see the following code:

protected void appendBookmarkContent(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode)
{
    try
    {
        Paragraph startPara = (Paragraph)srcBookmark.getBookmarkStart().getParentNode();
        Paragraph endPara = (Paragraph)srcBookmark.getBookmarkEnd().getParentNode();
        Node refNode = dstNode.getFirstChild();
        if (startPara != null && endPara != null)
        {
            Node endNode = endPara.getNextSibling();
            for (Node curNode = startPara; curNode != endNode; curNode = curNode.getNextSibling())
            {
                Node newNode = importer.importNode(curNode, true);
                dstNode.insertBefore(newNode, refNode);
            }
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Hope this helps.
Best regards.

Hello,
first of all, thanks a lot for your help… the paragraph is well inserted before the paragraph now… but when i look at the bookmark that is appended there are always 2 paragraphs at the end now. This paragraph is added when it is copied to the new document(see above) and is not coming from the source bookmark…
Any way to get loose of this?
Thanks,
Michael

I added the output of the new document in attachment…
Greetz,
Michael

Hi
Thanks for your inquiry. Maybe this occurs because BookmarkEnd is placed at the beginning of the next paragraph. You can easily solve this problem using the following method.

protected void appendBookmarkContent(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode)
{
    try
    {
        Paragraph startPara = (Paragraph)srcBookmark.getBookmarkStart().getParentNode();
        if (srcBookmark.getBookmarkStart().equals(startPara.getLastChild()))
            startPara = (Paragraph)startPara.getNextSibling();
        Paragraph endPara = (Paragraph)srcBookmark.getBookmarkEnd().getParentNode();
        if (srcBookmark.getBookmarkEnd().equals(endPara.getFirstChild()))
            endPara = (Paragraph)endPara.getPreviousSibling();
        Node refNode = dstNode.getFirstChild();
        if (startPara != null && endPara != null)
        {
            Node endNode = endPara.getNextSibling();
            for (Node curNode = startPara; curNode != endNode; curNode = curNode.getNextSibling())
            {
                Node newNode = importer.importNode(curNode, true);
                dstNode.insertBefore(newNode, refNode);
            }
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Hope this helps.
Best regards.

Hello there,
proposed solution didn’t remove the second paragraph, any other idea’s??
Thx,
Michael

Hi
Thanks for your inquiry. Could you please attach your source document? Do you mean the empty paragraph at the end of generated document? If so then note that empty document already contains one empty paragraph.
Best regards.