From 7d1c62a82608bcebd013253b16f59a8c0367c422 Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Thu, 12 Jun 2025 12:52:31 +0530 Subject: [PATCH 1/2] 961945: Added sample for custom header and footer in PDF document --- .../Custom-Headers-and-Footers-in-PDF.sln | 22 ++++ .../Custom-Headers-and-Footers-in-PDF.csproj | 16 +++ ...tom-Headers-and-Footers-in-PDF.csproj.user | 8 ++ .../Form1.Designer.cs | 63 +++++++++ .../Form1.cs | 61 +++++++++ .../Form1.resx | 120 ++++++++++++++++++ .../Program.cs | 17 +++ 7 files changed, 307 insertions(+) create mode 100644 Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF.sln create mode 100644 Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj create mode 100644 Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj.user create mode 100644 Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.Designer.cs create mode 100644 Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs create mode 100644 Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.resx create mode 100644 Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Program.cs diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF.sln b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF.sln new file mode 100644 index 00000000..9b395286 --- /dev/null +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35707.178 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Custom-Headers-and-Footers-in-PDF", "Custom-Headers-and-Footers-in-PDF\Custom-Headers-and-Footers-in-PDF.csproj", "{6A94ADAE-5F1B-42A5-9F82-2CBB511B2F82}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6A94ADAE-5F1B-42A5-9F82-2CBB511B2F82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A94ADAE-5F1B-42A5-9F82-2CBB511B2F82}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A94ADAE-5F1B-42A5-9F82-2CBB511B2F82}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A94ADAE-5F1B-42A5-9F82-2CBB511B2F82}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj new file mode 100644 index 00000000..0453a4ad --- /dev/null +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj @@ -0,0 +1,16 @@ + + + + WinExe + net8.0-windows + Custom_Headers_and_Footers_in_PDF + enable + true + enable + + + + + + + \ No newline at end of file diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj.user b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj.user new file mode 100644 index 00000000..7814ea24 --- /dev/null +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Custom-Headers-and-Footers-in-PDF.csproj.user @@ -0,0 +1,8 @@ + + + + + Form + + + diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.Designer.cs b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.Designer.cs new file mode 100644 index 00000000..40fcadfa --- /dev/null +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.Designer.cs @@ -0,0 +1,63 @@ +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf; + +namespace Custom_Headers_and_Footers_in_PDF +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "Form1"; + btnCreate = new Button(); + label = new Label(); + + //Label + label.Location = new System.Drawing.Point(0, 40); + label.Size = new System.Drawing.Size(426, 35); + label.Text = "Click the button to generate PDF document"; + label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + + //Button + btnCreate.Location = new System.Drawing.Point(180, 110); + btnCreate.Size = new System.Drawing.Size(85, 26); + btnCreate.Text = "Create PDF"; + btnCreate.Click += new EventHandler(btnCreate_Click); + + //Create PDF + ClientSize = new System.Drawing.Size(450, 150); + Controls.Add(label); + Controls.Add(btnCreate); + Text = "Create PDF"; + } + private Button btnCreate; + private Label label; + + #endregion + } +} diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs new file mode 100644 index 00000000..47f98f15 --- /dev/null +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs @@ -0,0 +1,61 @@ +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; + +namespace Custom_Headers_and_Footers_in_PDF +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + private void btnCreate_Click(object sender, EventArgs e) + { + // Create a new PDF document + PdfDocument document = new PdfDocument(); + PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); + PdfBrush brush = new PdfSolidBrush(Color.Black); + + // Paragraph content to display on each page + string paragraph = @"The page tree serves as the root of the document. In the simplest case, it is just a list of the pages in the document. Each page is defined as an independent entity with metadata (e.g., page dimensions) and a reference to its resources and content, which are defined separately. Together, the page tree and page objects create the “paper” that composes the document. + + Resources are objects that are required to render a page. For example, a single font is typically used across several pages, so storing the font information in an external resource is much more efficient. A content object defines the text and graphics that actually show up on the page. Together, content objects and resources define the appearance of an individual page. + + Finally, the document’s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree."; + + // Create 3 pages + for (int i = 0; i < 3; i++) + { + PdfPage page = document.Pages.Add(); + + // Header + string headerText = $"Header for Page - {i + 1}"; + page.Graphics.DrawString(headerText, font, brush, new PointF(10, 10)); + + // Footer + string footerText = $"Footer for Page - {i + 1}"; + page.Graphics.DrawString(footerText, font, brush, new PointF(10, page.GetClientSize().Height - 30)); + + // Draw paragraph content with left alignment + PdfTextElement textElement = new PdfTextElement(paragraph, font, brush) + { + StringFormat = new PdfStringFormat() + { + Alignment = PdfTextAlignment.Left, + LineSpacing = 5f + } + }; + + RectangleF contentBounds = new RectangleF(20, 100, page.GetClientSize().Width - 80, page.GetClientSize().Height - 150); + textElement.Draw(page, contentBounds); + } + + // Save the document + document.Save("Output.pdf"); + // Close the PDF document + document.Close(true); + } + + } +} diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.resx b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.resx new file mode 100644 index 00000000..1af7de15 --- /dev/null +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Program.cs b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Program.cs new file mode 100644 index 00000000..a58a7659 --- /dev/null +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Program.cs @@ -0,0 +1,17 @@ +namespace Custom_Headers_and_Footers_in_PDF +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); + } + } +} \ No newline at end of file From 18cd3eb7f19f42839a019b5e51289ad983f9f171 Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Fri, 13 Jun 2025 10:42:20 +0530 Subject: [PATCH 2/2] 961945 Updated proper section code sample --- .../Form1.cs | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs index 47f98f15..e87d6745 100644 --- a/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs +++ b/Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs @@ -12,33 +12,49 @@ public Form1() private void btnCreate_Click(object sender, EventArgs e) { - // Create a new PDF document - PdfDocument document = new PdfDocument(); - PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); - PdfBrush brush = new PdfSolidBrush(Color.Black); + // Create a new PDF document + PdfDocument document = new PdfDocument(); - // Paragraph content to display on each page + // Paragraph content to display on each page string paragraph = @"The page tree serves as the root of the document. In the simplest case, it is just a list of the pages in the document. Each page is defined as an independent entity with metadata (e.g., page dimensions) and a reference to its resources and content, which are defined separately. Together, the page tree and page objects create the “paper” that composes the document. +Resources are objects that are required to render a page. For example, a single font is typically used across several pages, so storing the font information in an external resource is much more efficient. A content object defines the text and graphics that actually show up on the page. Together, content objects and resources define the appearance of an individual page. +Finally, the document’s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree."; - Resources are objects that are required to render a page. For example, a single font is typically used across several pages, so storing the font information in an external resource is much more efficient. A content object defines the text and graphics that actually show up on the page. Together, content objects and resources define the appearance of an individual page. + // ===== Section 1 ===== + AddSectionWithHeaderFooter(document, "Header - Section 1", "Footer - Section 1", 1, paragraph); - Finally, the document’s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree."; + // ===== Section 2 ===== + AddSectionWithHeaderFooter(document, "Header - Section 2", "Footer - Section 2", 1, paragraph); - // Create 3 pages - for (int i = 0; i < 3; i++) + // Save and close the document + document.Save("Output.pdf"); + // Close and dispose of the PDF document + document.Close(true); + } + + // Adds a specified number of pages to the document, each with a unique header, footer, and paragraph content. + static void AddSectionWithHeaderFooter(PdfDocument document, string headerText, string footerText, int numberOfPages, string pageContent) + { + // Define fonts and brush + PdfFont headerFooterFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold); + PdfFont bodyFont = new PdfStandardFont(PdfFontFamily.Helvetica, 11); + PdfBrush brush = PdfBrushes.Black; + + for (int i = 0; i < numberOfPages; i++) { + // Add a new page PdfPage page = document.Pages.Add(); - // Header - string headerText = $"Header for Page - {i + 1}"; - page.Graphics.DrawString(headerText, font, brush, new PointF(10, 10)); + // Draw Header + page.Graphics.DrawString(headerText, headerFooterFont, brush, new PointF(10, 10)); - // Footer - string footerText = $"Footer for Page - {i + 1}"; - page.Graphics.DrawString(footerText, font, brush, new PointF(10, page.GetClientSize().Height - 30)); + // Draw Footer + float footerY = page.GetClientSize().Height - 20; + page.Graphics.DrawString(footerText, headerFooterFont, brush, new PointF(10, footerY)); - // Draw paragraph content with left alignment - PdfTextElement textElement = new PdfTextElement(paragraph, font, brush) + // Draw the paragraph content using PdfTextElement for proper layout and wrapping + RectangleF contentBounds = new RectangleF(20, 40, page.GetClientSize().Width - 40, page.GetClientSize().Height - 80); + PdfTextElement textElement = new PdfTextElement(pageContent, bodyFont, brush) { StringFormat = new PdfStringFormat() { @@ -46,15 +62,9 @@ private void btnCreate_Click(object sender, EventArgs e) LineSpacing = 5f } }; - - RectangleF contentBounds = new RectangleF(20, 100, page.GetClientSize().Width - 80, page.GetClientSize().Height - 150); + // Draw the paragraph text within the defined rectangle area on the page textElement.Draw(page, contentBounds); } - - // Save the document - document.Save("Output.pdf"); - // Close the PDF document - document.Close(true); } }