diff --git a/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.sln b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.sln new file mode 100644 index 00000000..bd92a6ff --- /dev/null +++ b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF.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}") = "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 + 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/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Adding-dynamic-headers-and-footers-in-PDF.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 new file mode 100644 index 00000000..b026b4e2 --- /dev/null +++ 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 @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + Adding-dynamic-headers-and-footers-in-PDF + enable + enable + + + + + + + diff --git a/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/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 new file mode 100644 index 00000000..e69de29b diff --git a/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Program.cs b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Program.cs new file mode 100644 index 00000000..ed81179a --- /dev/null +++ b/Header and Footer/Adding-dynamic-headers-and-footers-in-PDF/.NET/Adding-dynamic-headers-and-footers-in-PDF/Program.cs @@ -0,0 +1,104 @@ +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; + +class Program +{ + static void Main(string[] args) + { + // Create a new PDF document. + PdfDocument document = new PdfDocument(); + + // Subscribe to the PageAdded event to add header and footer for every page. + document.Pages.PageAdded += (sender, e) => PageAddedHandler(sender, e); + + // Define content font and brush for main text. + PdfFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18); + PdfBrush contentBrush = new PdfSolidBrush(Color.Black); + + // Define the main instructional text. + 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."; + + // 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) => + { + // 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); + }; + + // 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); + } + + // 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; + + // 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 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), + new PdfSolidBrush(Color.Black), + new PointF(10, page.GetClientSize().Height - 30) + ); + } +} \ No newline at end of file