DocumentBuilder.MoveToDocumentEnd() not working

After executing MailMerge, we need to move to the END of the document to create an object (a line) and some blank pages (page breaks).

    docTemplate.WordDoc.MailMerge.Execute(fieldNames, fieldValues.ToArray());

    // add end line
    DocumentBuilder builder = new DocumentBuilder(docTemplate.WordDoc);
    builder.MoveToDocumentEnd();            
    Shape endOfContentLine = new Shape(docTemplate.WordDoc, ShapeType.Line);
    endOfContentLine.Name = END_OF_WORD_CONTENT;
    endOfContentLine.Bounds = new RectangleF(0, 0, 500, 0);
    endOfContentLine.StrokeColor = Color.Red; // DEBIG Red for now so we can see it
    builder.InsertNode(endOfContentLine);
    
    // add extra pages
    for (int i = 0; i <= 5; i++)
    {
        builder.MoveToDocumentEnd();
        builder.InsertBreak(BreakType.PageBreak);
    }

Problem) The method DocumentBuilder.MoveToDocumentEnd() is not working as expected.
The red line we create is NOT at the END of the document. There is one line of static text below it (see attached) output_GPRINTER.zip (54.1 KB)

Question) We need to find out the page number where the shape endOfContentLine is printed. It’s not predictable, because we do mail merge above the line.

@bprabu,

In this case, after moving cursor to the end of document, please use builder.Writeln(); to get the desired output:

...
...
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.Writeln();
...
...

Hope, this helps.

Question) We need to find out the page number where the shape endOfContentLine is printed. It’s not predictable, because we do mail merge above the line.

Yes. That fixed the problem to draw the endOfContentLine, but we need to find out how to determine the page number of that line(shape). Please help.

@bprabu,

You can get the exact coordinates of Shape by using the following code:

Document doc = new Document("E:\\temp\\input.docx");
Shape blackLine = (Shape)doc.GetChildNodes(NodeType.Shape, true)[0];

LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

layoutEnumerator.Current = layoutCollector.GetEntity(blackLine);

Console.WriteLine("(Top , Left) : ({0} , {1}) and (Width , Height) : ({2} , {3})",
        layoutEnumerator.Rectangle.Top / 72, layoutEnumerator.Rectangle.Left / 72,
        layoutEnumerator.Rectangle.Width / 72, layoutEnumerator.Rectangle.Height / 72);

Hope, this helps.

Please also see the following methods