1+ using Syncfusion . Drawing ;
2+ using Syncfusion . DocIO ;
3+ using Syncfusion . DocIO . DLS ;
4+ using System . IO ;
5+
6+ namespace Format_Page_Number_In_Word_Document
7+ {
8+ class Program
9+ {
10+ static void Main ( string [ ] args )
11+ {
12+ // Generates a Word document with multiple sections and page numbering.
13+ using ( WordDocument document = GenerateLetterWithHeader ( ) )
14+ {
15+ // Saves the Word document to the output folder.
16+ document . Save ( Path . GetFullPath ( @"Output/Sample.docx" ) , FormatType . Docx ) ;
17+ }
18+ }
19+ /// <summary>
20+ /// Generates a Word document with multiple sections and page numbering.
21+ /// </summary>
22+ /// <returns>Return WordDocument</returns>
23+ public static WordDocument GenerateLetterWithHeader ( )
24+ {
25+ // Sample content for the first section.
26+ string letterSection1 = "<p>Your cooperation in this effort is greatly appreciated</p>" ;
27+
28+ // Sample content for the second section.
29+ string letterSection2 = "<p>Your cooperation in this effort is greatly appreciated</p>" ;
30+
31+ // Loads the template Word document.
32+ string dataPath = Path . GetFullPath ( @"Data/Template.docx" ) ;
33+ WordDocument letterDoc = new WordDocument ( dataPath ) ;
34+
35+ // Retrieves the Normal style and updates its font settings.
36+ WParagraphStyle style = letterDoc . Styles . FindByName ( "Normal" ) as WParagraphStyle ;
37+
38+ if ( style != null )
39+ {
40+ style . CharacterFormat . FontName = "Verdana" ;
41+ style . CharacterFormat . FontSize = 12 ;
42+ }
43+
44+ // Defines the left margin value for the document.
45+ float letterMarginLeft = 140 ;
46+
47+ // Disables XHTML validation when inserting XHTML content.
48+ letterDoc . XHTMLValidateOption = XHTMLValidationType . None ;
49+
50+ // Tracks the current section number.
51+ int sectionNumber = 1 ;
52+
53+ // Adds a new section to the document.
54+ letterDoc . AddSection ( ) ;
55+
56+ // Iterates through all sections in the document.
57+ foreach ( IWSection section in letterDoc . Sections )
58+ {
59+ // Sets common page settings.
60+ section . PageSetup . Margins . Right = 40 ;
61+ section . PageSetup . PageSize = new SizeF ( 600 , 790 ) ;
62+
63+ // Configures the first section.
64+ if ( sectionNumber == 1 )
65+ {
66+ // Sets page margins.
67+ section . PageSetup . Margins . Top = 0 ;
68+ section . PageSetup . Margins . Left = letterMarginLeft ;
69+
70+ // Inserts XHTML content into the section body.
71+ section . Body . InsertXHTML ( letterSection1 ) ;
72+
73+ sectionNumber ++ ;
74+ }
75+ // Configures subsequent sections.
76+ else
77+ {
78+ // Sets header distance from the top of the page.
79+ section . PageSetup . HeaderDistance = 45 ;
80+
81+ // Starts the section on a new page.
82+ section . BreakCode = SectionBreakCode . NewPage ;
83+
84+ // Restarts page numbering for the new section.
85+ section . PageSetup . PageStartingNumber = 2 ;
86+ section . PageSetup . RestartPageNumbering = true ;
87+
88+ // Sets page margins.
89+ section . PageSetup . Margins . Top = 0 ;
90+ section . PageSetup . Margins . Left = letterMarginLeft - 70 ;
91+
92+ // Creates a paragraph in the section header.
93+ IWParagraph paragraph = section . HeadersFooters . Header . AddParagraph ( ) ;
94+
95+ // Adds a right-aligned tab stop.
96+ paragraph . ParagraphFormat . Tabs . AddTab ( 560.0f , TabJustification . Right , TabLeader . NoLeader ) ;
97+
98+ // Adds header text before the page number field.
99+ paragraph . AppendText ( "\t Page " ) ;
100+
101+ // Inserts a PAGE field into the header.
102+ WField field = paragraph . AppendField ( "CurrentPageNumber" , FieldType . FieldPage ) as WField ;
103+
104+ IEntity entity = field ;
105+
106+ // Iterates through the field entities and applies formatting.
107+ while ( entity != null && entity . NextSibling != null )
108+ {
109+ // Formats text ranges within the field.
110+ if ( entity is WTextRange textRange )
111+ {
112+ textRange . CharacterFormat . FontSize = 22 ;
113+ textRange . CharacterFormat . FontName = "Times New Roman" ;
114+ }
115+ // Stops when the field end marker is reached.
116+ else if ( entity is WFieldMark fieldMark &&
117+ fieldMark . Type == FieldMarkType . FieldEnd )
118+ {
119+ break ;
120+ }
121+
122+ // Moves to the next sibling entity.
123+ entity = entity . NextSibling ;
124+ }
125+
126+ // Adds a body paragraph to the section.
127+ paragraph = section . AddParagraph ( ) ;
128+
129+ // Sets the page size.
130+ section . PageSetup . PageSize = new SizeF ( 600 , 790 ) ;
131+
132+ // Inserts XHTML content into the section body.
133+ section . Body . InsertXHTML ( letterSection2 ) ;
134+ }
135+ }
136+ // Returns the generated Word document.
137+ return letterDoc ;
138+ }
139+ }
140+ }
0 commit comments