Skip to content

Commit 0fe76c3

Browse files
committed
963596: Added dynamic footers to the sections of the PDF document
1 parent 0bc58de commit 0fe76c3

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-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.14.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dynamic-footers-across-PDF-pages", "Dynamic-footers-across-PDF-pages\Dynamic-footers-across-PDF-pages.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>Dynamic_footers_across_PDF_pages</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/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
class Program
5+
{
6+
public static PdfPage page;
7+
8+
static void Main(string[] args)
9+
{
10+
// Create a new PDF document
11+
PdfDocument document = new PdfDocument();
12+
13+
// Create a section and apply bottom margin for footers
14+
PdfSection section1 = document.Sections.Add();
15+
section1.PageSettings.Margins.Bottom = 30;
16+
17+
// Attach a custom handler for the PageAdded event
18+
section1.PageAdded += (sender, e) => PageAddedHandler(sender, e, 1);
19+
20+
// Add the first page to initialize pagination
21+
section1.Pages.Add();
22+
23+
// Prepare font and brush for the main content
24+
PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18);
25+
PdfBrush contentBrush = new PdfSolidBrush(Color.Black);
26+
27+
// Optionally, set up line spacing and paragraph styles
28+
PdfStringFormat format = new PdfStringFormat
29+
{
30+
ParagraphIndent = 35f,
31+
LineSpacing = 20f
32+
};
33+
// Get example instructional text related to PDF creation
34+
string overflowText = GetLongPdfGuideText();
35+
// Draw the instructional text using PdfTextElement, which handles pagination
36+
var textElement = new PdfTextElement(overflowText, contentFont, contentBrush);
37+
PdfLayoutFormat layoutFormat = new PdfLayoutFormat
38+
{
39+
Layout = PdfLayoutType.Paginate, // Enables pagination
40+
PaginateBounds = new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30) // Leaves space for footer
41+
};
42+
43+
var layoutResult = textElement.Draw(section1.Pages[0],
44+
new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30), layoutFormat);
45+
//Create file stream.
46+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
47+
{
48+
//Save the PDF document to file stream.
49+
document.Save(outputFileStream);
50+
}
51+
//Close the document.
52+
document.Close(true);
53+
}
54+
55+
/// <summary>
56+
/// Handles the PageAdded event to draw a dynamic footer on each page.
57+
/// </summary>
58+
static void PageAddedHandler(object sender, PageAddedEventArgs e, int sectionNumber)
59+
{
60+
PdfPage page = e.Page;
61+
int currentPage = page.Section.Pages.IndexOf(page) + 1;
62+
63+
// Generate a random alphanumeric code for added uniqueness in the footer
64+
string randomFooter = GenerateRandomFooterCode();
65+
66+
string footerText = $"Section {sectionNumber} - Page {currentPage} - {randomFooter}";
67+
68+
// Add the footer to the page
69+
DrawFooter(page, footerText);
70+
}
71+
72+
/// <summary>
73+
/// Draws the footer text at the bottom of the specified page.
74+
/// </summary>
75+
static void DrawFooter(PdfPage page, string footerText)
76+
{
77+
page.Graphics.DrawString(
78+
footerText,
79+
new PdfStandardFont(PdfFontFamily.Helvetica, 12),
80+
new PdfSolidBrush(Color.Black),
81+
new PointF(10, page.GetClientSize().Height - 30)
82+
);
83+
}
84+
85+
/// <summary>
86+
/// Generates a random 3-letter code with numbers for footer uniqueness.
87+
/// </summary>
88+
static string GenerateRandomFooterCode()
89+
{
90+
Random random = new Random();
91+
char[] letters = new char[3];
92+
for (int i = 0; i < 3; i++)
93+
letters[i] = (char)('A' + random.Next(26));
94+
return new string(letters) + random.Next(100, 1000).ToString();
95+
}
96+
97+
/// <summary>
98+
/// Returns sample instructional text about PDF generation and features.
99+
/// </summary>
100+
static string GetLongPdfGuideText()
101+
{
102+
// This text simulates documentation for PDF feature usage
103+
return @"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals.
104+
105+
Key Features:
106+
- Multi-page automatic content flow using pagination
107+
- Support for rich text formatting: headers, bullets, and tables
108+
- Insert images, tables, and charts seamlessly
109+
- Add interactive elements: bookmarks, hyperlinks, and attachments
110+
- Control layout: margins, page breaks, and dynamic footers
111+
112+
Usage Example:
113+
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.
114+
115+
Adding dynamic footers is useful for:
116+
- Section labeling in large documents
117+
- Including secure or traceable codes for each page
118+
- Ensuring readers always know their page context
119+
120+
Other advanced scenarios:
121+
- Creating Table of Contents with page navigation
122+
- Inserting named destinations for quick jumps
123+
- Using graphics and interactive elements within the same document
124+
125+
Experiment by updating this program to add headers, watermarks, or section-based page numbers based on your specific requirements.
126+
127+
For more information, visit:
128+
https://help.syncfusion.com/file-formats/pdf/working-with-text
129+
https://help.syncfusion.com/file-formats/pdf/working-with-graphics
130+
131+
This concludes the instructional workflow for auto-paginated, footer-enhanced PDF generation in .NET.";
132+
}
133+
}

0 commit comments

Comments
 (0)