Create PDF from html with Radio Buttons, Textbox & other controls in asp.net.

Aman Sharma
2
The main focus of the article is to learn how to create PDF from HTML in ASP.NET using C#. We can use iTextSharp for this purpose, but problem occurs when we need to show radio button of webpage on PDF.  iTextSharp has some limitation.
It is not easy to achieve this using iTextSharp.
One solution is to use Paid API or Use free community versions of these PDF generator.
In this article I am using Select.PDF to convert ASP.NET webpage to PDF using C#. Community Version of the PDF generator is free and enough to complete our basic needs.


Follow These steps:

Download  Select.HtmlToPdf.dllSelect.Html.dep  from this link:


OR
To install Select.Pdf Html To Pdf Converter for .NET - Community Edition (FREE), run the following command in the Package Manager Console

PM> Install-Package Select.HtmlToPdf

Now Design your application:

Add a form in your application like as given below:

HTML of Sample Webpage:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>  
</head>
<body>
    <form id="form1" runat="server">
        <asp:Panel ID="pnlPerson" runat="server">      
             <div>Generate PDF of HTML in ASP.NET</div>
    <table
        <tr><td colspan="4"><strong>1. Select Radio Button from given Below</strong></td></tr>
        <tr>
<td><asp:RadioButton ID="rdb1" Text="Radio1" runat="server" Font-Italic="True" GroupName="1a" /> </td>
<td><asp:RadioButton ID="rdb2" Text="Radio2" runat="server" Font-Italic="True" GroupName="1a" />
            </td>
<td><asp:RadioButton ID="rdb3" Text="Radio3" runat="server" Font-Italic="True" GroupName="1a" />
            </td>
<td><asp:RadioButton ID="rdb4" Text="Radio4" runat="server" Font-Italic="True" GroupName="1a" />
            </td>
</tr>      
 <tr><td colspan="4">&nbsp;</td></tr>
      
          <tr><td colspan="4"><strong>2 </strong>Enter Data in Following TextBox </td></tr>
          <tr><td colspan="2">
              <asp:TextBox ID="textbox1" runat="server" Width="200px"></asp:TextBox>
              </td><td>&nbsp;</td><td>&nbsp;</td></tr>

          <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
        </tr>          
      
        <tr>
            <td colspan="4">
                <p class="MsoNormal">
                    In this article I am using Select.PDF to convert ASP.NET webpage to PDF using C#. Community Version of the PDF generator is free and enough to complete our basic needs.</p>
            </td>
        </tr>
                  
    </table></asp:Panel>
             <asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export" Width="73px" />
    </form>
</body>
</html>

Now write the following code in Code behind:

Add Following Namespace:
using System.Text;
using System.Threading;
using System.IO;
using SelectPdf;

Now write following Code on Button Click Event:

protected void btnExport_Click(object sender, EventArgs e)
    {
        var sb_htmlCode = new StringBuilder();
        pnlPerson.RenderControl(new HtmlTextWriter(new StringWriter(sb_htmlCode)));
        string htmlCode = sb_htmlCode.ToString();

         // read parameters from the webpage
            string htmlString = htmlCode;
            string baseUrl = "";

            string pdf_page_size ="A4";
            PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
                pdf_page_size, true);

            string pdf_orientation = "Portrait";
            PdfPageOrientation pdfOrientation =
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                pdf_orientation, true);

            int webPageWidth = 800;
            try
            {
                webPageWidth = Convert.ToInt32(800);
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = Convert.ToInt32("");
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.WebPageHeight = webPageHeight;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

            // save pdf document
            doc.Save(Response, false, Server.MapPath("~/output.pdf"));

            // close pdf document
            doc.Close();
        }


Demo: 

Input Page:
Click Image to Enlarge View

Output PDF:

Click Image to Enlarge View

Post a Comment

2Comments
  1. It works perfectly... I straggle to print radio button in pdf... with your tutorial i done my work in perfect time... thank u

    ReplyDelete
Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !