Skip to content

Commit 8a1b1ec

Browse files
committed
963596: Added proper code changes.
1 parent 5b46570 commit 8a1b1ec

File tree

4 files changed

+49
-39
lines changed

4 files changed

+49
-39
lines changed

Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln renamed to Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.14.36221.1 d17.14
55
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}"
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}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
6-
<RootNamespace>Dynamic_footers_across_PDF_pages</RootNamespace>
6+
<RootNamespace>Adding-dynamic-headers-and-footers-in-PDF</RootNamespace>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>
99
</PropertyGroup>

Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs renamed to Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Program.cs

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,99 @@ class Program
66
{
77
static void Main(string[] args)
88
{
9-
// Create a new PDF document
9+
// Create a new PDF document.
1010
PdfDocument document = new PdfDocument();
1111

12-
// Add a section with space at the bottom for the footer
13-
PdfSection section1 = document.Sections.Add();
14-
section1.PageSettings.Margins.Bottom = 30;
12+
// Subscribe to the PageAdded event to add header and footer for every page.
13+
document.Pages.PageAdded += (sender, e) => PageAddedHandler(sender, e);
1514

16-
// Subscribe to the PageAdded event to add dynamic footer on each new page
17-
section1.PageAdded += (sender, e) => PageAddedHandler(sender, e, 1);
18-
19-
// Add the first page (the rest will be added with page overflow)
20-
section1.Pages.Add();
21-
22-
// Prepare the content font and brush
15+
// Define content font and brush for main text.
2316
PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18);
2417
PdfBrush contentBrush = new PdfSolidBrush(Color.Black);
2518

26-
// Define formatting for the main content
27-
PdfStringFormat format = new PdfStringFormat()
28-
{
29-
ParagraphIndent = 35f,
30-
LineSpacing = 20f
31-
};
32-
// Instructional content (long enough to require multiple pages)
19+
// Define the main instructional text.
3320
string overflowText =
34-
@"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals.
21+
@"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals.
22+
3523
Key Features:
3624
- Multi-page automatic content flow using pagination
3725
- Support for rich text formatting: headers, bullets, and tables
3826
- Insert images, tables, and charts seamlessly
3927
- Add interactive elements: bookmarks, hyperlinks, and attachments
4028
- Control layout: margins, page breaks, and dynamic footers
29+
4130
Usage Example:
4231
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+
4333
Adding dynamic footers is useful for:
4434
- Section labeling in large documents
4535
- Including secure or traceable codes for each page
4636
- Ensuring readers always know their page context
37+
4738
Other advanced scenarios:
4839
- Creating Table of Contents with page navigation
4940
- Inserting named destinations for quick jumps
5041
- Using graphics and interactive elements within the same document
42+
5143
Experiment by updating this program to add headers, watermarks, or section-based page numbers based on your specific requirements.
44+
5245
For more information, visit:
5346
https://help.syncfusion.com/file-formats/pdf/working-with-text
5447
https://help.syncfusion.com/file-formats/pdf/working-with-graphics
48+
5549
This concludes the instructional workflow for auto-paginated, footer-enhanced PDF generation in .NET.";
5650

57-
// Draw text with automatic pagination (triggers PageAdded for each extra page)
58-
var textElement = new PdfTextElement(overflowText, contentFont, PdfPens.Black, contentBrush,format);
59-
PdfLayoutFormat layoutFormat = new PdfLayoutFormat
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) =>
6060
{
61-
Layout = PdfLayoutType.Paginate,
62-
PaginateBounds = new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30)
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);
6363
};
64-
textElement.Draw(
65-
section1.Pages[0],
66-
new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30),
67-
layoutFormat
68-
);
69-
// Save and close
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.
7072
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write))
7173
{
7274
document.Save(outputFileStream);
7375
}
7476
document.Close(true);
7577
}
76-
// Handles the PageAdded event to draw a dynamic footer on each page.
77-
static void PageAddedHandler(object sender, PageAddedEventArgs e, int sectionNumber)
78+
79+
// Add header and footer to every page.
80+
static void PageAddedHandler(object sender, PageAddedEventArgs e)
7881
{
7982
PdfPage page = e.Page;
8083
int currentPage = page.Section.Pages.IndexOf(page) + 1;
8184

82-
// Generate a human-readable timestamp for the footer
83-
string timestamp = DateTime.Now.ToString("'Date:' yyyy-MM-dd 'Time:' HH:mm:ss");
84-
string footerText = $"Section {sectionNumber} - Page {currentPage} - {timestamp}";
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+
);
8593

86-
// Draw footer on the current page
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}";
8797
page.Graphics.DrawString(
8898
footerText,
8999
new PdfStandardFont(PdfFontFamily.Helvetica, 12),
90100
new PdfSolidBrush(Color.Black),
91101
new PointF(10, page.GetClientSize().Height - 30)
92102
);
93103
}
94-
}
104+
}

0 commit comments

Comments
 (0)