Showing Nothing in created PDF using iTextSharp? - c#

i am trying to export my HTML page to PDF using iTextSharp lib. firstly i am trying to print table which is in my Aspx page.PDF is created successfully but there nothing in PDF.
i am using this code below :
protected void btnExportToPdf_Click(object sender, EventArgs e)
{
btnExportToPdf.Visible = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
exportTable.RenderControl(hw);
//this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End(); }

Now I'm not too seasoned, but the problem to me looks like you are never actually writing the strings to the file. I see a few different string objects you initialize, and I see you adding the HTML to it, but I never see you actually write them to the PDF. This is my usual process for parsing HTML to PDF, maybe you can look at it, and see what you are missing.
System.Text.StringBuilder store = new System.Text.StringBuilder();
string line;
while ((line = htmlReader.ReadLine()) != null)
{
store.Append(line + Environment.NewLine);
}
string html = store.ToString();
FileStream stream = new FileStream(newFileName, FileMode.Create, FileAccess.Write);
Document document = new Document(PageSize.LETTER, 15, 15, 35, 25);
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
System.Collections.Generic.List<IElement> htmlarraylist = new List<IElement>(HTMLWorker.ParseToList(new StringReader(html), new StyleSheet()));
foreach (IElement element in htmlarraylist)
{
document.Add(element);
}
document.Close();
That was what jumped out to me at least, was the fact that it doesn't look like you actually write the generated text to the PDF. I hope this helps.

Related

PDF not getting download in iTextSharp [duplicate]

I am trying to save a panel to pdf using ITextSharp. When I set a breakpoint and debug, the code stops on this line of code, however it does not give an error and just stops.
if (IsPostBack)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Quote.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
**Panel1.RenderControl(hw);**
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
In the panel I have textboxes, labels and GridView.
You've got a couple of problems. If you're getting a The document has no pages message that means no content was added to the PDF which almost definitely means that there's a problem with the RenderControl method and nothing to do with iTextSharp. Another problem is that you are writing to the Response.OutputStream as well as outputting the literal document by using Response.Write(pdfDoc). You only need to do the former, I'm not actually sure what would happen with the latter.
The The document has no pages message also could mean that iTextSharp couldn't find anything to create content from. So you might have valid HTML but iTextSharp just doesn't know what to do with it. This probably isn't the case but I just had to mention it.
I'm going to break your code up into steps to help debug things. The first two blocks convert the control to HTML and then sanity checks it to make sure we've got something. You should inspect the contents of buf if you have any problems.
The third block creates a PDF from a MemoryStream and then dumps it into a byte array instead of writing directly to the Response.OutputStream. Although there's nothing incorrect with writing to the Response.OutputStream you will often find that you're swallowing errors and in general debugging is much harder. Once this block is done you should inspect bytes to make sure it has something.
The last block does the final push to the browser.
//Convert our control to HTML
string buf;
using (var sw = new StringWriter()) {
using (var hw = new HtmlTextWriter(sw)) {
Panel1.RenderControl(hw);
}
buf = sw.ToString();
}
//Sanity check
if (String.IsNullOrWhiteSpace(buf)) {
throw new ApplicationException("No content found");
}
//Create our PDF and get a byte array
byte[] bytes;
using (var ms = new MemoryStream()) {
using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f)) {
using (var writer = PdfWriter.GetInstance(pdfDoc, ms)) {
pdfDoc.Open();
using (var htmlparser = new HTMLWorker(pdfDoc)) {
using (var sr = new StringReader(buf)) {
htmlparser.Parse(sr);
}
}
pdfDoc.Close();
}
}
bytes = ms.ToArray();
}
//Output the PDF
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Quote.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();

Converting a modal dialog window to pdf

I'm trying to convert a modal dialog with 3 DataTables to pdf using iTextSharp:
DataTable dtTable = (DataTable)ViewState["LoanPlan"];
DataTable dtTax = (DataTable)ViewState["PlanTaxData"];
DataTable dtPlan = (DataTable)ViewState["PlanAllData"];
I'm using this code to try and accomplish it:
protected void buttonPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
But it seems that it's actually trying to convert the whole .aspx page to .pdf format. How should I go about targeting just the modal dialogue or just the 3 DataTables?

iTextSharp is not showing HTML table in PDF

I don't understand why it is not working.
Here is my code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
string htmlstr = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p><table border=1><tr><td>1st</td><td>2nd</td></tr><tr><td>3rd</td><td>4th</td></tr></table></body></html>";
Panel panel1 = new Panel();
panel1.Controls.Add(new LiteralControl(htmlstr));
panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
My PDF is generated but the table is not showing ...
You've got some crazy code there (which appears to be from here). You've got an HTML string but then your dumping it into an ASP.Net control which you're further dumping into another ASP.Net control. Then you're asking ASP.Net to render the control back to HTML. That's three or four lines you can kill off.
Also, you're writing to the raw HTTP response stream and then sending the PDF to the same stream. This is your bigger problem, actually. I strongly suggest never writing to the raw stream until you're absolutely done processing things. You've also changed the HTTP headers which can cause problems if there are ASP.Net errors.
The below code is a rework of what you've got. I switched it over to using statements to ensure that things get cleaned up. If you're using an older unsupported version of iTextSharp you'll want to switch those back.
string htmlstr = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p><table border=1><tr><td>1st</td><td>2nd</td></tr><tr><td>3rd</td><td>4th</td></tr></table></body></html>";
//We'll store our final PDF in this
byte[] bytes;
//Read our HTML as a .Net stream
using (var sr = new StringReader(htmlstr)) {
//Standard PDF setup using a MemoryStream, nothing special
using (var ms = new MemoryStream()) {
using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f)) {
//Bind a parser to our PDF document
using (var htmlparser = new HTMLWorker(pdfDoc)) {
//Bind the writer to our document and our final stream
using (var w = PdfWriter.GetInstance(pdfDoc, ms)) {
pdfDoc.Open();
//Parse the HTML directly into the document
htmlparser.Parse(sr);
pdfDoc.Close();
//Grab the bytes from the stream before closing it
bytes = ms.ToArray();
}
}
}
}
}
//Assuming that the above worked we can now finally modify the HTTP response
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=BusinessUnit.pdf");
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
//Send the bytes from the PDF
Response.BinaryWrite(bytes);
Response.End();

Saving Panel to PDF not working nor getting errors

I am trying to save a panel to pdf using ITextSharp. When I set a breakpoint and debug, the code stops on this line of code, however it does not give an error and just stops.
if (IsPostBack)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Quote.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
**Panel1.RenderControl(hw);**
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
In the panel I have textboxes, labels and GridView.
You've got a couple of problems. If you're getting a The document has no pages message that means no content was added to the PDF which almost definitely means that there's a problem with the RenderControl method and nothing to do with iTextSharp. Another problem is that you are writing to the Response.OutputStream as well as outputting the literal document by using Response.Write(pdfDoc). You only need to do the former, I'm not actually sure what would happen with the latter.
The The document has no pages message also could mean that iTextSharp couldn't find anything to create content from. So you might have valid HTML but iTextSharp just doesn't know what to do with it. This probably isn't the case but I just had to mention it.
I'm going to break your code up into steps to help debug things. The first two blocks convert the control to HTML and then sanity checks it to make sure we've got something. You should inspect the contents of buf if you have any problems.
The third block creates a PDF from a MemoryStream and then dumps it into a byte array instead of writing directly to the Response.OutputStream. Although there's nothing incorrect with writing to the Response.OutputStream you will often find that you're swallowing errors and in general debugging is much harder. Once this block is done you should inspect bytes to make sure it has something.
The last block does the final push to the browser.
//Convert our control to HTML
string buf;
using (var sw = new StringWriter()) {
using (var hw = new HtmlTextWriter(sw)) {
Panel1.RenderControl(hw);
}
buf = sw.ToString();
}
//Sanity check
if (String.IsNullOrWhiteSpace(buf)) {
throw new ApplicationException("No content found");
}
//Create our PDF and get a byte array
byte[] bytes;
using (var ms = new MemoryStream()) {
using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f)) {
using (var writer = PdfWriter.GetInstance(pdfDoc, ms)) {
pdfDoc.Open();
using (var htmlparser = new HTMLWorker(pdfDoc)) {
using (var sr = new StringReader(buf)) {
htmlparser.Parse(sr);
}
}
pdfDoc.Close();
}
}
bytes = ms.ToArray();
}
//Output the PDF
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Quote.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();

export gridview data to pdf

its throwing error like document has no pages.
i have written code like this.
protected void Button4_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
can any1 help me on this
I suggest reading the documentation of the library you are using.
The error suggests that you need to add a page to the new document before you try and add content to it.
In some of the PDF libraries you add a page and then need to set it as the current page before adding content.

Categories

Resources