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 an 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. This is ideal for generating PDFs from dynamic web content.
Support for Modern HTML/CSS: IronPDF handles modern HTML5, CSS3, and JavaScript and various PDF generation tasks, ensuring that your web-based content is rendered accurately as a PDF, preserving the layout, fonts, and interactive elements.
Custom Headers and Footers
Support for JavaScript in PDFs
Edit Existing PDFs
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
API and Developer-Friendly Tools
Now let's get started with creating a new project, open Visual Studio and create a new project as below.
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Select create console application.
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Provide project name and location.
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Select .NET version
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Create a new project.
Using using NuGet Package Manager in Visual Studio console application you can use below command to add IronPDF NuGet library.
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
Also, IronPDF can be installed using Visual Studio Package Manager
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Generate PDF documents with ease using IronPDF library. Now let us get started with a simple blank PDF file.
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
PdfDocument pdf = new PdfDocument(270, 270);
pdf.SaveAs("simple.pdf"); // Generate pdf file
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
PdfDocument pdf = new PdfDocument(270, 270);
pdf.SaveAs("simple.pdf"); // Generate pdf file
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
Dim pdf As New PdfDocument(270, 270)
pdf.SaveAs("simple.pdf") ' Generate pdf file
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.
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
var pdf = new PdfDocument("simple.pdf");
var renderer = new ChromePdfRenderer();
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
pdf.PrependPdf(pagePdf);
pdf.SaveAs("simple_WithTitle.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
var pdf = new PdfDocument("simple.pdf");
var renderer = new ChromePdfRenderer();
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
pdf.PrependPdf(pagePdf);
pdf.SaveAs("simple_WithTitle.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
Dim pdf = New PdfDocument("simple.pdf")
Dim renderer = New ChromePdfRenderer()
Dim pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>")
pdf.PrependPdf(pagePdf)
pdf.SaveAs("simple_WithTitle.pdf")
End Sub
End Class
License Key Setup:
Loading an Existing PDF:
Rendering HTML to PDF:
Prepending the Cover Page:
Saving the Modified PDF:
Add from PixabayUpload
or drag and drop an image here
Add image alt text
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your code";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></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 with headers and footers
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
pdfDocument.SaveAs("report.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your code";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></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 with headers and footers
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
pdfDocument.SaveAs("report.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your code"
' Create a new HtmlToPdf object
Dim Renderer = New ChromePdfRenderer()
Dim htmlContent As String = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></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 with headers and footers
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
pdfDocument.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlHeader= New HtmlHeaderFooter() With {.HtmlFragment=headerHtml},
.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment=footerHtml}
})
pdfDocument.SaveAs("report.pdf")
End Sub
End Class
License Key Setup:
Create the PDF Renderer:
Define HTML Content:
Define Header and Footer HTML:
Custom header and footer HTML strings are specified:
HTML to PDF Conversion:
Adding Headers and Footers:
Saving the PDF:
Add from PixabayUpload
or drag and drop an image here
Add image alt text
class Program
{
static void Main()
{
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>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
class Program
{
static void Main()
{
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>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
Friend Class Program
Shared Sub Main()
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>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>"
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
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;
}
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #007BFF;
}
p {
font-size: 14px;
line-height: 1.6;
}
' styles.css
body
If True Then
font-family: Arial, sans-serif
margin:
20px
End If
h1
If True Then
color:
#007BFF;
End If
p
If True Then
font-size: 14px
line-height: 1.6
End If
// 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.";
});
' 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. It shows how to create a PDF with styled content and dynamic behavior (via JavaScript).
License Key Setup:
Define HTML Content with External Resources:
The HTML string is defined with:
A link to an external CSS file (styles.css) to style the content.
) with an ID dynamic-text that is styled by the external CSS and potentially modified by the JavaScript.
Rendering HTML to PDF:
Saving the PDF:
Add from PixabayUpload
or drag and drop an image here
Add image alt text
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as byte
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own here
// Convert byte to base64
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
string imgHtml = $"<img src='{imgDataUri}'>";
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml); // document object
pdf.SaveAs("embedded_sample.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as byte
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own here
// Convert byte to base64
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
string imgHtml = $"<img src='{imgDataUri}'>";
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml); // document object
pdf.SaveAs("embedded_sample.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
Dim renderer As New ChromePdfRenderer()
' Import image file as byte
Dim base64Bytes() As Byte = File.ReadAllBytes("image.jpg") ' Use your own here
' Convert byte to base64
Dim imgDataUri As String = "data:image/png;base64," & Convert.ToBase64String(base64Bytes)
Dim imgHtml As String = $"<img src='{imgDataUri}'>"
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(imgHtml) ' document object
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 then embedding it in HTML content. Here’s a breakdown of the process:
License Key Setup:
Image Import and Conversion to Base64:
Embedding Image in HTML:
Rendering HTML to PDF:
Saving the PDF:
Add from PixabayUpload
or drag and drop an image here
Add image alt text
using IronPdf;
// 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;
// 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
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
' Create a PDF from an existing HTML file using C#
Private pdf = renderer.RenderHtmlFileAsPdf("sample.html")
' Export to a file or Stream
pdf.SaveAs("output.pdf")
This program demonstrates how to convert an existing HTML file into a PDF document using the IronPDF library in C#. Here's an explanation of each step:
Instantiate the Renderer:
Convert HTML to PDF:
Save the PDF:
The following code snippet demonstrates the usage of IronPDF to convert URL to PDF.
using IronPdf;
// 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;
// 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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
Instantiate the Renderer:
Convert URL or Local File to PDF:
Save the 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. Place it before using IronPDF library like so.
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key"
IronPDF .NET PDF library makes PDF generation in C# not only simple but also powerful and versatile. Whether you're generating invoices, reports, or any other kind of document, 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 without worrying about low-level document structure details.
With IronPDF , you can create high-quality PDFs effortlessly in C#, allowing you to focus more on delivering great functionality to your users rather than worrying about the complexities of document formatting. Whether you're working with dynamic web content or creating static reports, IronPDF is a reliable solution for your PDF needs.