|
| 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