Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.37531.7 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C2D26B9-6BC1-0B0B-0218-62B152FB3C0D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {037D14B2-80D6-4805-A0DE-C2839EAEA9F8}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Format_Page_Number_In_Word_Document</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using Syncfusion.Drawing;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;

namespace Format_Page_Number_In_Word_Document
{
class Program
{
static void Main(string[] args)
{
// Generates a Word document with multiple sections and page numbering.
using (WordDocument document = GenerateLetterWithHeader())
{
// Saves the Word document to the output folder.
document.Save(Path.GetFullPath(@"Output/Sample.docx"), FormatType.Docx);
}
}
/// <summary>
/// Generates a Word document with multiple sections and page numbering.
/// </summary>
/// <returns>Return WordDocument</returns>
public static WordDocument GenerateLetterWithHeader()
{
// Sample content for the first section.
string letterSection1 = "<p>Your cooperation in this effort is greatly appreciated</p>";

// Sample content for the second section.
string letterSection2 = "<p>Your cooperation in this effort is greatly appreciated</p>";

// Loads the template Word document.
string dataPath = Path.GetFullPath(@"Data/Template.docx");
WordDocument letterDoc = new WordDocument(dataPath);

// Retrieves the Normal style and updates its font settings.
WParagraphStyle style = letterDoc.Styles.FindByName("Normal") as WParagraphStyle;

if (style != null)
{
style.CharacterFormat.FontName = "Verdana";
style.CharacterFormat.FontSize = 12;
}

// Defines the left margin value for the document.
float letterMarginLeft = 140;

// Disables XHTML validation when inserting XHTML content.
letterDoc.XHTMLValidateOption = XHTMLValidationType.None;

// Tracks the current section number.
int sectionNumber = 1;

// Adds a new section to the document.
letterDoc.AddSection();

// Iterates through all sections in the document.
foreach (IWSection section in letterDoc.Sections)
{
// Sets common page settings.
section.PageSetup.Margins.Right = 40;
section.PageSetup.PageSize = new SizeF(600, 790);

// Configures the first section.
if (sectionNumber == 1)
{
// Sets page margins.
section.PageSetup.Margins.Top = 0;
section.PageSetup.Margins.Left = letterMarginLeft;

// Inserts XHTML content into the section body.
section.Body.InsertXHTML(letterSection1);

sectionNumber++;
}
// Configures subsequent sections.
else
{
// Sets header distance from the top of the page.
section.PageSetup.HeaderDistance = 45;

// Starts the section on a new page.
section.BreakCode = SectionBreakCode.NewPage;

// Restarts page numbering for the new section.
section.PageSetup.PageStartingNumber = 2;
section.PageSetup.RestartPageNumbering = true;

// Sets page margins.
section.PageSetup.Margins.Top = 0;
section.PageSetup.Margins.Left = letterMarginLeft - 70;

// Creates a paragraph in the section header.
IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph();

// Adds a right-aligned tab stop.
paragraph.ParagraphFormat.Tabs.AddTab(560.0f, TabJustification.Right, TabLeader.NoLeader);

// Adds header text before the page number field.
paragraph.AppendText("\t Page ");

// Inserts a PAGE field into the header.
WField field = paragraph.AppendField("CurrentPageNumber", FieldType.FieldPage) as WField;

IEntity entity = field;

// Iterates through the field entities and applies formatting.
while (entity != null && entity.NextSibling != null)
{
// Formats text ranges within the field.
if (entity is WTextRange textRange)
{
textRange.CharacterFormat.FontSize = 22;
textRange.CharacterFormat.FontName = "Times New Roman";
}
// Stops when the field end marker is reached.
else if (entity is WFieldMark fieldMark &&
fieldMark.Type == FieldMarkType.FieldEnd)
{
break;
}

// Moves to the next sibling entity.
entity = entity.NextSibling;
}

// Adds a body paragraph to the section.
paragraph = section.AddParagraph();

// Sets the page size.
section.PageSetup.PageSize = new SizeF(600, 790);

// Inserts XHTML content into the section body.
section.Body.InsertXHTML(letterSection2);
}
}
// Returns the generated Word document.
return letterDoc;
}
}
}
Loading