Tutorial Aspx To PDF
Tutorial Aspx To PDF
Tutorial Aspx To PDF
ASPX to PDF
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Web.UI;
6. using System.Web.UI.WebControls;
8. using IronPdf;
7. namespace AspxToPdfTutorial
ASPX Pages to
PDF in ASP .NET
by Jacob Müller
This ASPX to PDF tutorial will guide you step-by-step how to save an ASPX page as a PDF in ASP.NET
web applications. Apply settings including setting file behavior and names, adding headers & footers,
changing print options, adding page breaks, combining Async & Multithreading, and more.
Table of Contents
1
ASP.NET to PDF Conversion
Follow these guiding steps:
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 in PDF format.
This tutorial shows how to use the IronPDF software component for .NET to turn any ASP.Net web form into
a PDF (ASP .Net to PDF). HTML, normally rendered as a web page, will be used to render as a PDF for
download or viewing in a web browser. The attached source project will show you how to convert a
webpage to PDF in Asp .net using C#.
We achieve this HTML to PDF conversion (ASPX to PDF) when rendering webpages using the IronPDF
library and its AspxToPdf Class.
Step 1
or
This will work in any C# .Net Framework project from Framework 4 and above, or .Net Core 2 and above. It
will also work just as well in VB.Net projects.
2
PM > Install-Package IronPdf
https://www.nuget.org/packages/IronPdf
Remember to add this statement to the top of any cs class file using IronPDF:
using IronPdf;
How to Tutorials
In the attached example source code, we rendered a business invoice "Invoice.aspx," a simple HTML business
invoice rendered as an ASP .NET Page.
The HTML page contains CSS3 stylesheets and may also include images and javascript.
To render this ASP.NET Web Page to a PDF instead of HTML, we need to open the C# (or VB.Net) code and
add this to the Page_Load event:
1. IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
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.
3
The entire C# code reads like this in full:
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Web.UI;
6. using System.Web.UI.WebControls;
7. using IronPdf;
8.
9. namespace AspxToPdfTutorial
10. {
11. { public partial class Invoice : System.Web.UI.Page
12. {
13. protected void Page_Load(object sender, EventArgs e)
14. {
15. IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
16. }
17. }
18. }
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf");
4
3.3. Change 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
5
4. Add Headers & Footers to ASPX PDFs
Using IronPDF, Headers and Footers can be added to the PDF 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.
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Web.UI;
6. using System.Web.UI.WebControls;
7. namespace AspxToPdfTutorial
8. {
9. public partial class Invoice : System.Web.UI.Page
10. {
11. protected void Page_Load(object sender, EventArgs e)
12. {
13. var AspxToPdfOptions = new IronPdf.PdfPrintOptions()
14. {
15. Header = new IronPdf.SimpleHeaderFooter()
16. {
17. CenterText = "Invoice",
18. DrawDividerLine = false,
19. FontFamily = "Arial",
20. FontSize = 12
21. },
22. Footer = new IronPdf.SimpleHeaderFooter()
23. {
24. LeftText = "{date} - {time}",
25. RightText = "Page {page} of {total-pages}",
26. FontFamily = "Arial",
27. FontSize = 12,
28. },
29. };
30.
31.
32. IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf",
33. AspxToPdfOptions);
34. }
35. }
36. }
6
Alternatively we can generate HTML headers and footers using the HtmlHeaderFooter class, which also
supports CSS, images, and hyperlinks.
1. using System.Collections.Generic;
2. using System.Linq;
3. using System.Web;
4. using System.Web.UI;
5. using System.Web.UI.WebControls;
6. namespace AspxToPdfTutorial
7. {
8. public partial class Invoice : System.Web.UI.Page
9. {
10. protected void Page_Load(object sender, EventArgs e)
11. {
12. var AspxToPdfOptions = new IronPdf.PdfPrintOptions()
13. {
14. Header = new IronPdf.SimpleHeaderFooter()
15. {
16. MarginTop = 50, // make sufficiant space for an HTML header
17. Header = new IronPdf.HtmlHeaderFooter()
18. {
19. HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page
20. {page} of {total-pages}</em></div>"
21. }
22. };
23. IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment,
24. "MyDocument.pdf", AspxToPdfOptions);
25. }
26. }
27. }
As seen in our examples, we may "merge" dynamic text or html into Headers / Footers using placeholders:
7
6. Combine Async & Multithreading for Performance
IronPDF was built for .NET Framework 4.0, or .NET Core 2 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.
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. We hope this tutorial has helped you to learn how to save an
ASPX page as PDF.
Going Forwards
Generally, the best way to learn any programming technique is through experimentation within your own
ASP.NET projects. This includes trying the ASPX to PDF Converter from IronPDF.
Download
8
Explore this 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. Please go ahead and fork us on Github for
more help using IronPDF. Feel free to share this with anyone who might be
asking, 'How do I Convert ASPX to PDF?'
Download