Skip to content

Commit 51b116b

Browse files
Merge pull request #185 from SyncfusionExamples/963596
963596: Added dynamic footers to the sections of the PDF document
2 parents 07c15fd + 8a1b1ec commit 51b116b

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
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.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adding-dynamic-headers-and-footers-in-PDF", "Adding-dynamic-headers-and-footers-in-PDF\Adding-dynamic-headers-and-footers-in-PDF.csproj", "{4DF00BDD-6231-4C4E-952C-A40D7E9AC032}"
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+
{4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.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 = {EBE02CCA-44B0-499E-A5F2-8088F932054E}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Adding-dynamic-headers-and-footers-in-PDF</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Output/gitkeep.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
// Create a new PDF document.
10+
PdfDocument document = new PdfDocument();
11+
12+
// Subscribe to the PageAdded event to add header and footer for every page.
13+
document.Pages.PageAdded += (sender, e) => PageAddedHandler(sender, e);
14+
15+
// Define content font and brush for main text.
16+
PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18);
17+
PdfBrush contentBrush = new PdfSolidBrush(Color.Black);
18+
19+
// Define the main instructional text.
20+
string overflowText =
21+
@"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals.
22+
23+
Key Features:
24+
- Multi-page automatic content flow using pagination
25+
- Support for rich text formatting: headers, bullets, and tables
26+
- Insert images, tables, and charts seamlessly
27+
- Add interactive elements: bookmarks, hyperlinks, and attachments
28+
- Control layout: margins, page breaks, and dynamic footers
29+
30+
Usage Example:
31+
This project demonstrates how to paginate multiple paragraphs of text describing PDF functionality. When the content exceeds a single page, Syncfusion’s PdfTextElement automatically creates new pages and triggers the PageAdded event. This allows you to attach custom footers, such as page numbers or custom codes, to each page for improved navigation and professional document appearance.
32+
33+
Adding dynamic footers is useful for:
34+
- Section labeling in large documents
35+
- Including secure or traceable codes for each page
36+
- Ensuring readers always know their page context
37+
38+
Other advanced scenarios:
39+
- Creating Table of Contents with page navigation
40+
- Inserting named destinations for quick jumps
41+
- Using graphics and interactive elements within the same document
42+
43+
Experiment by updating this program to add headers, watermarks, or section-based page numbers based on your specific requirements.
44+
45+
For more information, visit:
46+
https://help.syncfusion.com/file-formats/pdf/working-with-text
47+
https://help.syncfusion.com/file-formats/pdf/working-with-graphics
48+
49+
This concludes the instructional workflow for auto-paginated, footer-enhanced PDF generation in .NET.";
50+
51+
// Set the header and footer height
52+
float headerHeight = 40f;
53+
float footerHeight = 30f;
54+
55+
// Create a text element for automatic pagination.
56+
PdfTextElement textElement = new PdfTextElement(overflowText, contentFont, contentBrush);
57+
58+
// Subscribe to the BeginPageLayout event to offset text on each new page below the header.
59+
textElement.BeginPageLayout += (sender, args) =>
60+
{
61+
// Always start content BELOW the header on every page.
62+
args.Bounds = new RectangleF(0, headerHeight, args.Page.GetClientSize().Width, args.Page.GetClientSize().Height - headerHeight - footerHeight);
63+
};
64+
65+
// Add the first page.
66+
PdfPage firstPage = document.Pages.Add();
67+
68+
// Start drawing content (pagination and event will handle rest).
69+
textElement.Draw(firstPage, new PointF(0, headerHeight));
70+
71+
// Save and close the document.
72+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write))
73+
{
74+
document.Save(outputFileStream);
75+
}
76+
document.Close(true);
77+
}
78+
79+
// Add header and footer to every page.
80+
static void PageAddedHandler(object sender, PageAddedEventArgs e)
81+
{
82+
PdfPage page = e.Page;
83+
int currentPage = page.Section.Pages.IndexOf(page) + 1;
84+
85+
// Draw header at the top (within reserved header bounds).
86+
string headerText = $"This is the header - Page {currentPage}";
87+
page.Graphics.DrawString(
88+
headerText,
89+
new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold),
90+
new PdfSolidBrush(Color.DimGray),
91+
new PointF(10, 10) // Within header area
92+
);
93+
94+
// Draw footer at the bottom (within reserved footer area).
95+
string timestamp = DateTime.Now.ToString("'Date:' yyyy-MM-dd 'Time:' HH:mm:ss");
96+
string footerText = $"Page {currentPage} {timestamp}";
97+
page.Graphics.DrawString(
98+
footerText,
99+
new PdfStandardFont(PdfFontFamily.Helvetica, 12),
100+
new PdfSolidBrush(Color.Black),
101+
new PointF(10, page.GetClientSize().Height - 30)
102+
);
103+
}
104+
}

0 commit comments

Comments
 (0)