Skip to content

962072: Added headers to specific sections of a PDF sample code #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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}") = "Header-Management-in-PDF-Sections", "Header-Management-in-PDF-Sections\Header-Management-in-PDF-Sections.csproj", "{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06CEE8F2-2719-4BA0-982D-04E1F7AAEFDA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Header_Management_in_PDF_Sections</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Drawing;

namespace Header_Management_in_PDF_Sections
{
public class Program
{
public static void Main(string[] args)
{
// Initialize the PDF document
PdfDocument document = new PdfDocument();

// Define the font for the header
PdfFont headerfont = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

// Create the first section with headers
PdfSection sectionWithHeaders = document.Sections.Add();
// Apply header to this section
sectionWithHeaders.Template.Top = CreateHeaderTemplate(headerfont);

// Add 5 pages in the first section
for (int i = 0; i < 5; i++)
{
PdfPage page = sectionWithHeaders.Pages.Add();
DrawContentOnPage(page, $"Content for page {i + 1} in section with headers");
}

// Create the second section without headers
PdfSection sectionWithoutHeaders = document.Sections.Add();

// Add 5 pages in the second section
for (int i = 0; i < 5; i++)
{
PdfPage page = sectionWithoutHeaders.Pages.Add();
DrawContentOnPage(page, $"Content for page {i + 6} in section without headers");
}

// Save the PDF document
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write))
{
document.Save(outputFileStream);
}
document.Close(true);
}

private static PdfPageTemplateElement CreateHeaderTemplate(PdfFont headerfont)
{
RectangleF rect = new RectangleF(0, 0, 500, 50); // Adjust width and height as needed
PdfPageTemplateElement header = new PdfPageTemplateElement(rect);
header.Graphics.DrawString("Header Text", headerfont, PdfBrushes.Black, new PointF(0, 0)); // Customize your header here
return header;
}

private static void DrawContentOnPage(PdfPage page, string content)
{
PdfGraphics graphics = page.Graphics;
// You can customize the positioning and styling of your content
graphics.DrawString(content, new PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.Black, new PointF(0, 100));
}
}
}
Loading