Skip to content

Commit 9b91872

Browse files
Merge pull request #356 from SyncfusionExamples/ES-895946-Apply-footer-background-color
ES-895946-Add the sample Apply-footer-background-color
2 parents 7a5b323 + 6d42aef commit 9b91872

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed
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.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-footer-background-color", "Apply-footer-background-color\Apply-footer-background-color.csproj", "{A8911C29-ED5B-4DCD-97AC-E0328C062D5C}"
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+
{A8911C29-ED5B-4DCD-97AC-E0328C062D5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A8911C29-ED5B-4DCD-97AC-E0328C062D5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A8911C29-ED5B-4DCD-97AC-E0328C062D5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A8911C29-ED5B-4DCD-97AC-E0328C062D5C}.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 = {2D16ACBF-37C6-4A03-B9F5-B636CFC4DFA8}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Apply_footer_background_color</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Picture.png">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\Template.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
5+
{
6+
// Load the Word document from the file stream
7+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
8+
{
9+
// Add header and footer to the document
10+
AddHeaderFooter(document);
11+
// Save the modified document to an output file
12+
using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
13+
{
14+
document.Save(outputStream1, FormatType.Docx);
15+
}
16+
}
17+
}
18+
19+
/// <summary>
20+
/// Adds an image to the header paragraph.
21+
/// </summary>
22+
/// <param name="headerParagraph">The paragraph where the image will be added.</param>
23+
static void GetHeaderContent(IWParagraph headerParagraph)
24+
{
25+
// Open the image file stream
26+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Picture.png"), FileMode.Open, FileAccess.Read);
27+
28+
// Append the image to the header paragraph
29+
IWPicture picture = headerParagraph.AppendPicture(imageStream);
30+
31+
// Set image positioning and formatting properties
32+
picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
33+
picture.VerticalOrigin = VerticalOrigin.Margin;
34+
picture.VerticalPosition = -45; // Adjust vertical position
35+
picture.HorizontalOrigin = HorizontalOrigin.Column;
36+
picture.HorizontalPosition = 30f; // Adjust horizontal position
37+
picture.WidthScale = 50; // Scale image width
38+
picture.HeightScale = 40; // Scale image height
39+
}
40+
41+
/// <summary>
42+
/// Adds a styled footer with a background color and page number.
43+
/// </summary>
44+
static void GetFooterContent(IWParagraph footerParagraph)
45+
{
46+
// Create a rectangle shape in the footer for background styling
47+
Shape rectangleShape = footerParagraph.AppendShape(AutoShapeType.Rectangle, 700, 50);
48+
// Set rectangle shape properties (position, alignment, wrapping, color)
49+
rectangleShape.WrapFormat.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
50+
rectangleShape.HorizontalAlignment = ShapeHorizontalAlignment.Left;
51+
rectangleShape.VerticalAlignment = ShapeVerticalAlignment.Bottom;
52+
rectangleShape.HorizontalOrigin = HorizontalOrigin.Page;
53+
rectangleShape.VerticalOrigin = VerticalOrigin.Page;
54+
rectangleShape.FillFormat.Color = Syncfusion.Drawing.Color.LightBlue; // Set background color
55+
// Add a paragraph inside the rectangle for text
56+
footerParagraph = rectangleShape.TextBody.AddParagraph();
57+
footerParagraph.AppendText("Adventure Works Cycles");
58+
footerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
59+
// Add a right-aligned tab stop for page number formatting
60+
footerParagraph.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader);
61+
// Append page number fields
62+
footerParagraph.AppendText("\t"); // Insert tab space
63+
footerParagraph.AppendField("Page", FieldType.FieldPage); // Current page number
64+
footerParagraph.AppendText(" of ");
65+
footerParagraph.AppendField("NumPages", FieldType.FieldNumPages); // Total number of pages
66+
}
67+
68+
/// <summary>
69+
/// Adds headers and footers to all sections of the document.
70+
/// </summary>
71+
static void AddHeaderFooter(WordDocument document)
72+
{
73+
for (int i = 0; i < document.Sections.Count; i++)
74+
{
75+
WSection section = document.Sections[i];
76+
IWParagraph headerParagraph = section.HeadersFooters.FirstPageHeader.AddParagraph();
77+
GetHeaderContent(headerParagraph);
78+
IWParagraph headerOddParagraph = section.HeadersFooters.OddHeader.AddParagraph();
79+
GetHeaderContent(headerOddParagraph);
80+
IWParagraph evenParagraph = section.HeadersFooters.EvenHeader.AddParagraph();
81+
GetHeaderContent(evenParagraph);
82+
IWParagraph footerParagraph = section.HeadersFooters.FirstPageFooter.AddParagraph();
83+
GetFooterContent(footerParagraph);
84+
IWParagraph footerOddParagraph = section.HeadersFooters.OddFooter.AddParagraph();
85+
GetFooterContent(footerOddParagraph);
86+
IWParagraph footerEvenParagraph = section.HeadersFooters.EvenFooter.AddParagraph();
87+
GetFooterContent(footerEvenParagraph);
88+
}
89+
}

0 commit comments

Comments
 (0)