Skip to content

Commit bba51a8

Browse files
Modified the sample
1 parent 001eabc commit bba51a8

File tree

5 files changed

+324
-102
lines changed

5 files changed

+324
-102
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36221.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeaderAndFooterForSpecificPage", "HeaderAndFooterForSpecificPage\HeaderAndFooterForSpecificPage.csproj", "{546D19CF-BDDF-423D-A165-007E02911EF9}"
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+
{546D19CF-BDDF-423D-A165-007E02911EF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{546D19CF-BDDF-423D-A165-007E02911EF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{546D19CF-BDDF-423D-A165-007E02911EF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{546D19CF-BDDF-423D-A165-007E02911EF9}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E1A1C446-BB6A-4714-BE88-CC30C480A0A5}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
body {
6+
font-family: Arial, sans-serif;
7+
line-height: 1.6;
8+
margin: 0;
9+
padding: 20px;
10+
}
11+
.page-break {
12+
page-break-after: always;
13+
}
14+
.chapter {
15+
margin-bottom: 30px;
16+
}
17+
h1 {
18+
color: #2c3e50;
19+
font-size: 24px;
20+
margin-top: 40px;
21+
}
22+
h2 {
23+
color: #3498db;
24+
font-size: 20px;
25+
margin-top: 30px;
26+
}
27+
p {
28+
margin-bottom: 15px;
29+
}
30+
table {
31+
width: 100%;
32+
border-collapse: collapse;
33+
margin: 20px 0;
34+
}
35+
table, th, td {
36+
border: 1px solid #ddd;
37+
}
38+
th, td {
39+
padding: 10px;
40+
text-align: left;
41+
}
42+
img {
43+
max-width: 100%;
44+
height: auto;
45+
margin: 10px 0;
46+
}
47+
</style>
48+
</head>
49+
<body>
50+
51+
<div class="chapter">
52+
<h1>Chapter 1: Introduction to PDF Generation</h1>
53+
<p>This document demonstrates how to create multi-page PDF content using HTML. The content is structured to span exactly 5-6 pages when converted to PDF.</p>
54+
55+
<h2>1.1 Document Structure</h2>
56+
<p>Proper document structure is essential for consistent PDF conversion. We use:</p>
57+
<ul>
58+
<li>Semantic HTML5 elements</li>
59+
<li>CSS for styling and layout</li>
60+
<li>Page break controls where needed</li>
61+
</ul>
62+
63+
<h2>1.2 Page Layout Considerations</h2>
64+
<p>When designing for PDF output, consider:</p>
65+
<table>
66+
<tr>
67+
<th>Element</th>
68+
<th>Consideration</th>
69+
</tr>
70+
<tr>
71+
<td>Images</td>
72+
<td>Use high resolution (300dpi) for print quality</td>
73+
</tr>
74+
<tr>
75+
<td>Tables</td>
76+
<td>Avoid row breaks across pages when possible</td>
77+
</tr>
78+
<tr>
79+
<td>Text</td>
80+
<td>Maintain readable font sizes (10-12pt)</td>
81+
</tr>
82+
</table>
83+
</div>
84+
85+
<div class="page-break"></div>
86+
87+
<div class="chapter">
88+
<h1>Chapter 2: Advanced PDF Features</h1>
89+
90+
<h2>2.1 Headers and Footers</h2>
91+
<p>Headers and footers should be consistent across pages but may need to be suppressed on title pages.</p>
92+
93+
<h2>2.2 Page Numbering</h2>
94+
<p>Page numbers are typically added during PDF generation rather than in the HTML.</p>
95+
96+
<h2>2.3 Multi-column Layout</h2>
97+
<p>CSS columns can be used for newspaper-style layouts:</p>
98+
<div style="column-count: 2; column-gap: 20px; margin: 20px 0;">
99+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in dui mauris. Vivamus hendrerit arcu sed erat molestie vehicula. Sed auctor neque eu tellus rhoncus ut eleifend nibh porttitor.</p>
100+
<p>Ut in nulla enim. Phasellus molestie magna non est bibendum non venenatis nisl tempor. Suspendisse dictum feugiat nisl ut dapibus. Mauris iaculis porttitor posuere.</p>
101+
<p>Praesent id metus massa, ut blandit odio. Proin quis tortor orci. Etiam at risus et justo dignissim congue. Donec congue lacinia dui, a porttitor lectus condimentum laoreet.</p>
102+
</div>
103+
</div>
104+
105+
<div class="page-break"></div>
106+
107+
<div class="chapter">
108+
<h1>Chapter 3: Visual Elements in PDF</h1>
109+
110+
<h2>3.1 Images</h2>
111+
<p>Properly sized images help maintain document flow:</p>
112+
<img src="https://via.placeholder.com/600x200" alt="Sample Image" style="display: block; margin: 0 auto;">
113+
114+
<h2>3.2 Charts and Graphs</h2>
115+
<p>For data visualization, consider using SVG or high-resolution PNG images:</p>
116+
<img src="https://via.placeholder.com/600x300" alt="Sample Chart">
117+
118+
<h2>3.3 Borders and Backgrounds</h2>
119+
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; padding: 15px; margin: 20px 0;">
120+
<p>Styled divs can highlight important content while maintaining print readability.</p>
121+
</div>
122+
</div>
123+
124+
<div class="page-break"></div>
125+
126+
<div class="chapter">
127+
<h1>Chapter 4: Long-form Content</h1>
128+
129+
<h2>4.1 Multi-page Tables</h2>
130+
<p>Tables that span multiple pages should have headers repeated:</p>
131+
<table>
132+
<thead>
133+
<tr>
134+
<th>ID</th>
135+
<th>Name</th>
136+
<th>Description</th>
137+
<th>Value</th>
138+
</tr>
139+
</thead>
140+
<tbody>
141+
<!-- Repeat this block to extend content -->
142+
<tr><td>1</td><td>Item A</td><td>Description of item A</td><td>$100</td></tr>
143+
<tr><td>2</td><td>Item B</td><td>Description of item B</td><td>$200</td></tr>
144+
<tr><td>3</td><td>Item C</td><td>Description of item C</td><td>$300</td></tr>
145+
<!-- Repeat 15-20 times to ensure multi-page content -->
146+
</tbody>
147+
</table>
148+
</div>
149+
150+
<div class="chapter">
151+
<h1>Chapter 5: Conclusion</h1>
152+
<p>This template demonstrates how to structure HTML content that will reliably convert to a 5-6 page PDF document. Key takeaways:</p>
153+
154+
<ul>
155+
<li>Use proper semantic structure</li>
156+
<li>Control page breaks with CSS</li>
157+
<li>Optimize images for print</li>
158+
<li>Test with your PDF converter</li>
159+
</ul>
160+
161+
<h2>5.1 Final Notes</h2>
162+
<p>Always test your HTML-to-PDF conversion with actual content to ensure proper pagination. The exact page count may vary slightly based on:</p>
163+
<ul>
164+
<li>PDF converter settings</li>
165+
<li>Font rendering differences</li>
166+
<li>Margin requirements</li>
167+
</ul>
168+
</div>
169+
170+
</body>
171+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>HeaderAndFooterForSpecificPage</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\sample.html">
17+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.HtmlConverter;
3+
using Syncfusion.Pdf;
4+
using Syncfusion.Pdf.Graphics;
5+
using Syncfusion.Pdf.Parsing;
6+
7+
8+
// Initialize HTML to PDF converter
9+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
10+
//Convert the HTML to PDF without margins.
11+
BlinkConverterSettings settings = new BlinkConverterSettings();
12+
settings.Margin.All = 0;
13+
settings.ViewPortSize = new Size(1024, 768); // Set the viewport size for rendering
14+
htmlConverter.ConverterSettings = settings;
15+
16+
// Load HTML content from a file
17+
string html = File.ReadAllText(Path.GetFullPath(@"Data/Sample.html"));
18+
19+
// Convert HTML to PDF
20+
using (PdfDocument document = htmlConverter.Convert(html, ""))
21+
{
22+
if (Directory.Exists("Output") == false)
23+
{
24+
Directory.CreateDirectory("Output");
25+
}
26+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output-HTML-To-PDF.pdf"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
27+
{
28+
// Save the converted PDF document
29+
document.Save(fileStream);
30+
}
31+
}
32+
htmlConverter.Close();
33+
34+
// Load the source
35+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Output/Output-HTML-To-PDF.pdf"), FileMode.Open, FileAccess.Read))
36+
{
37+
//Load the converted document
38+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream))
39+
{
40+
41+
// Define pages requiring headers
42+
HashSet<int> pagesWithHeaders = new HashSet<int> { 0, 2 }; // 0-based: pages 1 and 3
43+
44+
// Create a new PDF document
45+
using (PdfDocument finalDocument = new PdfDocument())
46+
{
47+
// Create a section for the new document
48+
PdfSection section = finalDocument.Sections.Add();
49+
// Set margins for the section
50+
section.PageSettings.Margins.All = 0;
51+
// Iterate over each page in the original document
52+
for (int i = 0; i < loadedDocument.Pages.Count; i++)
53+
{
54+
bool hasHeaderAndFooter = pagesWithHeaders.Contains(i);
55+
if (hasHeaderAndFooter)
56+
{
57+
// Set the page size to match the original document by allocating space for headers and footer.
58+
section.PageSettings.Size = loadedDocument.Pages[i].Size;
59+
}
60+
else
61+
{
62+
// Set the page size to match the original document
63+
section.PageSettings.Size = loadedDocument.Pages[i].Size;
64+
}
65+
// Create a new page in the final document
66+
PdfPage destPage = section.Pages.Add();
67+
68+
// Copy content from the loaded document
69+
PdfTemplate contentTemplate = loadedDocument.Pages[i].CreateTemplate();
70+
71+
if (hasHeaderAndFooter)
72+
{
73+
//Define the font for header
74+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
75+
//Draw header and footer
76+
AddHeaderAndFooter(destPage, "PDF Page Header Content", "PDF Page Footer Content", font);
77+
//Draw the template
78+
destPage.Graphics.DrawPdfTemplate(contentTemplate, new PointF(0, 50)); //50 Header height
79+
}
80+
else
81+
{
82+
// Draw the content on the destination page
83+
destPage.Graphics.DrawPdfTemplate(contentTemplate, new PointF(0, 0));
84+
}
85+
}
86+
using (FileStream fileStream1 = new FileStream(Path.GetFullPath(@"Output/Output-With-HeaderAndFooter.pdf"), FileMode.Create, FileAccess.ReadWrite))
87+
{
88+
// Save the converted PDF document
89+
finalDocument.Save(fileStream1);
90+
}
91+
}
92+
}
93+
}
94+
95+
static void AddHeaderAndFooter(PdfPage page, string headerText, string footerText, PdfFont font)
96+
{
97+
// Draw header
98+
page.Graphics.DrawLine(PdfPens.Gray, new PointF(0, 50), new PointF(page.GetClientSize().Width, 50));
99+
100+
//Measure the header text
101+
SizeF textSize = font.MeasureString(headerText);
102+
page.Graphics.DrawString(headerText, font, PdfBrushes.Red, new PointF((page.GetClientSize().Width - textSize.Width) / 2, (50 - textSize.Height) / 2));
103+
104+
// Draw footer
105+
page.Graphics.DrawLine(PdfPens.Gray, new PointF(0, page.GetClientSize().Height - 50), new PointF(page.GetClientSize().Width, page.GetClientSize().Height - 50));
106+
page.Graphics.DrawString(footerText, font, PdfBrushes.Green, new PointF(10, page.GetClientSize().Height - 50)); //50 Footer height
107+
}

0 commit comments

Comments
 (0)