Add Complex Page Numbers in Microsoft Word
This example adds a complex page number to footers in Microsoft® Word document. A complex number has the form [Chapter #][separator][Page#], for example, 7–1. You can add this type of number in a header or footer. You can do this using a template, by inserting a page number in a footer, and specifying the page number properties.
Whether you are using a template or a program, your template must use a multilevel list for the heading level that contains the chapter to reference. To create this type of list:
In your Word template, on the Home tab, click the Multilevel List button .
Select the numbered heading item.
Apply the Normal style to the paragraph.
Save and close the template.
You can then use a program like this one to use the complex page number.
The ChapterStartStyle
and ChapterSeparator
properties on the PageNumber
object specify to use heading level 1
for the chapter number and an en-dash as a separator.
import mlreportgen.dom.*; d = Document('mypages','docx','numberChapters'); open(d); layout = d.CurrentPageLayout; % Page number formatting pgnum = PageNumber(1,'n'); pgnum.ChapterStartStyle = '1'; pgnum.ChapterSeparator = 'endash'; % Add page number object to page layout styles layout.Style = {pgnum}; % layout.Style = [layout.Style {pgnum}]; % Create the footer object and insert a page number myfooter = DOCXPageFooter(); para = Paragraph(); para.HAlign = 'center'; append(para,Page()); append(myfooter,para); % Set PageFooters on the current layout to your footer object layout.PageFooters = myfooter; % Create content for i=1:5 title = append(d,Heading1(['Chapter' num2str(i)])); title.Style = {PageBreakBefore}; for j=1:30 append(d,'This is the body of the chapter'); end end close(d); rptview(d.OutputPath);
Tip
Create a page layout for each chapter to restart numbering the pages for each chapter at 1.
See Also
mlreportgen.dom.PageNumber
| mlreportgen.dom.DOCXPageLayout
| mlreportgen.dom.DOCXPageFooter