Skip to content

Commit 84141ac

Browse files
Added sample
1 parent 6919baf commit 84141ac

6 files changed

Lines changed: 188 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.37531.7 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Format-Page-Number-In-Word-Document", "Format-Page-Number-In-Word-Document\Format-Page-Number-In-Word-Document.csproj", "{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {037D14B2-80D6-4805-A0DE-C2839EAEA9F8}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Format_Page_Number_In_Word_Document</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)