ASP.NET – Creating and Downloading PDF Files

  • by

PDF documents are an integral part of todays reporting ecosystem. PDF documents can be used as an non-editable and proof report for different scenarios. In ASP.NET web application we can create this PDF documents using iText7.

About iText7 library:

iText7 is a library for creating PDF files in Java and .NET. It was developed by Bruno Lowagie. It was initially published as an open-sourced library under the Mozilla Public License or the GNU Library General Public License open source licenses. As of today, iText7 core is an open source library.

Create PDF files in ASP.NET:

In your ASP.NET project add iText7 nuget package.iText7 nuget package

In this example we will create a very simple PDF document for our ASP.NET project. In the upcoming tutorial I will post on creating more complex PDF reports.

Edit your Default.aspx page as below:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AspTutorial.Default" %>

<!DOCTYPE html>
<html xmlns="">
<head runat="server">
<title>ASP.NET Multi-File upload</title>

<body style="font-family:Arial, Helvetica, sans-serif">
<form id="form1" runat="server">
<asp:Button ID="btnCreatePDF" runat="server" OnClick="btnCreatePDF_Click" Text="Create PDF"/>


This page contains only one button for creating the PDF file.

Create a folder named PDFResources and add following resources to it:

FreeSans.ttf, FreeSansBold.ttf (both are true type font face file), sRGB_CS_profile.icm (this is a color profile file from official documentation), kid.jpg (you can use any image you like).

This files are available on iText official tutorial document or you can also directly download source code from below link:


Edit the code-behind of the Default.aspx file as below.

using iText.IO.Font;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Pdfa;
using System;
using System.IO;
using System.Web;
using System.Web.UI;

namespace AspTutorial
public partial class Default : Page
protected void Page_Load(object sender, EventArgs e)


protected void btnCreatePDF_Click(object sender, EventArgs e)
string filePath = Server.MapPath("PDFResources/sRGB_CS_profile.icm");
string fontFile = Server.MapPath("PDFResources/FreeSans.ttf");
string fileName = "sample" + DateTime.Now.ToString("ddMMMyyyyHHmmss") + ".pdf";
PdfADocument pdf = new PdfADocument(new PdfWriter(Server.MapPath(fileName)), PdfAConformanceLevel.PDF_A_1B,
new PdfOutputIntent("Custom", "", "", "sRGB IEC61966-2.1",
new FileStream(filePath, FileMode.Open, FileAccess.Read)));

PdfFont font = PdfFontFactory.CreateFont(fontFile, PdfEncodings.WINANSI,
Document document = new Document(pdf);

Paragraph p = new Paragraph();
p.Add(new Text("A "));

Image imgKid = new Image(ImageDataFactory.Create(Server.MapPath("PDFResources/kid.jpg")));

p.Add(" is laughing");


var httpContext = HttpContext.Current;
httpContext.Response.Buffer = true;
httpContext.Response.ContentType = "application/pdf";
httpContext.Response.AddHeader("content-disposition", "attachment;filename="+ fileName);
httpContext.Response.SuppressContent = true;

catch (Exception ex)


Now run the form and press on the PDF button. A PDF document will be generated in your project’s root folder and will be downloaded during runtime.



Also see:

ASP.NET – Uploading multiple files using FileUpload control

ASP.NET – Create and Write Text to file

ASP.NET – Insert data using SQL Stored Procedures

Create ASP.NET Login page with SQL Database

You can also create PDF document using Node.js with PDFKit. In the previous tutorial details description on creating PDF files and PDF Invoices documents in Node.js is given.

In the upcoming tutorial I will share how we can create PDF invoices using ASP.NET.

  1. - Realtime Encode your string to Base64 format.
  2. - Realtime Decode Base64 data to plain text.
  3. - Realtime Encoding in Md5Hash.