diff --git a/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document.sln b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document.sln new file mode 100644 index 00000000..f0d7c523 --- /dev/null +++ b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document.sln @@ -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 diff --git a/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Data/Template.docx b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Data/Template.docx new file mode 100644 index 00000000..1ee14836 Binary files /dev/null and b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Data/Template.docx differ diff --git a/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Format-Page-Number-In-Word-Document.csproj b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Format-Page-Number-In-Word-Document.csproj new file mode 100644 index 00000000..d034f738 --- /dev/null +++ b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Format-Page-Number-In-Word-Document.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Format_Page_Number_In_Word_Document + + + + + + + + + Always + + + Always + + + + diff --git a/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Output/.gitkeep b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Output/Sample.docx b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Output/Sample.docx new file mode 100644 index 00000000..35a68d6e Binary files /dev/null and b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Output/Sample.docx differ diff --git a/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Program.cs b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Program.cs new file mode 100644 index 00000000..38f93591 --- /dev/null +++ b/Sections/Format-Page-Number-In-Word-Document/.NET/Format-Page-Number-In-Word-Document/Program.cs @@ -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); + } + } + /// + /// Generates a Word document with multiple sections and page numbering. + /// + /// Return WordDocument + public static WordDocument GenerateLetterWithHeader() + { + // Sample content for the first section. + string letterSection1 = "

Your cooperation in this effort is greatly appreciated

"; + + // Sample content for the second section. + string letterSection2 = "

Your cooperation in this effort is greatly appreciated

"; + + // 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; + } + } +} \ No newline at end of file