---
title: "Getting error to generate pdf"  
description: "Getting error to generate pdf"  
author: "Anonymous User"  
published: 2013-04-09  
updated: 2014-01-07  
canonical: https://www.mindstick.com/forum/745/getting-error-to-generate-pdf  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Getting error to generate pdf

Hi All!\

```
protected void txt_btn_Click(object sender, EventArgs e){    Response.ContentType = "application/pdf";    Response.AddHeader("content-disposition", "attachment;filename=TestResult.pdf");    Response.Cache.SetCacheability(HttpCacheability.NoCache);    StringBuilder htmlText = new StringBuilder();    htmlText.Append("<table style='color:red;' border='1'><tr><th>createing pdf</th><tr><td> abcdef</td></tr></table>");    StringReader stringReader = new StringReader(htmlText.ToString());    Document doc = new Document(PageSize.A4);    List<iTextSharp.text.IElement> elements =            iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(stringReader, null);    doc.Open();    foreach (object item in elements)    {        doc.Add((IElement)item);    }    doc.Close();    // Response Output    PdfWriter.GetInstance(doc, Response.OutputStream);    doc.Open();    //doc.Close();    Response.Write("PDF is created");}}
```

\
I am try to create [pdf file](https://www.mindstick.com/forum/572/pdf-file-download-in-zip-file-in-mvc-4).But pdf is created only 0kb.Mean when i open this it's shw [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) that May be pdf damaged\
[Thank you in advance](https://answers.mindstick.com/qa/40482/why-was-1968-a-year-of-tension-and-turmoil-list-10-events-1-being-the-best-and-10-the-worst-thank-you-in-advance)!

## Replies

### Reply by Ely Sanders

Hi, you can also try the following:

```
protected void txt_btn_Click(object sender, EventArgs e)
{
    var doc = new DocumentModel();
    string htmlText = "<table
style='color:red;' border='1'><tr><th>createing
pdf</th><tr><td>
abcdef</td></tr></table>";
    doc.Content.LoadText(htmlText, LoadOptions.HtmlDefault);
    doc.Save(this.Response, "TestResult.pdf", SaveOptions.PdfDefault);
}
```

I believe this will simplify your code a bit. I used this C# Word component, it has a direct API call for exporting [PDF](https://www.mindstick.com/articles/311856/6-of-the-best-pdf-editors-to-meet-your-business-needs) files in ASP.NET.

### Reply by AVADHESH PATEL

Hi Jeet!\
you are using iTextSharp library so try as below\

```
private void GeneratePDF()    {        try        {            string pdfPath = "~/PDF/File_1.pdf";            StringBuilder sb = new StringBuilder();            sb.Append("Name : chamara" + Environment.NewLine);            sb.Append("Address : sri lanaka" + Environment.NewLine);            sb.Append("Institute : SLIIT" + Environment.NewLine);            Document doc = new Document();            PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(pdfPath), FileMode.Create));            doc.Open();            doc.Add(new Paragraph(sb.ToString()));            doc.Close();        }        catch (Exception ex)        {            throw ex;        }    }
```


---

Original Source: https://www.mindstick.com/forum/745/getting-error-to-generate-pdf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
