.NET HELP

NuGet PDF Generator in .NET (Developer Tutorial)

Updated April 19, 2023
Share:

Introduction

PDF documents have become an essential part of the digital ecosystem, serving as a universal format for sharing and presenting information. In the world of software development, there is a constant need for tools that can efficiently create, modify, and convert PDF files. This is where NuGet PDF generators come in, offering developers a hassle-free way to generate PDF documents and integrate PDF functionalities into their applications.

One such powerful .NET PDF library is IronPDF, which is available as a NuGet package. In this article, we will dive deep into the world of NuGet, explore the features of the IronPDF library, and learn how to create and generate PDF files with ease.

What is NuGet?

NuGet is a package manager for the Microsoft development platform, including .NET Framework and .NET Core. It streamlines the process of incorporating third-party libraries and tools into your projects by automating package installation, version management, and dependency tracking.

With a vast repository of over 100,000 packages, NuGet enables developers to effortlessly add, update, and remove functionalities without having to manually manage DLLs or worry about compatibility issues. We can use it to install NuGet packages in visual studio.

IronPDF Library: A Powerful .NET PDF Generator

NuGet PDF Generator in .NET (Developer Tutorial): Figure 2 - IronPDF for .NET

IronPDF is a powerful and versatile .NET library that enables developers to create, modify, and convert PDF documents within their applications. Available as a package on NuGet, IronPDF streamlines PDF operations, making it an invaluable tool for developers working with .NET Framework and .NET Core projects.

IronPDF's key features include creating new PDF documents from scratch, converting HTML content to PDF files, modifying existing PDF files, and providing high-quality rendering and typography. The library simplifies the process of generating PDFs from existing HTML pages. It can create a PDF file using an HTML string, HTML file, or URL.

NuGet PDF Generator in .NET (Developer Tutorial): Figure 3 - Cross Platform Support

HTML to PDF Conversion

IronPDF's has the the ability to convert HTML content to PDF documents. This allows developers to create PDF files from existing HTML pages or dynamically generate PDFs from user-generated content. Here's an example of how to convert an HTML file to a PDF document:


    using IronPdf;

    // Instantiate Renderer
    var renderer = new ChromePdfRenderer();

    // Create a PDF from a HTML string using C#
    var pdf = renderer.RenderHtmlAsPdf("Hello World");

    // Export to a file or Stream
    pdf.SaveAs("output.pdf");

    using IronPdf;

    // Instantiate Renderer
    var renderer = new ChromePdfRenderer();

    // Create a PDF from a HTML string using C#
    var pdf = renderer.RenderHtmlAsPdf("Hello World");

    // Export to a file or Stream
    pdf.SaveAs("output.pdf");
Imports IronPdf

	' Instantiate Renderer
	Private renderer = New ChromePdfRenderer()

	' Create a PDF from a HTML string using C#
	Private pdf = renderer.RenderHtmlAsPdf("Hello World")

	' Export to a file or Stream
	pdf.SaveAs("output.pdf")
VB   C#

NuGet PDF Generator in .NET (Developer Tutorial): Figure 4 - Output PDF

URL to PDF document

IronPDF allows developers to convert web pages directly into PDF documents. This feature is particularly useful for scenarios where developers need to generate PDF documents from live web pages or save online content for offline viewing, archiving, or sharing.

By leveraging IronPDF's HTML-to-PDF conversion capabilities, developers can easily convert an entire web page, including its text, images, and styles, into a PDF document with just a few lines of code. Here's a quick overview of how this feature works:


    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");
Imports IronPdf

	' Instantiate Renderer
	Private renderer = New ChromePdfRenderer()

	' Create a PDF from a URL or local file path
	Private pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")

	' Export to a file or Stream
	pdf.SaveAs("url.pdf")
VB   C#

Modify PDF Files

IronPDF also enables developers to modify existing PDF documents and files, making it easy to add or remove content, merge documents, and apply security settings. Here's an example of how to add a watermark to a PDF document:


    using IronPdf;

    // Stamps a Watermark onto a new or existing PDF
    var renderer = new ChromePdfRenderer();

    var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
    pdf.ApplyWatermark("SAMPLE", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
    pdf.SaveAs(@"C:\Path\To\Watermarked.pdf");

    using IronPdf;

    // Stamps a Watermark onto a new or existing PDF
    var renderer = new ChromePdfRenderer();

    var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
    pdf.ApplyWatermark("SAMPLE", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
    pdf.SaveAs(@"C:\Path\To\Watermarked.pdf");
Imports IronPdf

	' Stamps a Watermark onto a new or existing PDF
	Private renderer = New ChromePdfRenderer()

	Private pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
	pdf.ApplyWatermark("SAMPLE", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center)
	pdf.SaveAs("C:\Path\To\Watermarked.pdf")
VB   C#

NuGet PDF Generator in .NET (Developer Tutorial): Figure 5 - Watermark applied to a PDF document

This example project demonstrates how to create a PDF document from URL, add a watermark to each page, and save the modified document as a new file.

Advanced PDF Functionality with IronPDF

Beyond the basic PDF file creation operations discussed earlier, IronPDF offers advanced features that can further enhance your PDF document generation and management capabilities. Some of these features include:

Merging PDF Documents: IronPDF enables you to merge multiple PDF files into a single document, making it easy to compile and organize related content.


    using IronPdf;

    var html_a = @" [PDF_A] 
             [PDF_A] 1st Page 

             [PDF_A] 2nd Page";

    var html_b = @" [PDF_B] 
             [PDF_B] 1st Page 

             [PDF_B] 2nd Page";

    var renderer = new ChromePdfRenderer();

    var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
    var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
    var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);

    merged.SaveAs("Merged.pdf");

    using IronPdf;

    var html_a = @" [PDF_A] 
             [PDF_A] 1st Page 

             [PDF_A] 2nd Page";

    var html_b = @" [PDF_B] 
             [PDF_B] 1st Page 

             [PDF_B] 2nd Page";

    var renderer = new ChromePdfRenderer();

    var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
    var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
    var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);

    merged.SaveAs("Merged.pdf");
Imports IronPdf

	Private html_a = " [PDF_A] 
             [PDF_A] 1st Page 

             [PDF_A] 2nd Page"

	Private html_b = " [PDF_B] 
             [PDF_B] 1st Page 

             [PDF_B] 2nd Page"

	Private renderer = New ChromePdfRenderer()

	Private pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
	Private pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)
	Private merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b)

	merged.SaveAs("Merged.pdf")
VB   C#

PDF Forms: IronPDF supports the creation and manipulation of PDF forms, allowing developers to generate dynamic, interactive PDF documents with fillable fields.

using IronPdf;
using System;

// Step 1.  Creating a PDF with editable forms from HTML using form and input tags
// Radio Button and Checkbox can also be implemented with input type 'radio' and 'checkbox'
const string formHtml = @"

            Editable PDF  Form

                First name:   
                Last name:   

                Please specify your gender:

                Female 

                Male 

                Non-Binary / Other

                Please select all medical conditions that apply:

                Hypertension

                Heart Disease

                Stoke

                Diabetes

                Kidney Disease
    ";

// Instantiate Renderer
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
renderer.RenderHtmlAsPdf(formHtml).SaveAs("BasicForm.pdf");

// Step 2. Reading and Writing PDF form values.
var FormDocument = PdfDocument.FromFile("BasicForm.pdf");

// Set and Read the value of the "firstname" field
var FirstNameField = FormDocument.Form.GetFieldByName("firstname");
FirstNameField.Value = "Minnie";
Console.WriteLine("FirstNameField value: {0}", FirstNameField.Value);

// Set and Read the value of the "lastname" field
IronPdf.Forms.FormField LastNameField = FormDocument.Form.GetFieldByName("lastname");
LastNameField.Value = "Mouse";
Console.WriteLine("LastNameField value: {0}", LastNameField.Value);

FormDocument.SaveAs("FilledForm.pdf");
using IronPdf;
using System;

// Step 1.  Creating a PDF with editable forms from HTML using form and input tags
// Radio Button and Checkbox can also be implemented with input type 'radio' and 'checkbox'
const string formHtml = @"

            Editable PDF  Form

                First name:   
                Last name:   

                Please specify your gender:

                Female 

                Male 

                Non-Binary / Other

                Please select all medical conditions that apply:

                Hypertension

                Heart Disease

                Stoke

                Diabetes

                Kidney Disease
    ";

// Instantiate Renderer
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
renderer.RenderHtmlAsPdf(formHtml).SaveAs("BasicForm.pdf");

// Step 2. Reading and Writing PDF form values.
var FormDocument = PdfDocument.FromFile("BasicForm.pdf");

// Set and Read the value of the "firstname" field
var FirstNameField = FormDocument.Form.GetFieldByName("firstname");
FirstNameField.Value = "Minnie";
Console.WriteLine("FirstNameField value: {0}", FirstNameField.Value);

// Set and Read the value of the "lastname" field
IronPdf.Forms.FormField LastNameField = FormDocument.Form.GetFieldByName("lastname");
LastNameField.Value = "Mouse";
Console.WriteLine("LastNameField value: {0}", LastNameField.Value);

FormDocument.SaveAs("FilledForm.pdf");
Imports IronPdf
Imports System

' Step 1.  Creating a PDF with editable forms from HTML using form and input tags
' Radio Button and Checkbox can also be implemented with input type 'radio' and 'checkbox'
Private Const formHtml As String = "

            Editable PDF  Form

                First name:   
                Last name:   

                Please specify your gender:

                Female 

                Male 

                Non-Binary / Other

                Please select all medical conditions that apply:

                Hypertension

                Heart Disease

                Stoke

                Diabetes

                Kidney Disease
    "

' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True
renderer.RenderHtmlAsPdf(formHtml).SaveAs("BasicForm.pdf")

' Step 2. Reading and Writing PDF form values.
Dim FormDocument = PdfDocument.FromFile("BasicForm.pdf")

' Set and Read the value of the "firstname" field
Dim FirstNameField = FormDocument.Form.GetFieldByName("firstname")
FirstNameField.Value = "Minnie"
Console.WriteLine("FirstNameField value: {0}", FirstNameField.Value)

' Set and Read the value of the "lastname" field
Dim LastNameField As IronPdf.Forms.FormField = FormDocument.Form.GetFieldByName("lastname")
LastNameField.Value = "Mouse"
Console.WriteLine("LastNameField value: {0}", LastNameField.Value)

FormDocument.SaveAs("FilledForm.pdf")
VB   C#

PDF Security: IronPDF provides options for securing your PDF documents, including password protection and encryption.

using IronPdf;

// Open an Encrypted File, alternatively create a new PDF from Html
var pdf = PdfDocument.FromFile("encrypted.pdf");

//Edit file security settings

//The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption();
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserAnnotations = false;

//Change or set the document encryption password
pdf.SecuritySettings.OwnerPassword = "top-secret"; // password to edit the pdf
pdf.SaveAs("secured.pdf");
using IronPdf;

// Open an Encrypted File, alternatively create a new PDF from Html
var pdf = PdfDocument.FromFile("encrypted.pdf");

//Edit file security settings

//The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption();
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserAnnotations = false;

//Change or set the document encryption password
pdf.SecuritySettings.OwnerPassword = "top-secret"; // password to edit the pdf
pdf.SaveAs("secured.pdf");
Imports IronPdf

' Open an Encrypted File, alternatively create a new PDF from Html
Private pdf = PdfDocument.FromFile("encrypted.pdf")

'Edit file security settings

'The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption()
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")
pdf.SecuritySettings.AllowUserAnnotations = False

'Change or set the document encryption password
pdf.SecuritySettings.OwnerPassword = "top-secret" ' password to edit the pdf
pdf.SaveAs("secured.pdf")
VB   C#

IronPDF's advanced features make it an invaluable tool for developers who require comprehensive PDF functionality in their .NET applications. From merging documents and managing forms to securing content and custom rendering, IronPDF offers a robust solution for all your PDF needs.

Conclusion

The IronPDF library, available on NuGet, is a powerful and versatile .NET PDF library that simplifies the process of creating, modifying, and converting PDF files in your applications. By leveraging its extensive features, such as generating new PDF documents, converting HTML to PDF, and modifying existing PDF files, developers can seamlessly integrate PDF functionality into their .NET Framework and .NET Core projects. With IronPDF, generating and managing PDF documents has never been easier.

IronPDF offers a free trial for their library, allowing developers to try out the features and functionalities of the software before making a purchase decision. If a developer decides to purchase a license after the trial period, the starting price for the license is $749. The price may vary depending on the type of license and the number of developers using the software. IronPDF also offers volume discounts for larger purchases.

< PREVIOUS
NuGet HTML to PDF (Developer Tutorial)
NEXT >
What is .NET Framework 4.7.2 (Recommended Libraries)

Ready to get started? Version: 2024.7 just released

Free NuGet Download Total downloads: 9,848,088 View Licenses >
123