Adding a automatic page numbering to a footer

Questions and answers on how to use DOCXReadWrite
Post Reply
Michel Huybrechts
Posts: 3
Joined: Sun Sep 10, 2017 1:30 pm

Adding a automatic page numbering to a footer

Post by Michel Huybrechts »

Using the examples from the samples I see how to add headers and footers to a page. I also figured out how to add tabs and figures. However, I can't find a way to add an automatic page numbering. The documentation does not explain this either.

How to add "page <#>" in a footer such that MSWord automatically fills in the <#> for each page?

Thank you
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Adding a automatic page numbering to a footer

Post by larsa »

Hello

You uses field codes for this.

Example:

Code: Select all

  DOCX.Editor.MainDoc.Headers.AddDefault;
  DOCX.Editor.MainDoc.Headers.Default_.Paras[0].AppendField('1','PAGE');
'1' is the default value. 'PAGE' is the field code
Lars Arvidsson, Axolot Data
Michel Huybrechts
Posts: 3
Joined: Sun Sep 10, 2017 1:30 pm

Re: Adding a automatic page numbering to a footer

Post by Michel Huybrechts »

Thank you, this works perfectly
DiBase
Posts: 13
Joined: Wed Feb 16, 2022 9:41 pm

Re: Adding a automatic page numbering to a footer

Post by DiBase »

I tried the code (with TDOCXReadWriteVcl 2.00.49) in a this modified version:

Code: Select all

  DOCX.Editor.Footers.AddDefault;
  DOCX.Editor.Footers.Default_.Paras[0].PAPX.Alignment := axptaCenter;
  DOCX.Editor.Footers.Default_.Paras[0].AppendPlainText( ' - ' );
  DOCX.Editor.Footers.Default_.Paras[0].AppendField('1','PAGE');
  DOCX.Editor.Footers.Default_.Paras[0].AppendPlainText( ' - ' );
Two issues with that:
  1. the last hyphen is not visible in Word (.docx export)
  2. in preview and pdf export only the default page number is visible (highlighted in gray)
Any solution for both?

- Dirk
Vinski
Posts: 12
Joined: Thu Dec 16, 2021 7:41 am

Re: Adding a automatic page numbering to a footer

Post by Vinski »

Hi,

I can help you with your first problem, if it is still relevant:
Instead of

Code: Select all

  DOCX.Editor.Footers.AddDefault;
  DOCX.Editor.Footers.Default_.Paras[0].PAPX.Alignment := axptaCenter;
  DOCX.Editor.Footers.Default_.Paras[0].AppendPlainText( ' - ' );
  DOCX.Editor.Footers.Default_.Paras[0].AppendField('1','PAGE');
  DOCX.Editor.Footers.Default_.Paras[0].AppendPlainText( ' - ' );
try

Code: Select all

  DOCX.Editor.Footers.AddDefault;
  DOCX.Editor.Footers.Default_.Paras[0].PAPX.Alignment := axptaCenter;
  DOCX.Editor.Footers.Default_.Paras[0].AppendPlainText( ' - ' );
  DOCX.Editor.Footers.Default_.Paras[0].AppendField('1','PAGE');
  DOCX.Editor.Footers.Default_.Paras[0].AppendFormattedText( ' - ' );
changing the last plain text to formatted text. That is what did the trick for me.
DiBase
Posts: 13
Joined: Wed Feb 16, 2022 9:41 pm

Re: Adding a automatic page numbering to a footer

Post by DiBase »

Hi Vinski,

thank you for your hint, but I solved it by adding two lines in the source code (function TAXWLogPara.AppendPlainText).

The second issue could also solved in the source code.
I changed initialization of FShowFields from True to False (in AXWGDIText.pas: TAXWTextPrint.Create).

- Dirk
Post Reply