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

Free Support Forum - aspose.com

Need Accurate Page Count after adding content with DocumentBuilder

We have a problem finding out the page count after adding content to the end of a word doc using DocumentBuilder.

Step 1, we merge content into the word doc using MailMerge.Execute()
// note the callback includes a DocumentBuilder
HandleMergeField callback = new HandleMergeField();
docTemplate.WordDoc.MailMerge.FieldMergingCallback = callback;
docTemplate.WordDoc.MailMerge.Execute(fieldNames, fieldValues.ToArray());

Step 2, we determine the page count from docTemplate.WordDoc.PageCount. This PageCount is correct at this time.

Step 3, we append a horizontal line to the end of the document, using the Builder instance that was part of the callback from earlier
// Add end line
builder.MoveToDocumentEnd();
builder.Writeln();
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;
builder.InsertNode(endOfContentLine);
builder.Writeln();

Step 4, we check the PageCount again from docTemplate.WordDoc.PageCount. This PageCount is WRONG in cases where the shape added above causes a new page.

Step 5, we append several page breaks to the end of the docuemnt, using the same builder
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);

OUR PROBLEM is in Step 4 - the Page Count after appending content (the shape) the WordDoc.PageCount is WRONG if the new content spilled over to a new page. We have tried adding more content (like a small paragraph of HTML) and we have the same problem. the WordDoc.PageCount is wrong.

QUESTION - How do we get an ACCURATE PAGE COUNT after appending content?

See attached.Artifact GHLCITD.zip (15.8 KB)

@bprabu

Please call Document.UpdatePageLayout method before getting the page count of document.

If you still face problem, please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue on our side and provide you more information.

PS: To attach the application, please zip and upload it.