Skip to content

Commit 900a2bb

Browse files
committed
963596: Simplify the code.
1 parent 0fe76c3 commit 900a2bb

File tree

1 file changed

+47
-86
lines changed
  • Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages

1 file changed

+47
-86
lines changed
Lines changed: 47 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,94 @@
11
using Syncfusion.Drawing;
22
using Syncfusion.Pdf;
33
using Syncfusion.Pdf.Graphics;
4+
45
class Program
56
{
6-
public static PdfPage page;
7-
87
static void Main(string[] args)
98
{
109
// Create a new PDF document
1110
PdfDocument document = new PdfDocument();
1211

13-
// Create a section and apply bottom margin for footers
12+
// Add a section with space at the bottom for the footer
1413
PdfSection section1 = document.Sections.Add();
1514
section1.PageSettings.Margins.Bottom = 30;
1615

17-
// Attach a custom handler for the PageAdded event
16+
// Subscribe to the PageAdded event to add dynamic footer on each new page
1817
section1.PageAdded += (sender, e) => PageAddedHandler(sender, e, 1);
1918

20-
// Add the first page to initialize pagination
19+
// Add the first page (the rest will be added with page overflow)
2120
section1.Pages.Add();
2221

23-
// Prepare font and brush for the main content
22+
// Prepare the content font and brush
2423
PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18);
2524
PdfBrush contentBrush = new PdfSolidBrush(Color.Black);
2625

27-
// Optionally, set up line spacing and paragraph styles
28-
PdfStringFormat format = new PdfStringFormat
26+
// Define formatting for the main content
27+
PdfStringFormat format = new PdfStringFormat()
2928
{
3029
ParagraphIndent = 35f,
3130
LineSpacing = 20f
3231
};
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-
32+
// Instructional content (long enough to require multiple pages)
33+
string overflowText =
34+
@"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals.
10535
Key Features:
10636
- Multi-page automatic content flow using pagination
10737
- Support for rich text formatting: headers, bullets, and tables
10838
- Insert images, tables, and charts seamlessly
10939
- Add interactive elements: bookmarks, hyperlinks, and attachments
11040
- Control layout: margins, page breaks, and dynamic footers
111-
11241
Usage Example:
11342
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-
11543
Adding dynamic footers is useful for:
11644
- Section labeling in large documents
11745
- Including secure or traceable codes for each page
11846
- Ensuring readers always know their page context
119-
12047
Other advanced scenarios:
12148
- Creating Table of Contents with page navigation
12249
- Inserting named destinations for quick jumps
12350
- Using graphics and interactive elements within the same document
124-
12551
Experiment by updating this program to add headers, watermarks, or section-based page numbers based on your specific requirements.
126-
12752
For more information, visit:
12853
https://help.syncfusion.com/file-formats/pdf/working-with-text
12954
https://help.syncfusion.com/file-formats/pdf/working-with-graphics
130-
13155
This concludes the instructional workflow for auto-paginated, footer-enhanced PDF generation in .NET.";
56+
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
60+
{
61+
Layout = PdfLayoutType.Paginate,
62+
PaginateBounds = new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30)
63+
};
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
70+
using (FileStream outputFileStream = new FileStream(@"Output/Output.pdf", FileMode.Create, FileAccess.Write))
71+
{
72+
document.Save(outputFileStream);
73+
}
74+
document.Close(true);
75+
}
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+
PdfPage page = e.Page;
80+
int currentPage = page.Section.Pages.IndexOf(page) + 1;
81+
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+
86+
// Draw footer on the current page
87+
page.Graphics.DrawString(
88+
footerText,
89+
new PdfStandardFont(PdfFontFamily.Helvetica, 12),
90+
new PdfSolidBrush(Color.Black),
91+
new PointF(10, page.GetClientSize().Height - 30)
92+
);
13293
}
13394
}

0 commit comments

Comments
 (0)