From 0fe76c39ee076c1ef79ed440ffaaa6d04ace86c4 Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Mon, 4 Aug 2025 12:23:46 +0530 Subject: [PATCH 1/4] 963596: Added dynamic footers to the sections of the PDF document --- .../.NET/Dynamic-footers-across-PDF-pages.sln | 25 ++++ .../Dynamic-footers-across-PDF-pages.csproj | 15 ++ .../Output/gitkeep.txt | 0 .../Program.cs | 133 ++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln create mode 100644 Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj create mode 100644 Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Output/gitkeep.txt create mode 100644 Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln new file mode 100644 index 00000000..b3fb5b24 --- /dev/null +++ b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36221.1 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +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}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4DF00BDD-6231-4C4E-952C-A40D7E9AC032}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EBE02CCA-44B0-499E-A5F2-8088F932054E} + EndGlobalSection +EndGlobal diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj new file mode 100644 index 00000000..a587f49b --- /dev/null +++ b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + Dynamic_footers_across_PDF_pages + enable + enable + + + + + + + diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Output/gitkeep.txt b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Output/gitkeep.txt new file mode 100644 index 00000000..e69de29b diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs new file mode 100644 index 00000000..ae54d6d2 --- /dev/null +++ b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs @@ -0,0 +1,133 @@ +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +class Program +{ + public static PdfPage page; + + static void Main(string[] args) + { + // Create a new PDF document + PdfDocument document = new PdfDocument(); + + // Create a section and apply bottom margin for footers + PdfSection section1 = document.Sections.Add(); + section1.PageSettings.Margins.Bottom = 30; + + // Attach a custom handler for the PageAdded event + section1.PageAdded += (sender, e) => PageAddedHandler(sender, e, 1); + + // Add the first page to initialize pagination + section1.Pages.Add(); + + // Prepare font and brush for the main content + PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18); + PdfBrush contentBrush = new PdfSolidBrush(Color.Black); + + // Optionally, set up line spacing and paragraph styles + PdfStringFormat format = new PdfStringFormat + { + ParagraphIndent = 35f, + LineSpacing = 20f + }; + // Get example instructional text related to PDF creation + string overflowText = GetLongPdfGuideText(); + // Draw the instructional text using PdfTextElement, which handles pagination + var textElement = new PdfTextElement(overflowText, contentFont, contentBrush); + PdfLayoutFormat layoutFormat = new PdfLayoutFormat + { + Layout = PdfLayoutType.Paginate, // Enables pagination + PaginateBounds = new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30) // Leaves space for footer + }; + + var layoutResult = textElement.Draw(section1.Pages[0], + new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30), layoutFormat); + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + document.Save(outputFileStream); + } + //Close the document. + document.Close(true); + } + + /// + /// Handles the PageAdded event to draw a dynamic footer on each page. + /// + static void PageAddedHandler(object sender, PageAddedEventArgs e, int sectionNumber) + { + PdfPage page = e.Page; + int currentPage = page.Section.Pages.IndexOf(page) + 1; + + // Generate a random alphanumeric code for added uniqueness in the footer + string randomFooter = GenerateRandomFooterCode(); + + string footerText = $"Section {sectionNumber} - Page {currentPage} - {randomFooter}"; + + // Add the footer to the page + DrawFooter(page, footerText); + } + + /// + /// Draws the footer text at the bottom of the specified page. + /// + static void DrawFooter(PdfPage page, string footerText) + { + page.Graphics.DrawString( + footerText, + new PdfStandardFont(PdfFontFamily.Helvetica, 12), + new PdfSolidBrush(Color.Black), + new PointF(10, page.GetClientSize().Height - 30) + ); + } + + /// + /// Generates a random 3-letter code with numbers for footer uniqueness. + /// + static string GenerateRandomFooterCode() + { + Random random = new Random(); + char[] letters = new char[3]; + for (int i = 0; i < 3; i++) + letters[i] = (char)('A' + random.Next(26)); + return new string(letters) + random.Next(100, 1000).ToString(); + } + + /// + /// Returns sample instructional text about PDF generation and features. + /// + static string GetLongPdfGuideText() + { + // This text simulates documentation for PDF feature usage + return @"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals. + +Key Features: +- Multi-page automatic content flow using pagination +- Support for rich text formatting: headers, bullets, and tables +- Insert images, tables, and charts seamlessly +- Add interactive elements: bookmarks, hyperlinks, and attachments +- Control layout: margins, page breaks, and dynamic footers + +Usage Example: +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. + +Adding dynamic footers is useful for: +- Section labeling in large documents +- Including secure or traceable codes for each page +- Ensuring readers always know their page context + +Other advanced scenarios: +- Creating Table of Contents with page navigation +- Inserting named destinations for quick jumps +- Using graphics and interactive elements within the same document + +Experiment by updating this program to add headers, watermarks, or section-based page numbers based on your specific requirements. + +For more information, visit: +https://help.syncfusion.com/file-formats/pdf/working-with-text +https://help.syncfusion.com/file-formats/pdf/working-with-graphics + +This concludes the instructional workflow for auto-paginated, footer-enhanced PDF generation in .NET."; + } +} From 900a2bb559699e9294a7a9a2b1d09f5796871d96 Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Mon, 4 Aug 2025 14:27:53 +0530 Subject: [PATCH 2/4] 963596: Simplify the code. --- .../Program.cs | 133 +++++++----------- 1 file changed, 47 insertions(+), 86 deletions(-) diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs index ae54d6d2..36718fd1 100644 --- a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs +++ b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs @@ -1,133 +1,94 @@ using Syncfusion.Drawing; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; + class Program { - public static PdfPage page; - static void Main(string[] args) { // Create a new PDF document PdfDocument document = new PdfDocument(); - // Create a section and apply bottom margin for footers + // Add a section with space at the bottom for the footer PdfSection section1 = document.Sections.Add(); section1.PageSettings.Margins.Bottom = 30; - // Attach a custom handler for the PageAdded event + // Subscribe to the PageAdded event to add dynamic footer on each new page section1.PageAdded += (sender, e) => PageAddedHandler(sender, e, 1); - // Add the first page to initialize pagination + // Add the first page (the rest will be added with page overflow) section1.Pages.Add(); - // Prepare font and brush for the main content + // Prepare the content font and brush PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18); PdfBrush contentBrush = new PdfSolidBrush(Color.Black); - // Optionally, set up line spacing and paragraph styles - PdfStringFormat format = new PdfStringFormat + // Define formatting for the main content + PdfStringFormat format = new PdfStringFormat() { ParagraphIndent = 35f, LineSpacing = 20f }; - // Get example instructional text related to PDF creation - string overflowText = GetLongPdfGuideText(); - // Draw the instructional text using PdfTextElement, which handles pagination - var textElement = new PdfTextElement(overflowText, contentFont, contentBrush); - PdfLayoutFormat layoutFormat = new PdfLayoutFormat - { - Layout = PdfLayoutType.Paginate, // Enables pagination - PaginateBounds = new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30) // Leaves space for footer - }; - - var layoutResult = textElement.Draw(section1.Pages[0], - new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30), layoutFormat); - //Create file stream. - using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) - { - //Save the PDF document to file stream. - document.Save(outputFileStream); - } - //Close the document. - document.Close(true); - } - - /// - /// Handles the PageAdded event to draw a dynamic footer on each page. - /// - static void PageAddedHandler(object sender, PageAddedEventArgs e, int sectionNumber) - { - PdfPage page = e.Page; - int currentPage = page.Section.Pages.IndexOf(page) + 1; - - // Generate a random alphanumeric code for added uniqueness in the footer - string randomFooter = GenerateRandomFooterCode(); - - string footerText = $"Section {sectionNumber} - Page {currentPage} - {randomFooter}"; - - // Add the footer to the page - DrawFooter(page, footerText); - } - - /// - /// Draws the footer text at the bottom of the specified page. - /// - static void DrawFooter(PdfPage page, string footerText) - { - page.Graphics.DrawString( - footerText, - new PdfStandardFont(PdfFontFamily.Helvetica, 12), - new PdfSolidBrush(Color.Black), - new PointF(10, page.GetClientSize().Height - 30) - ); - } - - /// - /// Generates a random 3-letter code with numbers for footer uniqueness. - /// - static string GenerateRandomFooterCode() - { - Random random = new Random(); - char[] letters = new char[3]; - for (int i = 0; i < 3; i++) - letters[i] = (char)('A' + random.Next(26)); - return new string(letters) + random.Next(100, 1000).ToString(); - } - - /// - /// Returns sample instructional text about PDF generation and features. - /// - static string GetLongPdfGuideText() - { - // This text simulates documentation for PDF feature usage - return @"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals. - + // Instructional content (long enough to require multiple pages) + string overflowText = +@"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals. Key Features: - Multi-page automatic content flow using pagination - Support for rich text formatting: headers, bullets, and tables - Insert images, tables, and charts seamlessly - Add interactive elements: bookmarks, hyperlinks, and attachments - Control layout: margins, page breaks, and dynamic footers - Usage Example: 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. - Adding dynamic footers is useful for: - Section labeling in large documents - Including secure or traceable codes for each page - Ensuring readers always know their page context - Other advanced scenarios: - Creating Table of Contents with page navigation - Inserting named destinations for quick jumps - Using graphics and interactive elements within the same document - Experiment by updating this program to add headers, watermarks, or section-based page numbers based on your specific requirements. - For more information, visit: https://help.syncfusion.com/file-formats/pdf/working-with-text https://help.syncfusion.com/file-formats/pdf/working-with-graphics - This concludes the instructional workflow for auto-paginated, footer-enhanced PDF generation in .NET."; + + // Draw text with automatic pagination (triggers PageAdded for each extra page) + var textElement = new PdfTextElement(overflowText, contentFont, PdfPens.Black, contentBrush,format); + PdfLayoutFormat layoutFormat = new PdfLayoutFormat + { + Layout = PdfLayoutType.Paginate, + PaginateBounds = new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30) + }; + textElement.Draw( + section1.Pages[0], + new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30), + layoutFormat + ); + // Save and close + using (FileStream outputFileStream = new FileStream(@"Output/Output.pdf", FileMode.Create, FileAccess.Write)) + { + document.Save(outputFileStream); + } + document.Close(true); + } + // Handles the PageAdded event to draw a dynamic footer on each page. + static void PageAddedHandler(object sender, PageAddedEventArgs e, int sectionNumber) + { + PdfPage page = e.Page; + int currentPage = page.Section.Pages.IndexOf(page) + 1; + + // Generate a human-readable timestamp for the footer + string timestamp = DateTime.Now.ToString("'Date:' yyyy-MM-dd 'Time:' HH:mm:ss"); + string footerText = $"Section {sectionNumber} - Page {currentPage} - {timestamp}"; + + // Draw footer on the current page + page.Graphics.DrawString( + footerText, + new PdfStandardFont(PdfFontFamily.Helvetica, 12), + new PdfSolidBrush(Color.Black), + new PointF(10, page.GetClientSize().Height - 30) + ); } } From 5b4657011ffd2496c998b0353fd9ea84e8d4bbcc Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Mon, 4 Aug 2025 14:34:44 +0530 Subject: [PATCH 3/4] 963596: Added proper output path. --- .../.NET/Dynamic-footers-across-PDF-pages/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs index 36718fd1..5372c5ac 100644 --- a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs +++ b/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs @@ -67,7 +67,7 @@ static void Main(string[] args) layoutFormat ); // Save and close - using (FileStream outputFileStream = new FileStream(@"Output/Output.pdf", FileMode.Create, FileAccess.Write)) + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write)) { document.Save(outputFileStream); } From 8a1b1ec2ab81b19fa5bedd24e0c308bb4c474f75 Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Tue, 5 Aug 2025 15:07:47 +0530 Subject: [PATCH 4/4] 963596: Added proper code changes. --- ...ng-dynamic-headers-and-footers-in-PDF.sln} | 2 +- ...dynamic-headers-and-footers-in-PDF.csproj} | 2 +- .../Output/gitkeep.txt | 0 .../Program.cs | 84 +++++++++++-------- 4 files changed, 49 insertions(+), 39 deletions(-) rename Header and Footer/{Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln => Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.sln} (80%) rename Header and Footer/{Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj => Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Adding-dynamic-headers-and-footers-in-PDF.csproj} (81%) rename Header and Footer/{Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages => Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF}/Output/gitkeep.txt (100%) rename Header and Footer/{Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages => Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF}/Program.cs (57%) diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.sln similarity index 80% rename from Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln rename to Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.sln index b3fb5b24..bd92a6ff 100644 --- a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages.sln +++ b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.14.36221.1 d17.14 MinimumVisualStudioVersion = 10.0.40219.1 -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}" +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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Adding-dynamic-headers-and-footers-in-PDF.csproj similarity index 81% rename from Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj rename to Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Adding-dynamic-headers-and-footers-in-PDF.csproj index a587f49b..b026b4e2 100644 --- a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Dynamic-footers-across-PDF-pages.csproj +++ b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Adding-dynamic-headers-and-footers-in-PDF.csproj @@ -3,7 +3,7 @@ Exe net8.0 - Dynamic_footers_across_PDF_pages + Adding-dynamic-headers-and-footers-in-PDF enable enable diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Output/gitkeep.txt b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Output/gitkeep.txt similarity index 100% rename from Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Output/gitkeep.txt rename to Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Output/gitkeep.txt diff --git a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Program.cs similarity index 57% rename from Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs rename to Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Program.cs index 5372c5ac..ed81179a 100644 --- a/Header and Footer/Dynamic-footers-across-PDF-pages/.NET/Dynamic-footers-across-PDF-pages/Program.cs +++ b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Program.cs @@ -6,84 +6,94 @@ class Program { static void Main(string[] args) { - // Create a new PDF document + // Create a new PDF document. PdfDocument document = new PdfDocument(); - // Add a section with space at the bottom for the footer - PdfSection section1 = document.Sections.Add(); - section1.PageSettings.Margins.Bottom = 30; + // Subscribe to the PageAdded event to add header and footer for every page. + document.Pages.PageAdded += (sender, e) => PageAddedHandler(sender, e); - // Subscribe to the PageAdded event to add dynamic footer on each new page - section1.PageAdded += (sender, e) => PageAddedHandler(sender, e, 1); - - // Add the first page (the rest will be added with page overflow) - section1.Pages.Add(); - - // Prepare the content font and brush + // Define content font and brush for main text. PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18); PdfBrush contentBrush = new PdfSolidBrush(Color.Black); - // Define formatting for the main content - PdfStringFormat format = new PdfStringFormat() - { - ParagraphIndent = 35f, - LineSpacing = 20f - }; - // Instructional content (long enough to require multiple pages) + // Define the main instructional text. string overflowText = -@"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals. + @"Creating PDF documentation programmatically with Syncfusion .NET libraries enables automation of reports, invoices, and technical manuals. + Key Features: - Multi-page automatic content flow using pagination - Support for rich text formatting: headers, bullets, and tables - Insert images, tables, and charts seamlessly - Add interactive elements: bookmarks, hyperlinks, and attachments - Control layout: margins, page breaks, and dynamic footers + Usage Example: 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. + Adding dynamic footers is useful for: - Section labeling in large documents - Including secure or traceable codes for each page - Ensuring readers always know their page context + Other advanced scenarios: - Creating Table of Contents with page navigation - Inserting named destinations for quick jumps - Using graphics and interactive elements within the same document + Experiment by updating this program to add headers, watermarks, or section-based page numbers based on your specific requirements. + For more information, visit: https://help.syncfusion.com/file-formats/pdf/working-with-text https://help.syncfusion.com/file-formats/pdf/working-with-graphics + This concludes the instructional workflow for auto-paginated, footer-enhanced PDF generation in .NET."; - // Draw text with automatic pagination (triggers PageAdded for each extra page) - var textElement = new PdfTextElement(overflowText, contentFont, PdfPens.Black, contentBrush,format); - PdfLayoutFormat layoutFormat = new PdfLayoutFormat + // Set the header and footer height + float headerHeight = 40f; + float footerHeight = 30f; + + // Create a text element for automatic pagination. + PdfTextElement textElement = new PdfTextElement(overflowText, contentFont, contentBrush); + + // Subscribe to the BeginPageLayout event to offset text on each new page below the header. + textElement.BeginPageLayout += (sender, args) => { - Layout = PdfLayoutType.Paginate, - PaginateBounds = new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30) + // Always start content BELOW the header on every page. + args.Bounds = new RectangleF(0, headerHeight, args.Page.GetClientSize().Width, args.Page.GetClientSize().Height - headerHeight - footerHeight); }; - textElement.Draw( - section1.Pages[0], - new RectangleF(0, 0, section1.Pages[0].GetClientSize().Width, section1.Pages[0].GetClientSize().Height - 30), - layoutFormat - ); - // Save and close + + // Add the first page. + PdfPage firstPage = document.Pages.Add(); + + // Start drawing content (pagination and event will handle rest). + textElement.Draw(firstPage, new PointF(0, headerHeight)); + + // Save and close the document. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write)) { document.Save(outputFileStream); } document.Close(true); } - // Handles the PageAdded event to draw a dynamic footer on each page. - static void PageAddedHandler(object sender, PageAddedEventArgs e, int sectionNumber) + + // Add header and footer to every page. + static void PageAddedHandler(object sender, PageAddedEventArgs e) { PdfPage page = e.Page; int currentPage = page.Section.Pages.IndexOf(page) + 1; - // Generate a human-readable timestamp for the footer - string timestamp = DateTime.Now.ToString("'Date:' yyyy-MM-dd 'Time:' HH:mm:ss"); - string footerText = $"Section {sectionNumber} - Page {currentPage} - {timestamp}"; + // Draw header at the top (within reserved header bounds). + string headerText = $"This is the header - Page {currentPage}"; + page.Graphics.DrawString( + headerText, + new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold), + new PdfSolidBrush(Color.DimGray), + new PointF(10, 10) // Within header area + ); - // Draw footer on the current page + // Draw footer at the bottom (within reserved footer area). + string timestamp = DateTime.Now.ToString("'Date:' yyyy-MM-dd 'Time:' HH:mm:ss"); + string footerText = $"Page {currentPage} {timestamp}"; page.Graphics.DrawString( footerText, new PdfStandardFont(PdfFontFamily.Helvetica, 12), @@ -91,4 +101,4 @@ static void PageAddedHandler(object sender, PageAddedEventArgs e, int sectionNum new PointF(10, page.GetClientSize().Height - 30) ); } -} +} \ No newline at end of file