ASP.NET – Creating and Downloading PDF Files

  • by
asp-net-create-pdf-file-sample

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:

Default.aspx:

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Multi-File upload</title>
<style>

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

</div>
</form>
</body>
</html>

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:

DOWNLOAD SOURCE CODE

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

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)
{
try
{
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", "", "http://www.color.org", "sRGB IEC61966-2.1",
new FileStream(filePath, FileMode.Open, FileAccess.Read)));

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

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

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

imgKid.GetAccessibilityProperties().SetAlternateDescription("kid");
p.Add(imgKid);
p.Add(" is laughing");

document.Add(p);
document.Close();

var httpContext = HttpContext.Current;
httpContext.Response.Buffer = true;
httpContext.Response.ContentType = "application/pdf";
httpContext.Response.AddHeader("content-disposition", "attachment;filename="+ fileName);
httpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
httpContext.Response.Write(document);
httpContext.Response.Flush();
httpContext.Response.SuppressContent = true;
httpContext.ApplicationInstance.CompleteRequest();

}
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.

asp-net-create-pdf-file-sample

DOWNLOAD SOURCE CODE

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.