Tuesday, 4 August 2020

iTextSharp To Create PDF In C#

8/04/2020 - By Pranav Singh 0


This article will show you how you can export html content to pdf in your asp.net application using iTextSharp.

So first we will create a new asp.net application an then install the nuget package for iTextSharp. Please check the below link and screen show.

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


Now open package installer and install the nuget package.


After installing add a button control in your page and generate the click event.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>HTML to PDF in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
        <h3>Export Html to PDF</h3><br />
        <div>
            <asp:Button ID="Button1" runat="server" Text="Export HTML to PDF" OnClick="Button1_Click" />
        </div>

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

Now in your code behind add the below code to export the html content from code behind to pdf.
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string htmlcontent = HTMLString();
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            List htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlcontent), null);

            for (int k = 0; k < htmlarraylist.Count; k++)

            {
                pdfDoc.Add((IElement)htmlarraylist[k]);
            }
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition""attachment;filename=sample.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
        }

   public string HTMLString()
        {
            StringBuilder htmltext = new StringBuilder();
            htmltext.Append("<div><h1>Welcome To aspdotnetpools</h1></div>");
            htmltext.Append("<table width='100%' border='1'>");
            htmltext.Append("<tr>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("</tr>");
            htmltext.Append("<tr>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("</tr>");
            htmltext.Append("<tr>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("</tr>");
            htmltext.Append("<tr>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("</tr>");
            htmltext.Append("<tr>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("</tr>");
            htmltext.Append("<tr>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("</tr>");
            htmltext.Append("<tr>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("<td>Data 1</td>");
            htmltext.Append("</tr>");
            htmltext.Append("</table>");
            htmltext.Append("<br/><br/>");
            htmltext.Append("Thanks you for downloading");
            htmltext.Append("aspdotnet-pools.com");

            return htmltext.ToString();
        }    
    }
}

In above code first I have prepared an html string and then exported it to pdf. Now run the application and check the output.


Here is the output.


DOWNLOAD

Tags:
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top