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

Free Support Forum - aspose.com

Inserting images & page breaks at the end of a document

I need to insert multiple images at the end of a word document, each separated by a page break. The image will not be inserted all at once... the code will insert an image, do some other document changes, insert another image at the end of the document, do so other changes, and so on.

Each time I need to insert an image and page break, I do the following:

  • Find the last node in the document (where node.NextPreOrderSibling(node.Document) == null).
  • Using DocumentBuilder:
    • builder.MoveTo(lastNode);
    • builder.InsertBreak(BreakType.PageBreak);
    • builder.InsertImage(file.GetFileAsStream(SourceFileContext));

This works fine for the first image, but each subsequent pagebreak/image combination gets inserted just before the first image that was inserted (and after the first pagebreak that was inserted). Note that after the first time, each time I find the last node of the document, the node type is Shape.

Do you have any suggestions?

thank you,

Hi Ken,

The structure of your alogrthim sounds correct. I suggest you replace the code that finds the last node in the document with the DocumentBuilder.MoveToDocumentEnd() method and see if this corrects the behaviour.

If the issue is still unsolved could you please post your code here and we will take a closer look for you.


I had much better luck using the DocumentBuilder.MoverToDocumentEnd() method. This brings me to the following question - should the code below throw an exception?

//WorkingDoc is instance of Document
//builder is instance of DocumentBuilder
Node lastNode = GetNode(builder.CurrentNode); //uses algorithm based on Node.NextPreOrder()
if (insertNode.NextPreOrder(WorkingDoc) == null
&& builder.CurrentNode != insertNode)
throw new Exception("Is this a bug?");

The reason for this question is that I can't use MoveToDocumentEnd(). In my original post, I simplified my problem statement. What I really need to do is the following: from the current node, move up to the nth hard pagebreak and then insert an pagebreak and an image. The problem I am running into is when the nth pagebreak happens to be the end of the document.


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. No, it is not a bug. DocumentBuilder.CurrentNode can return null if DocumentBuilder cursor was moved into an empty paragraph. Please follow the link for more information:


In your case if it is supposed that insertNode.NextPreOrder(WorkingDoc) == null, insertNode is last paragraph (if it is empty) or the last child of the paragraph (if it is not empty).

So there could be a situation when the latest node of your document is an empty paragraph. In this case, insertNode is a paragraph, and DocumentBuilder.CurrentNode returns null.

Best regards,