Skip to content

Commit fe02e2d

Browse files
Merge pull request #169 from SyncfusionExamples/962072
962072: Added headers to specific sections of a PDF sample code
2 parents 794876a + 7757309 commit fe02e2d

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Header-Management-in-PDF-Sections", "Header-Management-in-PDF-Sections\Header-Management-in-PDF-Sections.csproj", "{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Header_Management_in_PDF_Sections</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

Header and Footer/Header-Management-in-PDF-Sections/.NET/Header-Management-in-PDF-Sections/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)