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

Free Support Forum - aspose.com

Insert fields

I would like to insert the following formula into the footer of my document for page numbering


PAGE + Myvariable.

I would appreciate some assistance.
Hi Davis,

Thanks for your inquiry. It seems that you want to insert page number in footer of document. Please use following code example to insert page number in the footer of document. Hope this helps you.

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

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

builder.InsertField(FieldType.FieldPage, true);

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

Hello,

Thank you for the prompt response.

Let me provide more details to my question. So I am creating a document with multiple chapters. I have very specific requirements for numbering the pages in each chapter. The format for page numbering should output as such.

1-1
1-2
1-3
2-1
2-2

The first number is the chapter and the second is the page it falls on for that chapter. Simple enough

Printing the Chapter number is not an issue. It's the subsequent numbers that need a formula because of my requirements. I need this formula in the footer so how do I achieve that?

The mathematical formula for the second part of my page numbering format is.

(CurrentPageNumber + 2 ) - SomeIntegerVariable;

How do I insert this formula into my document so each page has the right number?

Thank you for your assistance.

/Davis A


Hi Davis,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for our reference:

  • Your input Word document
  • Please attach your target Word document showing the desired behavior. You can use Microsoft Word to create your target Word document. We will investigate as to how you are expecting your final document be generated like.

As soon as you get these pieces of information ready, we will then provide you more information about your query along with code. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click 'Reply' button that will bring you to the 'reply page' and there at the bottom you can include any attachments with that post by clicking the 'Add/Update' button.

Unfortunately I do not have an input document. All data/content is coming from various database tables and sources. However I've attachment the desired output. It broken up into two files because that is the only was I was able to create it manually. In my actual output it would be one file. Notice how the page numbers are prefixed by chapter then page number? Again the page number is based on a formula that includes the actual page number.

The page number formula as I stated before is

(CurrentPageNumber + a_variable ) - another_variable.

Thank you for your assistance.
Hi Davis,

Thanks for sharing the documents. From the shared documents, it seems that you want to insert table of contents with page number. Please read following documentation link about working with table of contents.
How-to Insert and Work with the Table of Contents Field

If you want page numbers that show the chapter number and the page number, such as 1-1 or 1:1, you need to format page numbers. Please check the attached image for formatting page number (include chapter number). We suggest you please read following web links for your kind reference.
Chapter headings with page numbers
Inserting Page Numbers for Chapters and Appendixes

Unfortunately, Aspose.Words does not provide API to format page number at the moment. The feature ID is WORDSNET-10869. We apologize for your inconvenience.
davis.ampofo:
(CurrentPageNumber + a_variable ) - another_variable.
You can insert such formula (field) in the footer of document using following code example.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

int a_variable = 10;

int another_variable = 20;

FieldBuilder fbuilder = new FieldBuilder(FieldType.FieldFormula);

fbuilder

.AddArgument(new FieldBuilder(FieldType.FieldPage))

.AddArgument("+")

.AddArgument(a_variable)

.AddArgument("-")

.AddArgument(another_variable)

.BuildAndInsert(builder.CurrentParagraph);

doc.UpdateFields();

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

The desired output document (Chapter 1.doc) does not contain the TOC field which contains page numbers and chapter numbers. The document contains only simple plain text. We have attached Word document showing the desired behavior. 1) Table of contents showing page number and chapter numbers 2) Page number with chapter number in footer of document.

Could you please confirm if you want the same output document? We will then provide you more information on this along with code.

I think the formula you provided for the FieldBuilder should work for what I need to accomplish. However, it appears FieldBuilder is only available in version 2016.01.30. Unfortunately, I currently have version 13.8.0.0 of Aspose.Words. If that is the case then I intend to upgrade (please confirm that is the case).

One other thing I need your assistance on. If you look at the Chapter 1.doc that I previously attached, notice the page numbers are centered in the footer and to the right of that there are additional text. How would I achieve the same spacing and formatting using the formula you provided?

Thanks again,
/Davis
Hi Davis,

Thanks for your inquiry.
davis.ampofo:
I think the formula you provided for the FieldBuilder should work for what I need to accomplish. However, it appears FieldBuilder is only available in version 2016.01.30. Unfortunately, I currently have version 13.8.0.0 of Aspose.Words. If that is the case then I intend to upgrade (please confirm that is the case).
Yes, you need to upgrade to latest version of Aspose.Words for .NET 16.1.0. Please upgrade to latest version of Aspose.Words for .NET 16.1.0.
davis.ampofo:
One other thing I need your assistance on. If you look at the Chapter 1.doc that I previously attached, notice the page numbers are centered in the footer and to the right of that there are additional text. How would I achieve the same spacing and formatting using the formula you provided?
In your case, we suggest you please insert a table with one row having three cells in the footer of document. Insert page number field in the second cell with paragraph alignment as center and insert additional text in the third cell with paragraph alignment as right. Please check following code example for your kind reference. Hope this helps you.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

Table table = builder.StartTable();

// Insert first cell

builder.InsertCell();

builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(33);

// Insert second cell

builder.InsertCell();

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

int a_variable = 10;

int another_variable = 20;

FieldBuilder fbuilder = new FieldBuilder(FieldType.FieldFormula);

fbuilder

.AddArgument(new FieldBuilder(FieldType.FieldPage))

.AddArgument("+")

.AddArgument(a_variable)

.AddArgument("-")

.AddArgument(another_variable)

.BuildAndInsert(builder.CurrentParagraph);

// Insert third cell

builder.InsertCell();

builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;

builder.Write("Enclosure (1)");

builder.EndRow();

builder.EndTable();

table.PreferredWidth = PreferredWidth.FromPercent(100);

table.ClearBorders();

doc.UpdateFields();

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

The issues you have found earlier (filed as WORDSNET-10869) have been fixed in this Aspose.Words for .NET 22.5 update also available on NuGet.