Published November 5, 2021
How to view PDF files in ASP.NET using C# and IronPDF
Most people open PDFs on a computer using a dedicated desktop application, but software engineers can also use IronPDF to create, view, open, read and edit PDF content with C# programmatically.
IronPDF turned out to be a very useful plugin when reading PDF files in ASP.NET and C#;
You can download the project file from this link.
It is possible to create PDF documents quickly and easily using C# with IronPDF.
Much of the design and layout of PDF documents can be accomplished by using existing HTML assets or by delegating the task to web design employees; it takes care of the time-consuming task of integrating PDF generation into your application, and it automates converting prepared documents into PDFs. With.NET, you can:
- Convert web forms, local HTML pages, and other websites to PDF format.
- Allow users to download documents, share them with others via email, or save them in the cloud.
- Invoice customers and provide quotations; prepare reports; negotiate contracts and other paperwork.
- Work with ASP.NET, ASP.NET Core, web forms, MVC, Web APIs on the.NET Framework and.NET Core, and other programming languages.
Setting up IronPDF Library
There are two ways to install the library;
Installing with the NuGet Package Manager
IronPDF can be installed via the Visual Studio Add-in or the NuGet Package Manager from the command line. Navigate to the console type in the following code in Visual Studio:
PM > Install-Package IronPdf
Download the DLL File Directly From the Website
Alternatively, you can get the DLL straight from the website.
Remember to include the following remark at the top of any cs class file that makes use of IronPDF:
Check out IronPdf Features.
IronPDF is a must-have plugin. Get yours now and try it with NuGet.
Create a PDF File From an HTML String in.NET C#
Creating a PDF file from an HTML string in C# is an efficient and rewarding method of creating a new PDF file in C#.
We can easily convert any HTML (HTML5) string into a PDF document by calling the HtmlToPdf.RenderHtmlAsPdf function. IronPDF DLL has an embedded version of the Google Chromium engine, fully functional and capable of rendering C# HTML to PDF documents.
//Render HTML to pdf
var Render_htm = new IronPdf.HtmlToPdf();
using var Rendered_pdf = Render.RenderHtmAsPdf("<h1>My First HTML to Pdf</h1>");
var Output_Path = "My_First_Html.pdf";
PDF.SaveAs(Output_Path);
System.Diagnostics.Process.Start(Output_Path);
//Render HTML to pdf
var Render_htm = new IronPdf.HtmlToPdf();
using var Rendered_pdf = Render.RenderHtmAsPdf("<h1>My First HTML to Pdf</h1>");
var Output_Path = "My_First_Html.pdf";
PDF.SaveAs(Output_Path);
System.Diagnostics.Process.Start(Output_Path);
'Render HTML to pdf
Dim Render_htm = New IronPdf.HtmlToPdf()
Dim Rendered_pdf = Render.RenderHtmAsPdf("<h1>My First HTML to Pdf</h1>")
Dim Output_Path = "My_First_Html.pdf"
PDF.SaveAs(Output_Path)
System.Diagnostics.Process.Start(Output_Path)
RenderHtmlAsPdf is a powerful tool that supports CSS, JavaScript, and images in total. It may be necessary to set the second argument of RenderHtmlAsPdf if these materials are stored on a hard disc.
BaseUrlPath:
The following code will generate a PDF file:
var Render_pdf = RenderHtmlAsPdf("<img src='image_1.png'/>",@"C:Newproject");
var Render_pdf = RenderHtmlAsPdf("<img src='image_1.png'/>",@"C:Newproject");
Dim Render_pdf = RenderHtmlAsPdf("<img src='image_1.png'/>","C:Newproject")
All CSS stylesheets, pictures, and javascript files referenced will be relative to the BaseUrlPath, allowing for a more organized and logical structure to be maintained. You can, of course, make use of pictures, stylesheets, and assets available on the internet, such as Web Fonts, Google Fonts, and even jQuery, if you choose.
Create a PDF document. Making Use of an Existing HTML URL
Existing URLs can be rendered into PDFs with C# in a very efficient and straightforward manner; this also enables teams to divide PDF design and back-end PDF rendering work across various sections, which is beneficial.
Consider the following page from endeavorcreative.com, which we will render in the following example:
//Render url to pdf
var Render_htm = new IronPdf.HtmlToPdf();
using var Rendered_pdf = Render.RenderUrlAsPdf(“https://endeavorcreative.com/setting-up-wordpress-website-from-scratch/”);
var Output_Path = "Url_pdf.pdf";
Rendered_pdf.SaveAs(Output_Path);
System.Diagnostics.Process.Start(Output_Path);
//Render url to pdf
var Render_htm = new IronPdf.HtmlToPdf();
using var Rendered_pdf = Render.RenderUrlAsPdf(“https://endeavorcreative.com/setting-up-wordpress-website-from-scratch/”);
var Output_Path = "Url_pdf.pdf";
Rendered_pdf.SaveAs(Output_Path);
System.Diagnostics.Process.Start(Output_Path);
'Render url to pdf
Dim Render_htm = New IronPdf.HtmlToPdf()
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'var Rendered_pdf = Render.RenderUrlAsPdf("https://endeavorcreative.com/setting-up-wordpress-website-from-scratch/”); var Output_Path = "Url_pdf.pdf"; Rendered_pdf.SaveAs(Output_Path); System.Diagnostics.Process.Start(Output_Path);
As a result of our C# code, you will see that hyperlinks (HTML links) and even HTML forms are kept in the generated PDF.
Create a PDF Document From an Existing HTML Document
We may also render any HTML file that is stored on our hard drive. It will appear that the file has been opened using the file:/ protocol for all relative assets such as CSS, pictures, and JavaScript, among others.
//Render existing html to pdf
var Render_htm = new IronPdf.HtmlToPdf();
using var Rendered_pdf = Render.RenderHtmlFileAsPdf("Assets/test1.html");
var Output_Path = "test1_pdf.pdf";
Rendered_pdf.SaveAs(Output_Path);
System.Diagnostics.Process.Start(Output_Path);
//Render existing html to pdf
var Render_htm = new IronPdf.HtmlToPdf();
using var Rendered_pdf = Render.RenderHtmlFileAsPdf("Assets/test1.html");
var Output_Path = "test1_pdf.pdf";
Rendered_pdf.SaveAs(Output_Path);
System.Diagnostics.Process.Start(Output_Path);
'Render existing html to pdf
Dim Render_htm = New IronPdf.HtmlToPdf()
Dim Rendered_pdf = Render.RenderHtmlFileAsPdf("Assets/test1.html")
Dim Output_Path = "test1_pdf.pdf"
Rendered_pdf.SaveAs(Output_Path)
System.Diagnostics.Process.Start(Output_Path)
The advantage of using this strategy is that it allows the developer to test the HTML content in a browser while still creating it. Chrome is the web browser on which IronPDF's rendering engine is built. Hence, we urge you to convert XML to PDF; printing XML material to PDF can be accomplished through XSLT templates.
Converting ASP.NET Web Forms to a PDF File
With a single line of code, you can convert ASP.NET online forms to PDF format instead of HTML. Place the line of code in the Page Load method of the page's code-behind file to make it appear on the page.
ASP.NET Web Forms applications can either be created from scratch or opened from a previous version.
Install the NuGet package if it is not already installed.
The using keyword should be used to import the IronPdf namespace.
Make your way to the code behind the page that you'd like to convert to PDF. For instance, the file Default.aspx.cs using ASP.NET
RenderThisPageAsPdf is a method on the AspxToPdf class.
using IronPdf;
using System;
using System.Web.UI;
namespace WebApplication7
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
}
}
}
using IronPdf;
using System;
using System.Web.UI;
namespace WebApplication7
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
}
}
}
Imports IronPdf
Imports System
Imports System.Web.UI
Namespace WebApplication7
Partial Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser)
End Sub
End Class
End Namespace
Apply HTML Templating
For Intranet and website developers, the ability to template or “batch produce” PDFs is a standard necessity.
Rather than creating a template for a PDF document, we can use IronPDF to generate a template for HTML by leveraging existing, well-tested technology.
A dynamically generated PDF file is created when the HTML template is supplemented with data from a query string or a database, as shown below.
As an example, consider the C# String class and its properties. The format method works well for basic “mail-merge” operations.
String.Format("<h1>Hello {}!<h1/>","World");
String.Format("<h1>Hello {}!<h1/>","World");
String.Format("<h1>Hello {}!<h1/>","World")
Because HTML files can be pretty extensive, it is common to utilize arbitrary placeholders such as [[NAME]] and then replace them with accurate data.
The following example will generate three PDF documents, each of which will be customized for a different user.
var HtmlTemplate = "<p>[[NAME]]</p>";
var Names = new[] { "John", "James", "Jenny" };
foreach (var name in Names) {
var HtmlInstance = HtmlTemplate.Replace("[[NAME]]", name);
using var Pdf = Renderer.RenderHtmlAsPdf(HtmlInstance);
Pdf.SaveAs(name + ".pdf");
}
var HtmlTemplate = "<p>[[NAME]]</p>";
var Names = new[] { "John", "James", "Jenny" };
foreach (var name in Names) {
var HtmlInstance = HtmlTemplate.Replace("[[NAME]]", name);
using var Pdf = Renderer.RenderHtmlAsPdf(HtmlInstance);
Pdf.SaveAs(name + ".pdf");
}
Dim HtmlTemplate = "<p>[[NAME]]</p>"
Dim Names = { "John", "James", "Jenny" }
For Each name In Names
Dim HtmlInstance = HtmlTemplate.Replace("[[NAME]]", name)
Dim Pdf = Renderer.RenderHtmlAsPdf(HtmlInstance)
Pdf.SaveAs(name & ".pdf")
Next name
Asp MVC Routing Download the PDF Version Of This Page
With the ASP MVC framework, you may direct the user to a PDF file.
When building a new ASP MVC application or adding an existing MVC controller to an existing application, select this option. Start the Visual Studio new project wizard by selecting ASP.NET Web Application (.NET Framework) -> MVC from the drop-down menu. Alternatively, you can open an existing MVC project. Replace the Index method in the HomeController file in the Controllers folder, or create a new controller in the Controllers folder.
The following is an example of how the code should be written:
using IronPdf;
using System;
using System.Web.Mvc;
namespace WebApplication8.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
using var PDF = HtmlToPdf.StaticRenderUrlAsPdf(new Uri("https://en.wikipedia.org"));
return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf");
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
using IronPdf;
using System;
using System.Web.Mvc;
namespace WebApplication8.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
using var PDF = HtmlToPdf.StaticRenderUrlAsPdf(new Uri("https://en.wikipedia.org"));
return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf");
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
Imports IronPdf
Imports System
Imports System.Web.Mvc
Namespace WebApplication8.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Dim PDF = HtmlToPdf.StaticRenderUrlAsPdf(New Uri("https://en.wikipedia.org"))
Return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf")
End Function
Public Function About() As ActionResult
ViewBag.Message = "Your application description page."
Return View()
End Function
Public Function Contact() As ActionResult
ViewBag.Message = "Your contact page."
Return View()
End Function
End Class
End Namespace
Add a Cover Page to a PDF document

IronPDF simplifies the process of merging pdf documents. The most common application of this technique is to add a cover page or back page to an already-rendered PDF document that has been rendered.
To accomplish this, we first create a cover page and then use the PdfDocument.
To combine the two documents, use the merge static method.
using var PDF = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");
using var Merged = PdfDocument.Merge(new PdfDocument("CoverPage.pdf"), PDF);
Merged.SaveAs("Combined.Pdf");
using var PDF = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");
using var Merged = PdfDocument.Merge(new PdfDocument("CoverPage.pdf"), PDF);
Merged.SaveAs("Combined.Pdf");
Dim PDF = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/")
Dim Merged = PdfDocument.Merge(New PdfDocument("CoverPage.pdf"), PDF)
Merged.SaveAs("Combined.Pdf")
Add a Watermark to Your Document
Last but not least, adding a watermark to PDF documents can be accomplished using C# code; this can be used to add a disclaimer to each page of a document stating that it is "confidential" or "a sample."
// Stamps a watermark onto a new or existing PDF
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
using var pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
pdf.WatermarkAllPages("<h2 style='color:red'>SAMPLE</h2>", PdfDocument.WaterMarkLocation.MiddleCenter, 50, -45, "https://www.nuget.org/packages/IronPdf");
pdf.SaveAs(@"C:\PathToWatermarked.pdf");
// Stamps a watermark onto a new or existing PDF
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
using var pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
pdf.WatermarkAllPages("<h2 style='color:red'>SAMPLE</h2>", PdfDocument.WaterMarkLocation.MiddleCenter, 50, -45, "https://www.nuget.org/packages/IronPdf");
pdf.SaveAs(@"C:\PathToWatermarked.pdf");
' Stamps a watermark onto a new or existing PDF
Dim Renderer As New IronPdf.HtmlToPdf()
Dim pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
pdf.WatermarkAllPages("<h2 style='color:red'>SAMPLE</h2>", PdfDocument.WaterMarkLocation.MiddleCenter, 50, -45, "https://www.nuget.org/packages/IronPdf")
pdf.SaveAs("C:\PathToWatermarked.pdf")
Your PDF File Can Be Protected Using a Password
When you set the password property of a PDF document, it will be encrypted, and the user will be required to provide the correct password to read the document. This sample can be used in a.NET Core Console application.
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var htmlToPdf = new HtmlToPdf();
using var pdfDocument = htmlToPdf.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdfDocument.Password = "strong!@#pass&^%word";
pdfDocument.SaveAs("secured.pdf");
}
}
}
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var htmlToPdf = new HtmlToPdf();
using var pdfDocument = htmlToPdf.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdfDocument.Password = "strong!@#pass&^%word";
pdfDocument.SaveAs("secured.pdf");
}
}
}
Imports IronPdf
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim htmlToPdf As New HtmlToPdf()
Dim pdfDocument = htmlToPdf.RenderHtmlAsPdf("<h1>Hello World<h1>")
pdfDocument.Password = "strong!@#pass&^%word"
pdfDocument.SaveAs("secured.pdf")
End Sub
End Class
End Namespace
Without the advantages mentioned above, with the IronPDF, you can also:
- Extract images and text from PDFs using OCR
- Edit the HTML content of the web you have converted
- Enhance foreground and background images
Creating PDFs is such a challenging undertaking; some people may have never come across the fundamental notions they should employ to produce the most outstanding documents. As a result, IronPDF is extremely helpful, as it simplifies creating PDFs and, as a result, improves the original presentation of documents created from PDFs and HTML.
Based on the information provided in the documentation and competitor analysis: IronPDF is the most effective tool to use when creating PDFs, making it simple for anybody, including those who work in offices or schools, to complete their tasks efficiently.

IronPDF is a must-have .NET library. Get yours now and try it with NuGet.