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

Free Support Forum - aspose.com

Total Pages in word document

I have a word document and within it want to put the total pages into a table, sounds simple enough. When I use this line of code it gives me the wrong amount of pages, it should be 7 pages but instead I get 54, no idea where its getting this figure from?





docbuilder.InsertField(“NUMPAGES”, “”)

Hi Martin,

Thanks for your inquiry. Please use DocumentBuilder.InsertField method (FieldType, Boolean) to insert a Word field into a document and optionally updates the field result. If you still face problem, please share your input Word document here for testing purposes. We will investigate the issue on our side and provide you more information.

Document doc = new Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertField(FieldType.FieldNumPages, true);

doc.Save(MyDir + "Out.docx");

I’m now getting field code 26 is not invalid or not supported, does this mean I need to upgrade? The code i’m using is:



docbuilder.InsertField(Aspose.Words.Fields.FieldType.FieldNumPages, True)



It appears in the object browser so its as if its there, I’m using TFS libraries hence why I’ve included the full path

Hi Martin,

Thanks for your inquiry. Please use latest version of Aspose.Words for .NET 15.12.0. If you still face problem, please share your input and output documents here for testing. We will investigate the issue on our side and provide you more information.

Thanks for your response, unfortunately an upgrade doesn’t solve this, still get the issue. Its strange as my header will work using this line of code:

docbuilder.InsertField(“NUMPAGES”, “”)

But if I place this in my class somewhere else it produces a very high number!..

If when the document is loaded you right click on the field in question and click the update field it works

Fix



oWord.ActiveDocument.Bookmarks(“bk”).Range.Text = oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages)

Hi Martin,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

    create a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.

Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Martin,

Thanks for your reply via email. Unfortunately, your code example contains compile time errors. To ensure a timely and accurate response, please share the requested detail (in my previous post) for testing purposes.

As soon as you get these pieces of information ready, we'll start investigation into your issue and provide you more information. Thanks for your cooperation.