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

Free Support Forum - aspose.com

[java] insert doc at bookmark- section number

Hello,

I try to insert a document at a bookmark location of a main document. It’s ok but if the inserted document contains a section number which must follow existing one in main doc, it’s not ok. See example :

main doc :

1. part one
2. part two
2.1 part two-one
bookmark1
2.2 part two-two
3. part three

sub doc :
1.1 sub part

after insertion :
1. part one

2. part two

2.1 part two-one
1.1 sub part

2.2 part two-two

3. part three

but I want :
1. part one

  1. part two

2.1 part two-one
2.2 sub part

2.3 part two-two

  1. part three

    My insertion code (copy from your forum) :
    private void insertDocument(Node node, Document doc) throws Exception
    {
    CompositeNode parentNode = node.getParentNode();
    while (true)
    {
    if (parentNode == null)
    {
    throw new Exception(“Document cannot be inserted after the specified node.”);
    }
    if (parentNode instanceof Story || parentNode instanceof Cell || parentNode instanceof Shape)
    {
    break;
    }
    node = parentNode;

    parentNode = node.getParentNode();
    }

    int index = node.getParentNode().getChildNodes().indexOf(node);
    Document dstDoc = node.getDocument();
    Section insertedSection;

    for (Node section : doc.getSections())
    {
    insertedSection = (Section) dstDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

    for (Node insertedNode : insertedSection.getBody().getChildNodes())
    {
    // Do not insert node if it is a last empty paragarph in the section.
    if (insertedNode instanceof Paragraph && insertedNode == ((Section) section).getBody().getLastChild() && insertedNode.toTxt().equals(""))
    {
    break;
    }
    parentNode.getChildNodes().insert(++index, insertedNode.deepClone(true));
    }
    }
    }

    A bug ?

Hi

Thank you for your inquiry. No, this in not bug. The insertDocument method appends content of each section of source document into destination document. If you update fields then you will see this. You can try using the following code. Unfortunately you should update section numbers manually.

public static void main(String[] args) throws Exception {

Document doc1 = new Document("in1.doc");

Document doc2 = new Document("in2.doc");

insertDocument(doc1.getRange().getBookmarks().get("test"), doc2);

doc1.save("out.doc");

}

private static void insertDocument(Bookmark bookmark, Document srcDoc) throws Exception

{

Document dstDoc = bookmark.getBookmarkStart().getDocument();

DocumentBuilder builder = new DocumentBuilder(dstDoc);

builder.moveToBookmark(bookmark.getName());

Section current = builder.getCurrentSection();

builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);

srcDoc.getFirstSection().getPageSetup().setSectionStart(BreakType.SECTION_BREAK_CONTINUOUS);

for (Node section : srcDoc.getSections())

{

Section insertedSection = (Section) dstDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

dstDoc.insertAfter(insertedSection, current);

current = insertedSection;

}

}

I hope that this will help you.

Best regards.

your code insert a page break before each document insertion, and it not modify section number. For the moment I solve my problem by using autonum word fields, because they are automatically updated after insertion.

Hi

It is very nice that you found solution independently. As I said Aspose.Words doesn’t update fields. You should update fields manually and then you will see difference. (Ctrl+A then push F9)

Best regards.