When you need to convert ASPX web pages to PDF files within your application, the easiest way to get the job done is to use a dedicated .NET library for PDF rendering.

GemBox.Document library does a good job of converting most content types from ASPX to HTML and then render it as a PDF file. Any text, lists, tables, images, forms and page layout can be converted to look almost identical to the web page in the browser. Some CSS features like pseudo-elements and floated items might not work perfectly. So we suggest you download the Free version of GemBox.Document and follow this example with your content to see how it works for your exact needs.

Quick example of ASPX to PDF conversion

The following example demonstrates how to convert an ASP.NET web page (a simple “About.aspx” page) into a PDF format and stream (download) the generated PDF file to client’s browser.

This is achieved by using a Page.Render method, in the code behind of the desired page, to obtain ASPX content in HTML format.

Then that HTML formatted content is loaded into a new document and that document is written, in PDF format, directly to HttpResponse.OutputStream.

ASPX source web page

The following screenshot shows an ASP.NET web page that is converted to PDF. Notice that it contains a button control (“Convert ASPX page to PDF”) which on click event downloads the PDF file created by converting the page’s ASPX code.

ASPX About Page Screenshot

Rendered PDF

The following screenshot shows the resulting PDF file.

PDF About Page Screenshot

ASPX code

The following code is the content of “About.aspx” file.
Notice that the page’s “EnableEventValidation” attribute is set to “false”. This is required in order to avoid the runtime error when calling Page.Render method.

<%@ Page Title="About" 
         Language="C#" 
         MasterPageFile="~/Site.Master" 
         AutoEventWireup="true" 
         CodeBehind="About.aspx.cs" 
         Inherits="WebApplication.About"
         EnableEventValidation="false" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<div style="font-family:Arial;font-size:14px;color:#333;text-align:center;">

<h1 style="font-size:48px;font-weightbold;margin-bottom:20px;">
Lorem Ipsum - <%: Title %> Page
</h1>

<p style="font-size:24px;">
<em>"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."</em>
</p>

<p style="color:coral;text-align:left;">
<strong>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</strong>
</p>

<p style="color:coral;text-align:right;">
Duis mattis est quis mauris pulvinar, aliquet mollis dui sagittis.
</p>

<table style="width:100%;border:2px solid #EEE;">
<tr><th>Lorem Ipsum 1</th><th>Lorem Ipsum 2</th><th>Lorem Ipsum 3</th></tr>
<tr><td style="background-color:#EEE;">1</td><td>2</td><td>3</td></tr>
<tr><td>11</td><td style="background-color:#EEE;">22</td><td>33</td></tr>
<tr><td>111</td><td>222</td><td style="background-color:#EEE;">333</td></tr>
</table>

<p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
</div>
 
<asp:Button ID="Button1" runat="server" Text="Convert ASPX page to PDF" OnClick="Button1_Click" />
</asp:Content>

C# code

The following code is the content of “About.aspx.cs” file, the page’s code behind in C# language.

using System;
using System.IO;
using System.Web.UI;
using GemBox.Document;

public partial class About : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set license key to use GemBox.Document in a Free mode.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Continue to use the component in a Trial mode when free limit is reached.
        ComponentInfo.FreeLimitReached += (sen, ev) => ev.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // Render ASPX page as HTML formatted string.
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.Render(htw);

        // Load HTML text to DocumentModel.
        string html = sw.ToString();
        DocumentModel document = new DocumentModel();
        document.Content.LoadText(html, LoadOptions.HtmlDefault);
 
        // Convert ASPX to PDF by exporting, downloading,
        // DocumentModel in PDF format from ASP.NET application.
        document.Save(this.Response, "About.pdf");
    }
 
    // Required to avoid the runtime error.
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}

VB.NET code

The following code is the content of “About.aspx.vb” file, the page’s code behind in VB.NET language.

Imports System
Imports System.IO
Imports System.Web.UI
Imports GemBox.Document
 
Partial Public Class About
    Inherits Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        ' Set license key to use GemBox.Document in a Free mode.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Continue to use the component in a Trial mode when free limit is reached.
        AddHandler ComponentInfo.FreeLimitReached, Sub(sen, ev) ev.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        ' Render ASPX page as HTML formatted string.
        Dim sw As New StringWriter()
        Dim htw As New HtmlTextWriter(sw)
        Me.Render(htw)

        ' Load HTML text to DocumentModel.
        Dim html As String = sw.ToString()
        Dim document As New DocumentModel()
        document.Content.LoadText(html, LoadOptions.HtmlDefault)

        ' Convert ASPX to PDF by exporting, downloading,
        ' DocumentModel in PDF format from ASP.NET application.
        document.Save(Me.Response, "About.pdf")
    End Sub

    ' Required to avoid the runtime error.
    Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    End Sub
End Class

Download the Free Version of GemBox.Document

GemBox.Document Free delivers the same performance and set of features as the Professional version. However, the Free version is limited to 20 paragraphs. You can use the free version for any purpose, including commercial applications.
If you want to see how fast and efficient GemBox.Document performs for large files, you can enable Trial mode right from within the free version.

Get the GemBox.Document Professional for unlimited use

GemBox.Document is a standalone .NET component for fast reading, writing, editing, converting and printing of DOCX, DOC, PDF, HTML, XPS, RTF, and TXT files using C# or VB.NET with one simple API.