10 .NET API products for your office documents
Total Suite Value:
$7,192 USD
Creating PDF documents from HTML content has become an essential requirement for various applications, ranging from generating invoices to archiving web content. IronPDF is a powerful and versatile .NET library that simplifies the process of converting HTML to PDF, making it effortless for developers to create high-quality PDF documents in C#. In this article, we will explore the features of IronPDF and provide a step-by-step guide to using it for PDF creation.
IronPDF is a .NET library that allows developers to convert HTML to PDF with ease. It supports a wide range of features, including CSS, JavaScript, and even embedded images. With IronPDF, you can create PDFs that look exactly like your HTML web pages, ensuring a seamless transition between formats. This library is particularly useful for web applications that need to generate dynamic PDF documents on the fly.
IronPDF allows developers to seamlessly integrate PDF functionality into .NET applications without needing to manually manage PDF file structures. IronPDF leverages the Chrome-based rendering engine to convert HTML pages (including complex CSS, JavaScript, and images) into well-structured PDF documents. It can be used for generating reports, invoices, eBooks, or any type of document that needs to be presented in PDF format.
IronPDF is versatile, offering functionality that not only renders PDFs but also provides a wide range of PDF manipulation options like editing, form handling, encryption, and more.
HTML to PDF Conversion
HTML Rendering: IronPDF can convert HTML documents or web pages (including HTML with CSS, images, and JavaScript) directly into a PDF document with just a few lines, making it ideal for generating PDFs from dynamic web content.
Support for Modern HTML/CSS: IronPDF handles modern HTML5, CSS3, and JavaScript, ensuring that your web-based content is rendered accurately as a PDF, preserving the layout, fonts, and interactive elements.
Advanced Rendering: It uses Chrome’s rendering engine (via Chromium) for accurate, high-quality PDF generation, making it more reliable than many other HTML-to-PDF libraries.
Custom Headers and Footers
IronPDF allows developers to add custom headers and footers to PDF documents, which can include dynamic content such as page numbers, document title, or custom text.
Support for JavaScript in PDFs
Edit Existing PDFs
IronPDF provides the ability to edit existing PDFs. You can modify text, images, and add annotations to existing PDF files. This feature is useful for watermarking documents, adding signatures, or updating content within PDF files.
Merge and Split PDFs
Support for Interactive Forms
Page Manipulation
Security and Encryption
Watermarking and Branding
Text and Image Extraction
Unicode and Multi-language Support
Optimized for Performance
API and Developer-Friendly Tools
Cross-Platform Support
Now let's get started with creating a new project, open Visual Studio and create a new project as below.
Select create console application.
Provide project name and location.
Select .NET version
Create a new project.
Using the NuGet Package Manager in Visual Studio console application you can use the below command to add the IronPDF NuGet library.
Install-Package IronPdf
Install-Package IronPdf
Also, IronPDF can be installed using Visual Studio Package Manager.
Generate PDF documents with ease using the IronPDF library. Now let's get started with a simple blank PDF file.
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new PDF document with specific dimensions (270x270 points)
PdfDocument pdf = new PdfDocument(270, 270);
// Save the blank PDF document to disk
pdf.SaveAs("simple.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new PDF document with specific dimensions (270x270 points)
PdfDocument pdf = new PdfDocument(270, 270);
// Save the blank PDF document to disk
pdf.SaveAs("simple.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Create a new PDF document with specific dimensions (270x270 points)
Dim pdf As New PdfDocument(270, 270)
' Save the blank PDF document to disk
pdf.SaveAs("simple.pdf")
End Sub
End Class
This program demonstrates how to use the IronPDF library to create a PDF document in C#. Here's what happens in the code:
License Key Setup: The program first sets the license key for the IronPDF library. This is necessary to use the full features of the library, as the license key ensures you have access to the full functionality (rather than being limited to a trial version).
Creating a PDF Document: The program then creates a new PDF document with a size of 270x270 points. A point is a unit of measurement in printing and is equivalent to 1/72 of an inch. Therefore, this would create a square document of roughly 3.75 inches by 3.75 inches.
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Load the existing PDF document
var pdf = new PdfDocument("simple.pdf");
// Create a renderer for converting HTML to PDF
var renderer = new ChromePdfRenderer();
// Render HTML content as a PDF
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
// Prepend the rendered page as the first page of the existing PDF
pdf.PrependPdf(pagePdf);
// Save the modified PDF with a new filename
pdf.SaveAs("simple_WithTitle.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Load the existing PDF document
var pdf = new PdfDocument("simple.pdf");
// Create a renderer for converting HTML to PDF
var renderer = new ChromePdfRenderer();
// Render HTML content as a PDF
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
// Prepend the rendered page as the first page of the existing PDF
pdf.PrependPdf(pagePdf);
// Save the modified PDF with a new filename
pdf.SaveAs("simple_WithTitle.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Load the existing PDF document
Dim pdf = New PdfDocument("simple.pdf")
' Create a renderer for converting HTML to PDF
Dim renderer = New ChromePdfRenderer()
' Render HTML content as a PDF
Dim pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>")
' Prepend the rendered page as the first page of the existing PDF
pdf.PrependPdf(pagePdf)
' Save the modified PDF with a new filename
pdf.SaveAs("simple_WithTitle.pdf")
End Sub
End Class
License Key Setup: Set the IronPDF license key to enable the library's full functionality.
Loading an Existing PDF: Load an existing PDF file named "simple.pdf" into a new PdfDocument object. This is the PDF that will have a new cover page prepended to it.
Rendering HTML to PDF: Use a ChromePdfRenderer object to render HTML content into a PDF. In this example, the HTML content is a simple <h1>
tag with the text "Awesome IronPDF Library", which is converted into a cover-page PDF using the RenderHtmlAsPdf
method.
Prepending the Cover Page: Use the PrependPdf
method to insert the cover page PDF (generated from the HTML) at the beginning of the existing PDF document.
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new HtmlToPdf renderer
var renderer = new ChromePdfRenderer();
// Define the HTML content
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>";
// Define headers and footers as HTML
string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
string footerHtml = "<div style='text-align: center;'>Confidential</div>";
// Convert the HTML content to a PDF document
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader = new HtmlHeaderFooter() { HtmlFragment = headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment = footerHtml }
});
// Save the PDF document with headers and footers
pdfDocument.SaveAs("report.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new HtmlToPdf renderer
var renderer = new ChromePdfRenderer();
// Define the HTML content
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>";
// Define headers and footers as HTML
string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
string footerHtml = "<div style='text-align: center;'>Confidential</div>";
// Convert the HTML content to a PDF document
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader = new HtmlHeaderFooter() { HtmlFragment = headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment = footerHtml }
});
// Save the PDF document with headers and footers
pdfDocument.SaveAs("report.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Create a new HtmlToPdf renderer
Dim renderer = New ChromePdfRenderer()
' Define the HTML content
Dim htmlContent As String = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>"
' Define headers and footers as HTML
Dim headerHtml As String = "<div style='text-align: right;'>Page {page} of {total-pages}</div>"
Dim footerHtml As String = "<div style='text-align: center;'>Confidential</div>"
' Convert the HTML content to a PDF document
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlHeader = New HtmlHeaderFooter() With {.HtmlFragment = headerHtml},
.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment = footerHtml}
})
' Save the PDF document with headers and footers
pdfDocument.SaveAs("report.pdf")
End Sub
End Class
License Key Setup: Set the IronPDF license key to enable full functionality.
Create the PDF Renderer: An instance of ChromePdfRenderer is created to render HTML content into a PDF format.
Define HTML Content: Create a simple HTML string including a title, heading, and paragraph.
Define Header and Footer HTML:
HTML to PDF Conversion: Convert the HTML content into a PDF document using the RenderHtmlAsPdf method.
Adding Headers and Footers: Add the defined headers and footers to the PDF using the AddHtmlHeadersAndFooters method.
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Define the HTML content with links to external CSS and JS files
string htmlContent = @"
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h2>Styled Content</h2>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
// Create a PDF renderer instance
var renderer = new ChromePdfRenderer();
// Render HTML content to a PDF
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to disk
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Define the HTML content with links to external CSS and JS files
string htmlContent = @"
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h2>Styled Content</h2>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
// Create a PDF renderer instance
var renderer = new ChromePdfRenderer();
// Render HTML content to a PDF
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to disk
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Define the HTML content with links to external CSS and JS files
Dim htmlContent As String = "
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h2>Styled Content</h2>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>"
' Create a PDF renderer instance
Dim renderer = New ChromePdfRenderer()
' Render HTML content to a PDF
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF to disk
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf")
End Sub
End Class
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #007BFF;
}
p {
font-size: 14px;
line-height: 1.6;
}
// script.js
document.addEventListener('DOMContentLoaded', function() {
var dynamicText = document.getElementById('dynamic-text');
dynamicText.textContent = "This content has been modified by JavaScript.";
});
// script.js
document.addEventListener('DOMContentLoaded', function() {
var dynamicText = document.getElementById('dynamic-text');
dynamicText.textContent = "This content has been modified by JavaScript.";
});
This code demonstrates how to use IronPDF in C# to generate a PDF from HTML content that includes links to external CSS and JavaScript files.
License Key Setup: Set the IronPDF license key to enable full functionality.
Define HTML Content with External Resources:
Rendering HTML to PDF: Use the RenderHtmlAsPdf method to convert the HTML content (including the linked CSS and JavaScript) into a PDF document.
using IronPdf;
using System;
using System.IO;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a PDF renderer instance
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as bytes
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own image file here
// Convert image bytes to Base64 string
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
// Create HTML content with the embedded Base64 image
string imgHtml = $"<img src='{imgDataUri}'>";
// Render HTML content to a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml);
// Save the PDF with the embedded image
pdf.SaveAs("embedded_sample.pdf");
}
}
using IronPdf;
using System;
using System.IO;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a PDF renderer instance
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as bytes
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own image file here
// Convert image bytes to Base64 string
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
// Create HTML content with the embedded Base64 image
string imgHtml = $"<img src='{imgDataUri}'>";
// Render HTML content to a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml);
// Save the PDF with the embedded image
pdf.SaveAs("embedded_sample.pdf");
}
}
Imports IronPdf
Imports System
Imports System.IO
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Create a PDF renderer instance
Dim renderer As New ChromePdfRenderer()
' Import image file as bytes
Dim base64Bytes() As Byte = File.ReadAllBytes("image.jpg") ' Use your own image file here
' Convert image bytes to Base64 string
Dim imgDataUri As String = "data:image/png;base64," & Convert.ToBase64String(base64Bytes)
' Create HTML content with the embedded Base64 image
Dim imgHtml As String = $"<img src='{imgDataUri}'>"
' Render HTML content to a PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(imgHtml)
' Save the PDF with the embedded image
pdf.SaveAs("embedded_sample.pdf")
End Sub
End Class
This C# program demonstrates how to use IronPDF to embed an image into a PDF document by converting the image to a Base64 string and embedding it in HTML content.
License Key Setup: Set the IronPDF license key to enable full functionality.
Image Import and Conversion to Base64:
Embedding Image in HTML: The Base64 string representing the image is embedded in an HTML <img>
tag.
Rendering HTML to PDF: Use IronPDF's ChromePdfRenderer to render the HTML with the embedded image into a PDF document.
using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from an existing HTML file using C#
var pdf = renderer.RenderHtmlFileAsPdf("sample.html");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from an existing HTML file using C#
var pdf = renderer.RenderHtmlFileAsPdf("sample.html");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Instantiate Renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from an existing HTML file using C#
Dim pdf = renderer.RenderHtmlFileAsPdf("sample.html")
' Export to a file or Stream
pdf.SaveAs("output.pdf")
End Sub
End Class
This program demonstrates how to convert an existing HTML file into a PDF document using the IronPDF library in C#.
Instantiate the Renderer: Create an instance of ChromePdfRenderer, responsible for rendering HTML content into PDF.
Convert HTML to PDF: Use RenderHtmlFileAsPdf
method to convert the HTML file (sample.html) into a PDF document.
SaveAs
method to save the PDF document as "output.pdf".The following code snippet demonstrates the usage of IronPDF to convert URL to PDF.
using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a URL or local file path
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Export to a file or Stream
pdf.SaveAs("url.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a URL or local file path
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Export to a file or Stream
pdf.SaveAs("url.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Instantiate Renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from a URL or local file path
Dim pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Export to a file or Stream
pdf.SaveAs("url.pdf")
End Sub
End Class
Instantiate the Renderer: Create an instance of ChromePdfRenderer
.
Convert URL or Local File to PDF: Use RenderUrlAsPdf
to create a PDF from a given URL or local file path.
SaveAs
method to save the resultant PDF document as "url.pdf".IronPDF is a powerful library for working with PDFs in C#. It allows developers to generate, modify, and manipulate PDF documents easily. Below are some common use cases of IronPDF in C# applications:
IronPDF provides a free trial. Place it before using the library like so:
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key"
IronPDF, the .NET PDF library, makes PDF generation in C# simple and powerful. Whether you're generating invoices, reports, or other types of documents, IronPDF offers robust features like HTML-to-PDF conversion, custom headers and footers, PDF editing, form handling, and more. It provides a seamless way to work with PDFs.
With IronPDF, you can create high-quality PDFs effortlessly in C#, allowing you to focus on delivering great functionality to your users instead of worrying about the complexities of document formatting. Whether working with dynamic web content or creating static reports, IronPDF is a reliable solution for your PDF needs.