|
| 1 | +using Syncfusion.Pdf.Graphics; |
| 2 | +using Syncfusion.Pdf; |
| 3 | +using Syncfusion.Drawing; |
| 4 | + |
| 5 | +namespace Header_Management_in_PDF_Sections |
| 6 | +{ |
| 7 | + public class Program |
| 8 | + { |
| 9 | + public static void Main(string[] args) |
| 10 | + { |
| 11 | + // Initialize the PDF document |
| 12 | + PdfDocument document = new PdfDocument(); |
| 13 | + |
| 14 | + // Define the font for the header |
| 15 | + PdfFont headerfont = new PdfStandardFont(PdfFontFamily.Helvetica, 12); |
| 16 | + |
| 17 | + // Create the first section with headers |
| 18 | + PdfSection sectionWithHeaders = document.Sections.Add(); |
| 19 | + // Apply header to this section |
| 20 | + sectionWithHeaders.Template.Top = CreateHeaderTemplate(headerfont); |
| 21 | + |
| 22 | + // Add 5 pages in the first section |
| 23 | + for (int i = 0; i < 5; i++) |
| 24 | + { |
| 25 | + PdfPage page = sectionWithHeaders.Pages.Add(); |
| 26 | + DrawContentOnPage(page, $"Content for page {i + 1} in section with headers"); |
| 27 | + } |
| 28 | + |
| 29 | + // Create the second section without headers |
| 30 | + PdfSection sectionWithoutHeaders = document.Sections.Add(); |
| 31 | + |
| 32 | + // Add 5 pages in the second section |
| 33 | + for (int i = 0; i < 5; i++) |
| 34 | + { |
| 35 | + PdfPage page = sectionWithoutHeaders.Pages.Add(); |
| 36 | + DrawContentOnPage(page, $"Content for page {i + 6} in section without headers"); |
| 37 | + } |
| 38 | + |
| 39 | + // Save the PDF document |
| 40 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write)) |
| 41 | + { |
| 42 | + document.Save(outputFileStream); |
| 43 | + } |
| 44 | + document.Close(true); |
| 45 | + } |
| 46 | + |
| 47 | + private static PdfPageTemplateElement CreateHeaderTemplate(PdfFont headerfont) |
| 48 | + { |
| 49 | + RectangleF rect = new RectangleF(0, 0, 500, 50); // Adjust width and height as needed |
| 50 | + PdfPageTemplateElement header = new PdfPageTemplateElement(rect); |
| 51 | + header.Graphics.DrawString("Header Text", headerfont, PdfBrushes.Black, new PointF(0, 0)); // Customize your header here |
| 52 | + return header; |
| 53 | + } |
| 54 | + |
| 55 | + private static void DrawContentOnPage(PdfPage page, string content) |
| 56 | + { |
| 57 | + PdfGraphics graphics = page.Graphics; |
| 58 | + // You can customize the positioning and styling of your content |
| 59 | + graphics.DrawString(content, new PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.Black, new PointF(0, 100)); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments