Inserting new document at the end of the paragraph in Aspose Words for Java

Hi we are currently inserting a new document into the main document after the last line of the specified paragraph. So the new document is placed as a new paragraph.
We are trying to place the new document at the end of the specified paragraph, instead of the placing the new document a line after the end of paragraph. Can you suggest if there is a solution for this?
Following code is used for inserting word document which is provided by aspose team

private static void insertDocument(Node insertAfterNode, Document srcDoc) throws Exception
{
    // Make sure that the node is either a pargraph or table.
    if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) &
        (insertAfterNode.getNodeType() != NodeType.TABLE))
        throw new Exception("The destination node should be either a paragraph or table.");
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.getParentNode();
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
    SectionCollection sectionColl = srcDoc.getSections();
    // Loop through all sections in the source document.
    for (int sec = 0; sec <sectionColl.getCount(); sec++)
    {
        Section srcSection = (Section) sectionColl.get(sec);
        Body body = srcSection.getBody();
        NodeCollection nodeColl = body.getChildNodes();
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        for (int nodeCount = 0; nodeCount <nodeColl.getCount(); nodeCount++)
        {
            Node srcNode = (Node) nodeColl.get(nodeCount);
            // Let's skip the node if it is a last empty paragarph in a section.
            if (srcNode.getNodeType() == 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

Thanks for your request. I think, you can find a solution in here:
https://forum.aspose.com/t/114203
Please let me know in case of any issues, I will be glad to help you.
Best regards.

Hi Alexey,
Thanks for the response. When i tried to insert the document (temp1.doc) in the place holder in input.doc, the temp1 document is inserted at the start of the input document.
InsertDocumentAtBookmark("",input.doc,temp1.doc). I called this method with the following parameters. Temp1 is the document which iam trying to insert in the input doc.
I have attached the documents for you to look. Please let us know what changes should be made to resolve this.

Hi

Thank you for additional information. “” is not a bookmarks. This is just text placeholder. Please see the following link to learn more about bookmarks:
https://docs.aspose.com/words/net/working-with-bookmarks/
Best regards.

Hi
Thanks for the response. I was able to insert the document at the specified bookmark. But there is a issue when the document has bullets in it.

  1. I have two bullets in the document which i want to insert into the main document. After inserting with book mark, the second bullet doesnt appear in the main document.
  2. The bookmark is not deleted after inserting the document. Do we need to delete this bookmark manually or just replace it with empty space.

Please help to resolve these issues

Hi

Thanks for your request.

  1. Bullets are an option of paragraphs. This method practically removes the original paragraphs, it only copies content of paragraph to the destination document (See comments through the code). So you should decide how you would like to import paragraphs into the destination document: as they are in the source document or just import content of paragraphs to the appropriate paragraphs in the destination document.
  2. Why do you need text inside bookmark in your source document? Just remove it.

Best regards.

Hi Alex,
Thanks for the response.
We want to import the paragraphs into destination document as they are in the source document. The source document formatting should be maintained in the destination document.
Instead of book mark, is there a way to replace the source document into destination document with all the formats intact? (like replacing it with a place holder)
Thanks

Hi

Thank you for additional information. I think here is small conflict. On one hand, you would like to insert the source document as continuation of the last paragraph of the destination document. On another hand, you would like to preserve formation of source paragraphs (I am talking about the first and last paragraphs in the source document). So what should we do if the first paragraphs of the source document is a list item?
Anyways, I this the technique demonstrated by code, I suggested you to use, will allow you to achieve what you need.
Best regards.

Hi Alex,
InsertDocumentAtBookmark code which you gave is not working when there are bullets in source document. Last bullet in list is lost. We do want to preserve formatting of source doc when it is moved in destination doc. Is there a way to do it??
Thanks

Hi

Thank you for additional information. Ok, I will try to explain how both of methods (InsertDocument and InsertDocuemntAtBookmarks) work. First of all you should learn more about Aspose.Words Document Object Model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
let’s suppose that Word documents consist of paragraphs, paragraphs consist of runs, which represents text formatting. Let’s use simple XML as an example.

<document name="source" >
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
</document>

Paragraphs can have different options. For instance, paragraph can be a list item:

<document name="source" >
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph isListItem="true">
		<run>List item</run>
	</paragraph>
	<paragraph isListItem="true">
		<run>Second list item</run>
	</paragraph>
</document>

We need to insert such document after some of paragraphs in our destination document. For instance, our destination document looks like the following and we need to insert our source document after the highlighted paragraph:

<document name="destination" >
	<paragraph>
		<run>After this paragraph we need to insert document</run>
	</paragraph>
	<paragraph>
		<run>Text in destination document</run>
	</paragraph>
</document>

in this case we will use InsertDocument method. This method just copies paragraphs from the source document and insert them after the specified paragraph in the destination document. in our case this will look like the following:

<document name="output" >
	<paragraph>
		<run>After this paragraph we need to insert document</run>
	</paragraph>
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph isListItem="true">
		<run>List item</run>
	</paragraph>
	<paragraph isListItem="true">
		<run>Second list item</run>
	</paragraph>
	<paragraph>
		<run>Text in destination document</run>
	</paragraph>
</document>

As you can see, in this case list items are preserved.
Now let’s suppose that we need to inset the source document in the mid of paragraph in the destination document. For example, at bookmark. Here is how our simplified document looks:

<document name="destination" >
	<paragraph>
		<run>Text before bookmark</run>
		<bookmark name="test" />
		<run>Text after bookmark</run>
	</paragraph>
	<paragraph>
		<run>Text in destination document</run>
	</paragraph>
</document>

The source document is the same as in the previous example. We would like that the source document was inserted right at the bookmark. So we need that the first word in the source document was inserted after <run>Text before bookmark</run> and the last word in the source document before <run>Text after bookmark</run> follows. In this case we use InsertDocumentAtBookmark method. The first step is moving DocumentBuilder cursor to the bookmarks and inserting a paragraphs break. So we will have the following intermediate document (I removed bookmark just for convenience):

<document name="destination" >
	<paragraph>
		<run>Text before bookmark</run>
	</paragraph>
	<paragraph>
		<run>Text after bookmark</run>
	</paragraph>
	<paragraph>
		<run>Text in destination document</run>
	</paragraph>
</document>

Now, we should append content of the first paragraph of the source document to the first part of paragraph in the destination document and content of the last paragraph of the source document to the second part of paragraph. So the output will look like this:

<document name="destination" >
	<paragraph>
		<run>Text before bookmark</run>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph>
		<run>some text</run>
		<run>some text</run>
	</paragraph>
	<paragraph isListItem="true">
		<run>List item</run>
	</paragraph>
	<paragraph>
		<run>Second list item</run>
		<run>Text after bookmark</run>
	</paragraph>
	<paragraph>
		<run>Text in destination document</run>
	</paragraph>
</document>

As you can see, in this case, one of our list items lost it paragraph’s attributes and it is no longer list item.

So as a summary:

  1. If you need to preserve all paragraph settings, you should copy whole paragraph into the destination document as it is done in InsertDocument method.
  2. If you need to insert source document as a continuation of some paragraph in the destination document (without paragraph breaks), you should use technique as demonstrated in InsertDocumentAtBookmark method.

You are free to modify these method to fit your needs. I hope this could help you to achieve what you need.
Best regards.

Thanks Alex for taking time to explain in detail. This definately helped us in getting desired result.

The issues you have found earlier (filed as WORDSNET-5251) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(22)