USING IRON SUITE

C# Create PDF - A Complete Guide to Using IronPDF

Introduction

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.

How to create PDF files in C# using IronPDF library

  1. Create a new Visual Studio project
  2. Add IronPDF library from NuGet package manager
  3. Create a Simple PDF file.
  4. Modify PDF files.
  5. Adding Headers and Footers
  6. Including External Stylesheets and Scripts
  7. Including Images and bitmaps.
  8. HTML Files to PDF conversion.
  9. URL to a PDF conversion.

Introduction to IronPDF

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.

Key Features of IronPDF C# create PDF files

  1. 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.

    • Website URL to PDF: IronPDF can take a string URL of the website as input and convert it to PDF.
  2. 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.

    • Headers and footers can be added to individual pages or as consistent elements across the entire document.
  3. Support for JavaScript in PDFs

    • IronPDF enables JavaScript execution within the HTML content before PDF generation. This allows for dynamic content rendering, such as form calculations or interactivity in the generated PDFs.
  4. 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.

    • Text extraction and modification allow you to manipulate content within a PDF document programmatically.
  5. Merge and Split PDFs

    • IronPDF allows you to merge multiple PDF files into a single document or split a large PDF into smaller files, ideal for workflows where documents need to be combined or broken down into more manageable parts.
  6. Support for Interactive Forms

    • You can create, fill, and manipulate PDF forms using IronPDF. It provides full support for interactive forms (like text fields, checkboxes, and radio buttons) and allows you to pre-fill forms with data.
  7. Page Manipulation

    • IronPDF offers methods for manipulating individual pages within a PDF document, such as rotating pages, deleting pages, or reordering them.
  8. Security and Encryption

    • IronPDF allows you to apply password protection and encryption to PDFs, ensuring that your documents are secure. You can set user permissions to prevent printing, copying, or editing the PDF.
  9. Watermarking and Branding

    • Adding watermarks to PDF documents is easy with IronPDF. You can overlay text or images as watermarks onto pages, providing protection against unauthorized copying or distribution of your documents.
  10. Text and Image Extraction

    • IronPDF allows for text and image extraction from PDF documents, enabling developers to extract data for processing or reuse.
  11. Unicode and Multi-language Support

    • IronPDF has robust Unicode support, meaning it can handle international characters and fonts, making it ideal for generating PDFs in multiple languages.
  12. Optimized for Performance

    • IronPDF is optimized for performance, capable of handling large PDF documents and high volumes of requests. The library ensures fast and efficient PDF generation even with large datasets or images.
  13. API and Developer-Friendly Tools

    • IronPDF comes with a comprehensive and easy-to-use API. Developers can quickly get started by using simple method calls to perform complex tasks.
  14. Cross-Platform Support

    • IronPDF is cross-platform compatible, meaning it can be used on both Windows and Linux environments, allowing you to generate and manipulate PDFs across different operating systems.

Step 1: Create a new Visual Studio project

Now let's get started with creating a new project, open Visual Studio and create a new project as below.

C# Create PDF - A Complete Guide to Using IronPDF: Figure 1

Select create console application.

C# Create PDF - A Complete Guide to Using IronPDF: Figure 2

Provide project name and location.

C# Create PDF - A Complete Guide to Using IronPDF: Figure 3

Select .NET version

C# Create PDF - A Complete Guide to Using IronPDF: Figure 4

Create a new project.

Step 2: Add IronPDF library from NuGet package manager

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
SHELL

Also, IronPDF can be installed using Visual Studio Package Manager.

C# Create PDF - A Complete Guide to Using IronPDF: Figure 5

Step 3: Create a Simple PDF file.

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
$vbLabelText   $csharpLabel

Explanation of the Code

This program demonstrates how to use the IronPDF library to create a PDF document in C#. Here's what happens in the code:

  1. 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).

  2. 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.

  3. Saving the PDF: After creating the blank document, the program saves the PDF with the filename "simple.pdf". Since no content is added to the document, the output will be a completely blank (black) PDF.

Step 4: Modify PDF files

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
$vbLabelText   $csharpLabel

Code Explanation

  1. License Key Setup: Set the IronPDF license key to enable the library's full functionality.

  2. 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.

  3. 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.

  4. 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.

  5. Saving the Modified PDF: Save the modified PDF (with the new cover page) under the name "simple_WithTitle.pdf". The original content from "simple.pdf" is retained, but now it begins with the newly added title page.

Output PDF

C# Create PDF - A Complete Guide to Using IronPDF: Figure 6

Step 5: Adding Headers and Footers

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
$vbLabelText   $csharpLabel

Code Snippet Explanation

  1. License Key Setup: Set the IronPDF license key to enable full functionality.

  2. Create the PDF Renderer: An instance of ChromePdfRenderer is created to render HTML content into a PDF format.

  3. Define HTML Content: Create a simple HTML string including a title, heading, and paragraph.

  4. Define Header and Footer HTML:

    • The header includes page numbers formatted as "Page {page} of {total-pages}" aligned to the right.
    • The footer includes the text "Confidential" aligned to the center of each page.
  5. HTML to PDF Conversion: Convert the HTML content into a PDF document using the RenderHtmlAsPdf method.

  6. Adding Headers and Footers: Add the defined headers and footers to the PDF using the AddHtmlHeadersAndFooters method.

  7. Saving the PDF: Save the generated PDF to a file named "report.pdf" on the disk.

Output PDF

C# Create PDF - A Complete Guide to Using IronPDF: Figure 7

Step 6: Including External Stylesheets and Scripts

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
$vbLabelText   $csharpLabel

styles.css

/* styles.css */
body {
    font-family: Arial, sans-serif;
    margin: 20px;
}
h1 {
    color: #007BFF;
}
p {
    font-size: 14px;
    line-height: 1.6;
}

script.js

// 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.";
});
JAVASCRIPT

Code Explanation

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.

  1. License Key Setup: Set the IronPDF license key to enable full functionality.

  2. Define HTML Content with External Resources:

    • Use a link to an external CSS file (styles.css) to style the content.
    • Use a link to an external JavaScript file (script.js) to add dynamic functionality.
  3. Rendering HTML to PDF: Use the RenderHtmlAsPdf method to convert the HTML content (including the linked CSS and JavaScript) into a PDF document.

  4. Saving the PDF: Save the generated PDF document to a file named "awesomeIronPDF_styled_content.pdf".

Notes

  • External CSS and JS Files: Ensure the linked styles.css and script.js files are accessible in the environment where the code runs.
  • JavaScript in PDFs: IronPDF executes JavaScript to render content before the PDF is generated.

Output PDF

C# Create PDF - A Complete Guide to Using IronPDF: Figure 8

Step 7: Including Images and bitmaps.

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
$vbLabelText   $csharpLabel

Explanation of the Code

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.

  1. License Key Setup: Set the IronPDF license key to enable full functionality.

  2. Image Import and Conversion to Base64:

    • Read an image file (e.g., image.jpg) as a byte array.
    • Convert this byte array into a Base64 string, prefixed with appropriate image type data.
  3. Embedding Image in HTML: The Base64 string representing the image is embedded in an HTML <img> tag.

  4. Rendering HTML to PDF: Use IronPDF's ChromePdfRenderer to render the HTML with the embedded image into a PDF document.

  5. Saving the PDF: Save the generated PDF, which now includes the embedded image, to "embedded_sample.pdf".

Output PDF

C# Create PDF - A Complete Guide to Using IronPDF: Figure 9

Step 8: HTML Files to PDF conversion.

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
$vbLabelText   $csharpLabel

Explanation of the Code

This program demonstrates how to convert an existing HTML file into a PDF document using the IronPDF library in C#.

  1. Instantiate the Renderer: Create an instance of ChromePdfRenderer, responsible for rendering HTML content into PDF.

  2. Convert HTML to PDF: Use RenderHtmlFileAsPdf method to convert the HTML file (sample.html) into a PDF document.

  3. Save the PDF: Use the SaveAs method to save the PDF document as "output.pdf".

Step 9: URL to a PDF conversion.

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
$vbLabelText   $csharpLabel

Explanation of the Code

  1. Instantiate the Renderer: Create an instance of ChromePdfRenderer.

  2. Convert URL or Local File to PDF: Use RenderUrlAsPdf to create a PDF from a given URL or local file path.

  3. Save the PDF: Use the SaveAs method to save the resultant PDF document as "url.pdf".

Use Cases of IronPDF

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:

1. Generate PDF Reports from HTML Content

  • Use Case: Create PDF reports from dynamic or static HTML content, such as invoices, financial statements, and product catalogs.
  • Example: A business application generating weekly sales reports and sending them as PDF attachments via email.

2. Convert Web Pages to PDF

  • Use Case: Convert web pages or URLs into PDF documents for saving articles, entire websites, or specific web pages.
  • Example: A tool allowing users to convert any webpage into a PDF.

3. Embed Images, Tables, and Graphs in PDFs

  • Use Case: Embed images, graphs, and complex tables into PDF files.
  • Example: A company application generating personalized invoices with embedded company logos.

4. Convert HTML Forms to PDFs

  • Use Case: Convert HTML forms to fillable or non-fillable PDF forms.
  • Example: A form management application allowing users to fill out forms online and download or email the form as a PDF.

5. Edit and Modify Existing PDFs

  • Use Case: Add, remove, or modify elements in an existing PDF.
  • Example: A document management system watermarking PDFs to prevent unauthorized distribution.

6. Merge Multiple PDFs into One

  • Use Case: Combine several PDFs into a single document.
  • Example: A tool for legal firms to combine separate pages into one PDF for storage and retrieval.

7. Extract Text and Data from PDFs

  • Use Case: Extract text, tables, and other data from existing PDF files.
  • Example: An OCR application extracting data from scanned forms.

8. Create PDFs from Templates

  • Use Case: Use pre-defined templates to generate consistent, branded PDF documents.
  • Example: A web service generating downloadable PDF certificates.

9. Digital Signatures for PDF Documents

  • Use Case: Add digital signatures to PDFs for secure document signing.
  • Example: A solution where contracts are signed electronically.

10. Automate PDF Document Generation

  • Use Case: Automate the generation of large volumes of PDF documents.
  • Example: A web application generating monthly PDF invoices.

11. Create Interactive PDFs

  • Use Case: Create PDFs with interactive elements.
  • Example: A form submission application.

12. Convert PDFs to Other Formats

  • Use Case: Convert PDFs to other formats like HTML or Word.
  • Example: A document conversion tool.

13. Split PDF Documents

  • Use Case: Split large PDFs into smaller files.
  • Example: A scanning application splitting documents into separate PDFs.

14. Password-Protect PDFs

  • Use Case: Encrypt and password-protect PDF files.
  • Example: A financial reporting application.

License Information (Trial available)

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"
$vbLabelText   $csharpLabel

Conclusion

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.

NEXT >
HTML to PDF C# - A Complete Guide with IronPDF