Learn how to turn any ASP.Net ASPX page into a PDF document into a PDF instead of HTML using a single line of code in C# or VB.Net

C# PDF ASP.NET ASPX

April 6th 2017 by Jacob Müller

How to Convert ASPX to PDF

Tutorial shows conversion of ASPX Files to PDFs in ASP.Net Web Applications.

Converting ASPX to PDF files

Follow these guiding steps:

  1. Install ASPX to PDF converter such as IronPDF into Visual Studio
  2. Start by converting an ASPX web form. See example Invoice.aspx
  3. Configure ASPX to PDF Settings for your example
  4. Add optional Headers and Footers to PDF document
  5. Trigger Page Breaks and use Async + Multithreading
  6. Download your completed PDF Document

How to Convert ASPX to PDF for ASP.Net

Microsoft Web Form Applications for ASP.Net are commonly used in the development of sophisticated websites, online banking, intranets and accounting systems. One common feature of ASP.Net (ASPX) websites is to generate dynamic PDF files such as invoices, tickets or management reports for users to download.

This tutorial shows how to use the IronPDF software component for .NET to turn any ASP.Net web form into a PDF. HTML normally rendered as a web page, will be used to render as a PDF for download or viewed in a web browser.

We achieve this HTML to PDF conversion (ASPX to PDF) when rendering webpages using the IronPDF library and its AspxToPdf Class.

Step 1: Getting Set Up

In Visual Studio, we need to add the IronPdf .NET PDF class library to our website application.

Right click on the project in solution explorer and select "Manage Nuget Packages" from the menu. From there we can browse (search) for "IronPDF" and install the latest version of the IronPDF .Net PDF library,

This will work in any .Net Framework Website project using Framework 4 and above. VB.Net and C# projects are equally supported.

PM > Install-Package IronPdf

https://www.nuget.org/packages/IronPdf

The IronPdf.dll may alternatively be downloaded and manually installed to the project "bin" folder or the machine GAC from https://ironpdf.com/packages/IronPdf.zip

With this library, coding a project to Convert ASPX to PDF will take a few minutes of .Net coding.**

Step 2: Start Converting ASP.Net Pages to PDF

We start with a normal ASPX "Web Form" which renders as HTML. We later convert the ASPX page to PDF file format.

In the attached example source-code we rendered a business invoice: "Invoice.aspx" which is a simple HTML business invoice rendered in ASP.NET.

The html page contains CSS3 stylesheets and may also include images and javascript.

To render this Web Page as a PDF instead of HTML we need to open the C# (or VB.Net) code and add this to the Page_Load event:

 IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser)
VB   C#

This is all that's required; the html now renders as a PDF. Hyperlinks, StyleSheets, Images and even HTML forms are preserved. This is very similar to the output if the user printed the HTML to a PDF in their browser themselves. IronPDF is built upon Chromium web browser technology that powers Google Chrome.

The entire C# code reads like this in full:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IronPdf;

namespace AspxToPdfTutorial
{
    public partial class Invoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports IronPdf

Namespace AspxToPdfTutorial
    
    Public Class Invoice
        Inherits System.Web.UI.Page
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser)
        End Sub
    End Class
End Namespace
VB   C#

Step 3: ASPX to PDF Converter Settings

There are many options to tweak and perfect when we convert ASPX files to PDF generated using .Net Web Forms.

These options are documented in full online at https://ironpdf.com/c%23-pdf-documentation/html/M_IronPdf_AspxToPdf_RenderThisPageAsPdf.htm

PDF FileBehavior

"InBrowser" file behavior attempts to show the PDF directly in the User's browser. This is not always possible in every web browser, but a common feature of modern, standards compliant browsers.

IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);

"Attachment" file behavior causes the PDF to be downloaded.

IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);

PDF File Name

We may also set the file name of the PDF document by adding an additional parameter. This means we can control the name of the file when the user decides to download or keep it.

IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf");

PDF Print Options

We can control the output of the PDF by adding an instance of the IronPdf.PdfPrintOptions Class:

https://ironpdf.com/c%23-pdf-documentation/html/T_IronPdf_PdfPrintOptions.htm

var AspxToPdfOptions = new IronPdf.PdfPrintOptions()
    {
        DPI = 300,
        EnableJavaScript = false,
        //.. many more options available  
    };

IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
Dim AspxToPdfOptions As var = New IronPdf.PdfPrintOptions
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions)
VB   C#

The PDF PrintOptions available includes:

  • CreatePdfFormsFromHtml Turns ASPX form elements into editable PDF forms
  • CssMediaType Enables Media="screen" or "print" for CSS Styles and CSS3 StyleSheets
  • CustomCssUrl Allows a custom CSS style-sheet to be applied to Html by URL
  • DPI Output DPI resolution of the PDF
  • EnableJavaScript Enables JavaScript, jQuery and even Json code within the ASPX Page. A RenderDelay may need to be applied
  • FirstPageNumber First page number for Header and Footer. The default is 1
  • FitToPaperWidth Where possible, shrinks the PDF content to a width of 1 page of virtual paper
  • Footer Sets the footer content for every PDF page using content strings or even HTML
  • GrayScale Outputs a greyscale PDF in shades of grey instead of full color
  • Header Sets the header content for every PDF page using content strings or even HTML
  • InputEncoding The input character encoding as a string. UTF-8 is Default for ASP.NET
  • JpegQuality Quality of any image within the ASPX Page which is large enough to need to be re-sampled. Values range from 0-100. 100 is highest quality, but also largest file size
  • MarginBottom Bottom PDF Paper margin in millimeters. Set to zero for a borderless pdf
  • MarginLeft Left PDF Paper margin in millimeters. Set to zero for a borderless pdf
  • MarginRight Right PDF Paper margin in millimeters. Set to zero for a borderless pdf
  • MarginTop Top PDF Paper margin in millimeters. Set to zero for a borderless pdf
  • PaperOrientation The PDF paper orientation. Landscape or Portrait
  • PaperSize Set an output paper size for PDF pages using System.Drawing.Printing.PaperKind. Alternatively developers may use the SetCustomPaperSize(int width, int height) method to create custom sizes
  • PrintHtmlBackgrounds Prints HTML image backgrounds
  • RenderDelay Milliseconds to wait after Html is rendered before printing so that Javacsript or JSON have time to work
  • Title PDF Document 'Title' meta-data
  • Zoom A % Scale level allowing the developer to enlarge or shrink html content

Step 4: Adding Headers & Footers to ASPX PDFs

Using IronPdf, Headers and Footers can be added to the PDFs output.

The simplest way to do this is with the SimpleHeaderFooter class, which supports a basic layout that can easily add dynamic data such as the current time and page numbering.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspxToPdfTutorial
{
    public partial class Invoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var AspxToPdfOptions = new IronPdf.PdfPrintOptions()
            {
                Header = new IronPdf.SimpleHeaderFooter()
                {
                    CenterText = "Invoice",
                    DrawDividerLine = false,
                    FontFamily = "Arial",
                    FontSize = 12
                },
                Footer = new IronPdf.SimpleHeaderFooter()
                {
                    LeftText = "{date} - {time}",
                    RightText = "Page {page} of {total-pages}",
                    FontFamily = "Arial",
                    FontSize = 12,

                },
            };

            IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace AspxToPdfTutorial
    
    Public Class Invoice
        Inherits System.Web.UI.Page
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim AspxToPdfOptions = New IronPdf.PdfPrintOptions
            IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions)
        End Sub
    End Class
End Namespace
VB   C#

Alternatively we can generate HTML headers and footers using the HtmlHeaderFooter class which also supports CSS, Images and Hyperlinks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspxToPdfTutorial
{
    public partial class Invoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var AspxToPdfOptions = new IronPdf.PdfPrintOptions()
            {
                MarginTop = 50, // make sufficiant space for an HTML header
                Header = new IronPdf.HtmlHeaderFooter()
                {
                    HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>" 
                }

            };

            IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "MyDocument.pdf", AspxToPdfOptions);
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace AspxToPdfTutorial
    
    Public Class Invoice
        Inherits System.Web.UI.Page
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim AspxToPdfOptions = New IronPdf.PdfPrintOptions
            IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "MyDocument.pdf", AspxToPdfOptions)
        End Sub
    End Class
End Namespace
VB   C#

As seen in our examples, we may "merge" dynamic text or html into Headers / Footers using placeholders:

  • {page} for the current page number of the PDF
  • {total-pages} as the total number of pages within the PDF
  • {date} for today's date in a format appropriate to the server's system environment
  • {time} for the time in hours:seconds using a 24 hour clock
  • {html-title} inserts the title from the head tag of the ASPX web form
  • {pdf-title} for the document file name

Step 5: ASPX File to PDF Tricks: Page Breaks

Where as html commonly 'flows' into a long page, PDFs simulate digital paper and are broken into consistent pages. Adding the following code to your ASPX page will automatically create a page-break in the .Net generated PDF.

<div style='page-break-after: always;'>&nbsp;</div>
HTML

Step 6: Async & Multithreading for Performance

IronPDF was built for .Net Framework 4.0 or above. In Framework 4.5 or above projects ASYNC can be utilised to improve performance when working with multiple documents.

Combining Async with multithreaded CPUs and the Paralllel.ForEach command will improve bulk processing significantly.

Step 7: Downloading this Tutorial as ASP.Net Source Code

The full ASPX to PDF Converter Source Code for this tutorial is available to be downloaded as a zipped Visual Studio Web Application project.

Download this tutorial as a ASP.Net Visual Studio project

The free download contains working code examples for a C# ASP.Net Web Forms project showing a web page rendered as a PDF with settings applied.

Step 8: Explore this ASPX to PDF Tutorial on GitHub

The code for this C# ASPX-To-PDF project is available in C# and VB.NET on GitHub as a ASP.NEt website project.

We hope this helps you to get up and running creating and processing PDF documents in ASP.NET web applications quickly and efficiently.

Step 9: Going Forwards

Generally, the best way to learn any programming technique is through experimentation within your own ASP.Net projects.

Developers may also be interested in the IronPdf.AspxToPdf Class reference: https://ironpdf.com/c%23-pdf-documentation/html/T_IronPdf_AspxToPdf.htm

Software Product Designer @ Team Iron Learn how to turn any ASP.Net ASPX page into a PDF document into a PDF instead of HTML using a single line of code in C# or VB.Net

Jacob Müller

Software Product Designer @ Team Iron

Jacob is a committed software engineer who enjoys development and writing about the C# and Python programming languages.

Jacob's main interest is currently development of software modules for large scale intranet development projects using C# and ASP.NET MVC.

He is also interested in the future of .Net Core as the basis of new platform independent development technology that will provide desktop, server and web applications within a single, open source framework which puts developers and users before VMs.