Fields - Adding 1 to the value of PAGE and NUMPAGES fields

Hello,

I’d like to add numbering to my word document using fields PAGE and NUMPAGES.

The thing is that I need to increase its value by 1.
For example, if the document contains 3 pages, on the first page I should see 2/4 and not 1/3.

Here’s an example of what I want to do (see the header)
Result_sample.docx (14.1 KB)

In word, the field I need to write is :
{= {PAGE} + 1}
{= {NUMPAGES} + 1}

In my code, when I try this line, I get a syntax error in the word document even though the field code seems correct.

docBuilder.InsertField(@"={PAGE}+1");

Any help is welcome !
Best regards.

@Irchad You should use the following code to get the expected output:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert formula field {= }
Field formulaField = builder.InsertField(FieldType.FieldFormula, false);
// Move DocumentBuilder inside formula field field code
builder.MoveTo(formulaField.Separator);
// Insert PAGE field.
builder.InsertField(FieldType.FieldPage, false);
// Continue writing formula
builder.Write("+1");
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

Hello,

Thanks for the reply.
When I try your code with a second field for NUMPAGES, I’m getting a syntax error

Here’s my code

// Insert formula field PAGE {= }
Field formulaField = this.docBuilder.InsertField(FieldType.FieldFormula, false);
// Move DocumentBuilder inside formula field field code
this.docBuilder.MoveTo(formulaField.Separator);
// Insert PAGE field.
this.docBuilder.InsertField(FieldType.FieldPage, false);
// Continue writing formula
this.docBuilder.Write("+1");

// Display separator between PAGE and NUMPAGES
this.docBuilder.Write(" / ");

// Insert formula field for NUMPAGES {= }
Field formulaNumPagesField = this.docBuilder.InsertField(FieldType.FieldFormula, false);
// Move DocumentBuilder inside formula field field code
this.docBuilder.MoveTo(formulaField.Separator);
// Insert NUMPAGES field.
this.docBuilder.InsertField(FieldType.FieldNumPages, false);
// Continue writing formula
this.docBuilder.Write("+1");

I think it is the “/” that breaks everything.
How do I tell the code that I completed the PAGE field, so that I can put free text and then start another field for NUMPAGES?

Thanks !

@Irchad Please try using the following code:

Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);

// Insetrt two formula fields separated by a slash.
Field formulaFieldPage = docBuilder.InsertField(FieldType.FieldFormula, false);
// Display separator between PAGE and NUMPAGES
docBuilder.Write(" / ");
Field formulaFieldNumpages = docBuilder.InsertField(FieldType.FieldFormula, false);

// Move DocumentBuilder inside formula field field code
docBuilder.MoveTo(formulaFieldPage.Separator);
// Insert PAGE field.
docBuilder.InsertField(FieldType.FieldPage, false);
// Continue writing formula
docBuilder.Write("+1");

// Move DocumentBuilder inside formula field field code
docBuilder.MoveTo(formulaFieldNumpages.Separator);
// Insert NUMPAGES field.
docBuilder.InsertField(FieldType.FieldNumPages, false);
// Continue writing formula
docBuilder.Write("+1");

doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

The problem with your code occurs because when you write a slash, DocumentBuilder cursor is still inside field code.

It’s working !

Thanks a lot !

1 Like