NuGet PDF Generator in .NET (Developer Tutorial)
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
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.
HTML to PDF Conversion
IronPDF has the ability to convert HTML content into 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 string to a PDF document:
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from an 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 an 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 an HTML string using C#
Private pdf = renderer.RenderHtmlAsPdf("Hello World")
' Export to a file or Stream
pdf.SaveAs("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")
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")
This example project demonstrates how to create a PDF document from a 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 htmlA = @"<h1>[PDF_A]</h1><p>[PDF_A] 1st Page</p><p>[PDF_A] 2nd Page</p>";
var htmlB = @"<h1>[PDF_B]</h1><p>[PDF_B] 1st Page</p><p>[PDF_B] 2nd Page</p>";
var renderer = new ChromePdfRenderer();
var pdfDocA = renderer.RenderHtmlAsPdf(htmlA);
var pdfDocB = renderer.RenderHtmlAsPdf(htmlB);
var merged = PdfDocument.Merge(pdfDocA, pdfDocB);
merged.SaveAs("Merged.pdf");
using IronPdf;
var htmlA = @"<h1>[PDF_A]</h1><p>[PDF_A] 1st Page</p><p>[PDF_A] 2nd Page</p>";
var htmlB = @"<h1>[PDF_B]</h1><p>[PDF_B] 1st Page</p><p>[PDF_B] 2nd Page</p>";
var renderer = new ChromePdfRenderer();
var pdfDocA = renderer.RenderHtmlAsPdf(htmlA);
var pdfDocB = renderer.RenderHtmlAsPdf(htmlB);
var merged = PdfDocument.Merge(pdfDocA, pdfDocB);
merged.SaveAs("Merged.pdf");
Imports IronPdf
Private htmlA = "<h1>[PDF_A]</h1><p>[PDF_A] 1st Page</p><p>[PDF_A] 2nd Page</p>"
Private htmlB = "<h1>[PDF_B]</h1><p>[PDF_B] 1st Page</p><p>[PDF_B] 2nd Page</p>"
Private renderer = New ChromePdfRenderer()
Private pdfDocA = renderer.RenderHtmlAsPdf(htmlA)
Private pdfDocB = renderer.RenderHtmlAsPdf(htmlB)
Private merged = PdfDocument.Merge(pdfDocA, pdfDocB)
merged.SaveAs("Merged.pdf")
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 = @"
<h1>Editable PDF Form</h1>
<label>First name: <input type='text' name='firstname' /></label>
<label>Last name: <input type='text' name='lastname' /></label>
<p>Please specify your gender:</p>
<label><input type='radio' name='gender' value='female' /> Female</label>
<label><input type='radio' name='gender' value='male' /> Male</label>
<label><input type='radio' name='gender' value='other' /> Non-Binary / Other</label>
<p>Please select all medical conditions that apply:</p>
<label><input type='checkbox' name='condition' value='hypertension' /> Hypertension</label>
<label><input type='checkbox' name='condition' value='heartDisease' /> Heart Disease</label>
<label><input type='checkbox' name='condition' value='stroke' /> Stroke</label>
<label><input type='checkbox' name='condition' value='diabetes' /> Diabetes</label>
<label><input type='checkbox' name='condition' value='kidneyDisease' /> Kidney Disease</label>
";
// 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.FindFormField("firstname");
firstNameField.Value = "Minnie";
Console.WriteLine("FirstNameField value: {0}", firstNameField.Value);
// Set and Read the value of the "lastname" field
var lastNameField = formDocument.Form.FindFormField("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 = @"
<h1>Editable PDF Form</h1>
<label>First name: <input type='text' name='firstname' /></label>
<label>Last name: <input type='text' name='lastname' /></label>
<p>Please specify your gender:</p>
<label><input type='radio' name='gender' value='female' /> Female</label>
<label><input type='radio' name='gender' value='male' /> Male</label>
<label><input type='radio' name='gender' value='other' /> Non-Binary / Other</label>
<p>Please select all medical conditions that apply:</p>
<label><input type='checkbox' name='condition' value='hypertension' /> Hypertension</label>
<label><input type='checkbox' name='condition' value='heartDisease' /> Heart Disease</label>
<label><input type='checkbox' name='condition' value='stroke' /> Stroke</label>
<label><input type='checkbox' name='condition' value='diabetes' /> Diabetes</label>
<label><input type='checkbox' name='condition' value='kidneyDisease' /> Kidney Disease</label>
";
// 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.FindFormField("firstname");
firstNameField.Value = "Minnie";
Console.WriteLine("FirstNameField value: {0}", firstNameField.Value);
// Set and Read the value of the "lastname" field
var lastNameField = formDocument.Form.FindFormField("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 = "
<h1>Editable PDF Form</h1>
<label>First name: <input type='text' name='firstname' /></label>
<label>Last name: <input type='text' name='lastname' /></label>
<p>Please specify your gender:</p>
<label><input type='radio' name='gender' value='female' /> Female</label>
<label><input type='radio' name='gender' value='male' /> Male</label>
<label><input type='radio' name='gender' value='other' /> Non-Binary / Other</label>
<p>Please select all medical conditions that apply:</p>
<label><input type='checkbox' name='condition' value='hypertension' /> Hypertension</label>
<label><input type='checkbox' name='condition' value='heartDisease' /> Heart Disease</label>
<label><input type='checkbox' name='condition' value='stroke' /> Stroke</label>
<label><input type='checkbox' name='condition' value='diabetes' /> Diabetes</label>
<label><input type='checkbox' name='condition' value='kidneyDisease' /> Kidney Disease</label>
"
' 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.FindFormField("firstname")
firstNameField.Value = "Minnie"
Console.WriteLine("FirstNameField value: {0}", firstNameField.Value)
' Set and Read the value of the "lastname" field
Dim lastNameField = formDocument.Form.FindFormField("lastname")
lastNameField.Value = "Mouse"
Console.WriteLine("LastNameField value: {0}", lastNameField.Value)
formDocument.SaveAs("FilledForm.pdf")
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")
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 of 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.
Frequently Asked Questions
What is a NuGet PDF generator?
A NuGet PDF generator is a tool that allows developers to generate and manipulate PDF documents within their applications using packages available in the NuGet package manager, such as IronPDF.
What is IronPDF?
IronPDF is a robust .NET library available on NuGet, which enables developers to create, modify, and convert PDF documents. It supports operations like converting HTML to PDF, modifying PDF files, and adding security features to PDFs.
How can I convert HTML content to a PDF?
To convert HTML content to a PDF using IronPDF, you can use the ChromePdfRenderer class to render HTML strings, files, or URLs into PDF documents with just a few lines of C# code.
Can you merge multiple PDF documents?
Yes, using IronPDF, you can merge multiple PDF documents into a single file, allowing developers to compile and organize related content efficiently.
What advanced PDF functionalities are available?
IronPDF offers advanced functionalities like merging PDF documents, managing PDF forms, and applying security settings such as password protection and encryption.
Is there a free trial available?
Yes, IronPDF offers a free trial of their library, allowing developers to explore its features before making a purchase decision.
How can I apply a watermark to a PDF?
To apply a watermark to a PDF using IronPDF, you can use the ApplyWatermark method to stamp text onto each page of an existing PDF document.
What is the starting price for a license?
The starting price for an IronPDF license is referred to as '$liteLicense'. The exact price may vary depending on the license type and the number of developers using the software.
Can you convert web pages to PDF documents?
Yes, using IronPDF, you can convert web pages directly into PDF documents, which is useful for saving online content for offline viewing, archiving, or sharing.
What is NuGet?
NuGet is a package manager for the Microsoft development platform, which automates the process of incorporating third-party libraries and tools into projects by handling package installation, version management, and dependency tracking.