Skip to footer content

How to Convert HTML to PDF with C# in .NET 8

// Ensure you have the IronPDF library installed.
// You can install IronPDF through NuGet Package Manager.

using IronPdf;
using System;

namespace PdfGenerationExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize Renderer
            var Renderer = new IronPdf.ChromePdfRenderer();

            // Render an HTML string as a PDF
            var htmlContent = "<h1>Welcome to PDF Generation</h1><p>This PDF was generated using IronPDF in a .NET 8 application.</p>";

            // Create PDF from HTML string
            var pdf = Renderer.RenderHtmlAsPdf(htmlContent);

            // Save PDF to file
            pdf.SaveAs("HtmlStringExample.pdf");

            // Convert a live URL to a PDF document
            var urlToConvert = "https://example.com";
            var pdfFromUrl = Renderer.RenderUrlAsPdf(urlToConvert);

            // Save PDF to file
            pdfFromUrl.SaveAs("WebsiteExample.pdf");

            // Illustration: Create and configure a new PDF document with custom settings
            Renderer.RenderingOptions.MarginTop = 20;        // Set top margin
            Renderer.RenderingOptions.MarginBottom = 20;     // Set bottom margin
            Renderer.RenderingOptions.Header = new SimpleHeaderFooter()
            {
                CenterText = "Sample Header",
                DrawDividerLine = true,
                FontSize = 12
            };                                                // Configure header
            Renderer.RenderingOptions.Footer = new SimpleHeaderFooter()
            {
                CenterText = "Page {page} of {total-pages}",
                DrawDividerLine = true,
                FontSize = 10
            };                                                // Configure footer

            // Example: Merge PDFs
            var pdf1 = PdfDocument.FromFile("HtmlStringExample.pdf");
            var pdf2 = PdfDocument.FromFile("WebsiteExample.pdf");
            var mergedPdf = PdfDocument.Merge(pdf1, pdf2);

            // Save the merged PDF
            mergedPdf.SaveAs("MergedDocuments.pdf");

            Console.WriteLine("PDF files have been created, saved, and merged successfully.");
        }
    }
}
// Ensure you have the IronPDF library installed.
// You can install IronPDF through NuGet Package Manager.

using IronPdf;
using System;

namespace PdfGenerationExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize Renderer
            var Renderer = new IronPdf.ChromePdfRenderer();

            // Render an HTML string as a PDF
            var htmlContent = "<h1>Welcome to PDF Generation</h1><p>This PDF was generated using IronPDF in a .NET 8 application.</p>";

            // Create PDF from HTML string
            var pdf = Renderer.RenderHtmlAsPdf(htmlContent);

            // Save PDF to file
            pdf.SaveAs("HtmlStringExample.pdf");

            // Convert a live URL to a PDF document
            var urlToConvert = "https://example.com";
            var pdfFromUrl = Renderer.RenderUrlAsPdf(urlToConvert);

            // Save PDF to file
            pdfFromUrl.SaveAs("WebsiteExample.pdf");

            // Illustration: Create and configure a new PDF document with custom settings
            Renderer.RenderingOptions.MarginTop = 20;        // Set top margin
            Renderer.RenderingOptions.MarginBottom = 20;     // Set bottom margin
            Renderer.RenderingOptions.Header = new SimpleHeaderFooter()
            {
                CenterText = "Sample Header",
                DrawDividerLine = true,
                FontSize = 12
            };                                                // Configure header
            Renderer.RenderingOptions.Footer = new SimpleHeaderFooter()
            {
                CenterText = "Page {page} of {total-pages}",
                DrawDividerLine = true,
                FontSize = 10
            };                                                // Configure footer

            // Example: Merge PDFs
            var pdf1 = PdfDocument.FromFile("HtmlStringExample.pdf");
            var pdf2 = PdfDocument.FromFile("WebsiteExample.pdf");
            var mergedPdf = PdfDocument.Merge(pdf1, pdf2);

            // Save the merged PDF
            mergedPdf.SaveAs("MergedDocuments.pdf");

            Console.WriteLine("PDF files have been created, saved, and merged successfully.");
        }
    }
}
' Ensure you have the IronPDF library installed.
' You can install IronPDF through NuGet Package Manager.

Imports IronPdf
Imports System

Namespace PdfGenerationExample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Initialize Renderer
			Dim Renderer = New IronPdf.ChromePdfRenderer()

			' Render an HTML string as a PDF
			Dim htmlContent = "<h1>Welcome to PDF Generation</h1><p>This PDF was generated using IronPDF in a .NET 8 application.</p>"

			' Create PDF from HTML string
			Dim pdf = Renderer.RenderHtmlAsPdf(htmlContent)

			' Save PDF to file
			pdf.SaveAs("HtmlStringExample.pdf")

			' Convert a live URL to a PDF document
			Dim urlToConvert = "https://example.com"
			Dim pdfFromUrl = Renderer.RenderUrlAsPdf(urlToConvert)

			' Save PDF to file
			pdfFromUrl.SaveAs("WebsiteExample.pdf")

			' Illustration: Create and configure a new PDF document with custom settings
			Renderer.RenderingOptions.MarginTop = 20 ' Set top margin
			Renderer.RenderingOptions.MarginBottom = 20 ' Set bottom margin
			Renderer.RenderingOptions.Header = New SimpleHeaderFooter() With {
				.CenterText = "Sample Header",
				.DrawDividerLine = True,
				.FontSize = 12
			}
			Renderer.RenderingOptions.Footer = New SimpleHeaderFooter() With {
				.CenterText = "Page {page} of {total-pages}",
				.DrawDividerLine = True,
				.FontSize = 10
			}

			' Example: Merge PDFs
			Dim pdf1 = PdfDocument.FromFile("HtmlStringExample.pdf")
			Dim pdf2 = PdfDocument.FromFile("WebsiteExample.pdf")
			Dim mergedPdf = PdfDocument.Merge(pdf1, pdf2)

			' Save the merged PDF
			mergedPdf.SaveAs("MergedDocuments.pdf")

			Console.WriteLine("PDF files have been created, saved, and merged successfully.")
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

Unlock the full potential of PDF generation in C# with this detailed .NET 8 tutorial using IronPDF. You'll discover how to convert HTML strings, live websites, and dynamic Handlebars templates into high-quality, professional PDFs. The tutorial also shows how to customize layouts with headers, footers, margins, landscape orientation, and dynamic styling for personalized documents. Plus, you'll learn to merge multiple PDFs into a single polished file. IronPDF’s advanced rendering ensures pixel-perfect results for reports, invoices, marketing materials, and more. Get started today by downloading the free IronPDF trial and following this hands-on guide.

Further Reading: HTML to PDF in C# .NET

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.