@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");
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?
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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.