diff --git a/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage.sln b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage.sln new file mode 100644 index 00000000..d8c4b6dd --- /dev/null +++ b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36221.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeaderAndFooterForSpecificPage", "HeaderAndFooterForSpecificPage\HeaderAndFooterForSpecificPage.csproj", "{546D19CF-BDDF-423D-A165-007E02911EF9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {546D19CF-BDDF-423D-A165-007E02911EF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {546D19CF-BDDF-423D-A165-007E02911EF9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {546D19CF-BDDF-423D-A165-007E02911EF9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {546D19CF-BDDF-423D-A165-007E02911EF9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E1A1C446-BB6A-4714-BE88-CC30C480A0A5} + EndGlobalSection +EndGlobal diff --git a/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Data/sample.html b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Data/sample.html new file mode 100644 index 00000000..909b56e0 --- /dev/null +++ b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Data/sample.html @@ -0,0 +1,171 @@ + + + + + + + +
+

Chapter 1: Introduction to PDF Generation

+

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.

+ +

1.1 Document Structure

+

Proper document structure is essential for consistent PDF conversion. We use:

+ + +

1.2 Page Layout Considerations

+

When designing for PDF output, consider:

+ + + + + + + + + + + + + + + + + +
ElementConsideration
ImagesUse high resolution (300dpi) for print quality
TablesAvoid row breaks across pages when possible
TextMaintain readable font sizes (10-12pt)
+
+ +
+ +
+

Chapter 2: Advanced PDF Features

+ +

2.1 Headers and Footers

+

Headers and footers should be consistent across pages but may need to be suppressed on title pages.

+ +

2.2 Page Numbering

+

Page numbers are typically added during PDF generation rather than in the HTML.

+ +

2.3 Multi-column Layout

+

CSS columns can be used for newspaper-style layouts:

+
+

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.

+

Ut in nulla enim. Phasellus molestie magna non est bibendum non venenatis nisl tempor. Suspendisse dictum feugiat nisl ut dapibus. Mauris iaculis porttitor posuere.

+

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.

+
+
+ +
+ +
+

Chapter 3: Visual Elements in PDF

+ +

3.1 Images

+

Properly sized images help maintain document flow:

+ Sample Image + +

3.2 Charts and Graphs

+

For data visualization, consider using SVG or high-resolution PNG images:

+ Sample Chart + +

3.3 Borders and Backgrounds

+
+

Styled divs can highlight important content while maintaining print readability.

+
+
+ +
+ +
+

Chapter 4: Long-form Content

+ +

4.1 Multi-page Tables

+

Tables that span multiple pages should have headers repeated:

+ + + + + + + + + + + + + + + + +
IDNameDescriptionValue
1Item ADescription of item A$100
2Item BDescription of item B$200
3Item CDescription of item C$300
+
+ +
+

Chapter 5: Conclusion

+

This template demonstrates how to structure HTML content that will reliably convert to a 5-6 page PDF document. Key takeaways:

+ + + +

5.1 Final Notes

+

Always test your HTML-to-PDF conversion with actual content to ensure proper pagination. The exact page count may vary slightly based on:

+ +
+ + + \ No newline at end of file diff --git a/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/HeaderAndFooterForSpecificPage.csproj b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/HeaderAndFooterForSpecificPage.csproj new file mode 100644 index 00000000..d20cac9b --- /dev/null +++ b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/HeaderAndFooterForSpecificPage.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + HeaderAndFooterForSpecificPage + enable + enable + + + + + + + + + PreserveNewest + + + + diff --git a/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Output/gitkeep.txt b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Output/gitkeep.txt new file mode 100644 index 00000000..e69de29b diff --git a/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Program.cs b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Program.cs new file mode 100644 index 00000000..6567afc3 --- /dev/null +++ b/HTML to PDF/Blink/Header-And-Footer-For-Specific-Page/.NET/HeaderAndFooterForSpecificPage/Program.cs @@ -0,0 +1,107 @@ +using Syncfusion.Drawing; +using Syncfusion.HtmlConverter; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Parsing; + + + // Initialize HTML to PDF converter + HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); + //Convert the HTML to PDF without margins. + BlinkConverterSettings settings = new BlinkConverterSettings(); + settings.Margin.All = 0; + settings.ViewPortSize = new Size(1024, 768); // Set the viewport size for rendering + htmlConverter.ConverterSettings = settings; + + // Load HTML content from a file + string html = File.ReadAllText(Path.GetFullPath(@"Data/Sample.html")); + + // Convert HTML to PDF + using (PdfDocument document = htmlConverter.Convert(html, "")) + { + if (Directory.Exists("Output") == false) + { + Directory.CreateDirectory("Output"); + } + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output-HTML-To-PDF.pdf"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + // Save the converted PDF document + document.Save(fileStream); + } + } + htmlConverter.Close(); + +// Load the source +using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Output/Output-HTML-To-PDF.pdf"), FileMode.Open, FileAccess.Read)) +{ + //Load the converted document + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream)) + { + + // Define pages requiring headers + HashSet pagesWithHeaders = new HashSet { 0, 2 }; // 0-based: pages 1 and 3 + + // Create a new PDF document + using (PdfDocument finalDocument = new PdfDocument()) + { + // Create a section for the new document + PdfSection section = finalDocument.Sections.Add(); + // Set margins for the section + section.PageSettings.Margins.All = 0; + // Iterate over each page in the original document + for (int i = 0; i < loadedDocument.Pages.Count; i++) + { + bool hasHeaderAndFooter = pagesWithHeaders.Contains(i); + if (hasHeaderAndFooter) + { + // Set the page size to match the original document by allocating space for headers and footer. + section.PageSettings.Size = loadedDocument.Pages[i].Size; + } + else + { + // Set the page size to match the original document + section.PageSettings.Size = loadedDocument.Pages[i].Size; + } + // Create a new page in the final document + PdfPage destPage = section.Pages.Add(); + + // Copy content from the loaded document + PdfTemplate contentTemplate = loadedDocument.Pages[i].CreateTemplate(); + + if (hasHeaderAndFooter) + { + //Define the font for header + PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); + //Draw header and footer + AddHeaderAndFooter(destPage, "PDF Page Header Content", "PDF Page Footer Content", font); + //Draw the template + destPage.Graphics.DrawPdfTemplate(contentTemplate, new PointF(0, 50)); //50 Header height + } + else + { + // Draw the content on the destination page + destPage.Graphics.DrawPdfTemplate(contentTemplate, new PointF(0, 0)); + } + } + using (FileStream fileStream1 = new FileStream(Path.GetFullPath(@"Output/Output-With-HeaderAndFooter.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + // Save the converted PDF document + finalDocument.Save(fileStream1); + } + } + } +} + +static void AddHeaderAndFooter(PdfPage page, string headerText, string footerText, PdfFont font) +{ + // Draw header + page.Graphics.DrawLine(PdfPens.Gray, new PointF(0, 50), new PointF(page.GetClientSize().Width, 50)); + + //Measure the header text + SizeF textSize = font.MeasureString(headerText); + page.Graphics.DrawString(headerText, font, PdfBrushes.Red, new PointF((page.GetClientSize().Width - textSize.Width) / 2, (50 - textSize.Height) / 2)); + + // Draw footer + page.Graphics.DrawLine(PdfPens.Gray, new PointF(0, page.GetClientSize().Height - 50), new PointF(page.GetClientSize().Width, page.GetClientSize().Height - 50)); + page.Graphics.DrawString(footerText, font, PdfBrushes.Green, new PointF(10, page.GetClientSize().Height - 50)); //50 Footer height +}