Skip to content

Commit 18cd3eb

Browse files
committed
961945 Updated proper section code sample
1 parent 7d1c62a commit 18cd3eb

File tree

1 file changed

+34
-24
lines changed
  • Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF

1 file changed

+34
-24
lines changed

Header and Footer/Custom-Headers-and-Footers-in-PDF/.NET Framework/Custom-Headers-and-Footers-in-PDF/Form1.cs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,59 @@ public Form1()
1212

1313
private void btnCreate_Click(object sender, EventArgs e)
1414
{
15-
// Create a new PDF document
16-
PdfDocument document = new PdfDocument();
17-
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
18-
PdfBrush brush = new PdfSolidBrush(Color.Black);
15+
// Create a new PDF document
16+
PdfDocument document = new PdfDocument();
1917

20-
// Paragraph content to display on each page
18+
// Paragraph content to display on each page
2119
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.
20+
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.
21+
Finally, the document’s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree.";
2222

23-
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.
23+
// ===== Section 1 =====
24+
AddSectionWithHeaderFooter(document, "Header - Section 1", "Footer - Section 1", 1, paragraph);
2425

25-
Finally, the document’s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree.";
26+
// ===== Section 2 =====
27+
AddSectionWithHeaderFooter(document, "Header - Section 2", "Footer - Section 2", 1, paragraph);
2628

27-
// Create 3 pages
28-
for (int i = 0; i < 3; i++)
29+
// Save and close the document
30+
document.Save("Output.pdf");
31+
// Close and dispose of the PDF document
32+
document.Close(true);
33+
}
34+
35+
// Adds a specified number of pages to the document, each with a unique header, footer, and paragraph content.
36+
static void AddSectionWithHeaderFooter(PdfDocument document, string headerText, string footerText, int numberOfPages, string pageContent)
37+
{
38+
// Define fonts and brush
39+
PdfFont headerFooterFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold);
40+
PdfFont bodyFont = new PdfStandardFont(PdfFontFamily.Helvetica, 11);
41+
PdfBrush brush = PdfBrushes.Black;
42+
43+
for (int i = 0; i < numberOfPages; i++)
2944
{
45+
// Add a new page
3046
PdfPage page = document.Pages.Add();
3147

32-
// Header
33-
string headerText = $"Header for Page - {i + 1}";
34-
page.Graphics.DrawString(headerText, font, brush, new PointF(10, 10));
48+
// Draw Header
49+
page.Graphics.DrawString(headerText, headerFooterFont, brush, new PointF(10, 10));
3550

36-
// Footer
37-
string footerText = $"Footer for Page - {i + 1}";
38-
page.Graphics.DrawString(footerText, font, brush, new PointF(10, page.GetClientSize().Height - 30));
51+
// Draw Footer
52+
float footerY = page.GetClientSize().Height - 20;
53+
page.Graphics.DrawString(footerText, headerFooterFont, brush, new PointF(10, footerY));
3954

40-
// Draw paragraph content with left alignment
41-
PdfTextElement textElement = new PdfTextElement(paragraph, font, brush)
55+
// Draw the paragraph content using PdfTextElement for proper layout and wrapping
56+
RectangleF contentBounds = new RectangleF(20, 40, page.GetClientSize().Width - 40, page.GetClientSize().Height - 80);
57+
PdfTextElement textElement = new PdfTextElement(pageContent, bodyFont, brush)
4258
{
4359
StringFormat = new PdfStringFormat()
4460
{
4561
Alignment = PdfTextAlignment.Left,
4662
LineSpacing = 5f
4763
}
4864
};
49-
50-
RectangleF contentBounds = new RectangleF(20, 100, page.GetClientSize().Width - 80, page.GetClientSize().Height - 150);
65+
// Draw the paragraph text within the defined rectangle area on the page
5166
textElement.Draw(page, contentBounds);
5267
}
53-
54-
// Save the document
55-
document.Save("Output.pdf");
56-
// Close the PDF document
57-
document.Close(true);
5868
}
5969

6070
}

0 commit comments

Comments
 (0)