This article explains how to use iTextSharp and its features in ASP.Net.
What is ITextSharp? iTextSharp is a free and open source assembly that helps to convert page output or HTML content in a PDF file.
You can download it from here:
http://sourceforge.net/projects/itextsharp/
Now add that DLL in the application.
Getting Started: Start Visual Studio and create a new website in ASP.Net and add these 2 DLLs to the solution.
- Itextsharp.dll
- itextsharp.pdfa.dll
Now let's work on the UI part.
- <div id="print" runat="server">
- <h1>About Raj Kumar</h1>
- <div>
- <table>
- <tr>
- <td>
- <img src="http://www.c-sharpcorner.com/UploadFile/AuthorImage/raj1979.jpg" height="100" width="120" />
- </td>
- <td></td>
- </tr>
- <tr>
- <td>Name: </td>
- <td>Raj Kumar</td>
- </tr>
- <tr>
- <td>Designation: </td>
- <td>Asst. Project Manager</td>
- </tr>
- <tr>
- <td>Work Experience: </td>
- <td>9 Years in IT</td>
- </tr>
- <tr>
- <td>About: </td>
- <td>Raj Kumar is two times Microsoft MVP(Client App Dev) and four times C# Corner MVP, working as asst project manager with many hands on experience using ASP.NET 2.0/3.5, AJAX, MVC, C#, Visual Basic .NET, SQL Server 2005/2008, Oracle, WPF, WCF, XAML, HTML5, jQuery, Windows Phone, Web Api, LightSwitch 2012 and Silverlight. He has over 9 years of IT experience working most on Microsoft technologies. He holds Master's degree in Computer Science. When he is not writing code, he likes to write articles and play cricket. You can read my blogs here http:
- </tr>
- <tr>
- <td>Location: </td>
- <td>New Delhi INDIA</td>
- </tr>
- </table>
- </div>
- </div>
- <div>
- <asp:Button ID="btnCreatePDF" runat="server" Text="Create PDF" OnClick="btnCreatePDF_Click" />
- </div>
As you can see there is a div "<div id="print" runat="server"> " and a table inside of that div, so I need to export this content to a PDF file on button click.
Let's add these namespaces first in code behind.
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.html;
- using System.IO;
- using System.Text.RegularExpressions;
- using iTextSharp.text.html.simpleparser;
And write this code for the button click event.
- protected void btnCreatePDF_Click(object sender, EventArgs e)
- {
- Response.ContentType = "application/pdf";
- Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- StringWriter sw = new StringWriter();
- HtmlTextWriter w = new HtmlTextWriter(sw);
- print.RenderControl(w);
- string htmWrite = sw.GetStringBuilder().ToString();
- htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", "");
- htmWrite = htmWrite.Replace("\r\n", "");
- StringReader reader = new StringReader(htmWrite);
- Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
- string pdfFilePath = Server.MapPath(".") + "/PDFFiles";
- HTMLWorker htmlparser = new HTMLWorker(doc);
- PdfWriter.GetInstance(doc, Response.OutputStream);
- doc.Open();
- try
- {
- htmlparser.Parse(reader);
- doc.Close();
- Response.Write(doc);
- Response.End();
- }
- catch (Exception ex)
- { }
- finally
- {
- doc.Close();
- }
- }
To stop this error "RegisterForEventValidation can only be called during Render();", you need to write this method.
- public override void VerifyRenderingInServerForm(Control control)
- {
-
- }
Hit F5 to see the output.
Image 1.
Now click on the button.
Image 2.
When the button is clicked there is one PDF file opened like this, as you will see all the details have been exported to the PDF file. For more information download the attached sample.