Reputation: 73
The below code that I am using for convert the panel of the data to PDF.But it is giving an error:
The document has no pages at the place of document.close()
Here is my code:
protected void ConvertPDF_click(object sender, EventArgs e)
{
string attachment = "attachment; filename=test.pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
htextw.AddStyleAttribute("font-size", "7pt");
htextw.AddStyleAttribute("color", "Black");
Panel1.RenderControl(htextw);//Name of the Panel
Document document = new Document();
document = new Document(PageSize.A4, 5, 5, 15, 5);
FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
Response.Write(document);
}
Upvotes: 3
Views: 6582
Reputation: 19166
htmlworker.Parse
method gives you the parsed elements. It parses the html elements and converts them to their iTextSharp equivalent elements. Now you should add them to the document.
Upvotes: 1
Reputation: 9372
Can't see what's in your Panel
server control, but your code looks OK. And if the Panel only contains simple HTML, you don't necessarily need to add HTML IElement
objects individually (when calling HTMLWorker.ParseToList()
) to the Document
object as suggested by @VahidN. Here's a simple example - .aspx
file:
<%@ Page Language='C#' AutoEventWireup='true' CodeFile='panelTest.aspx.cs' Inherits='panelTest' %>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat='server'><title></title></head>
<body><form id='form1' runat='server'>
<asp:Panel ID='testPanel' runat='server'>
<h1>A H1 Heading</h1>
<table width='100%' border='1' align='center'
cellpadding='4' cellspacing='0'
>
<tr><td>TABLE ROW 1: CELL 1</td></tr>
<tr><td>TABLE ROW 2: CELL 1</td></tr>
</table>
<p>A Paragraph with <strong>bold</strong> and <em>italic</em> text.</p>
</asp:Panel>
<asp:Button runat='server'
oncommand='process'
text='Convert HtmlControl to PDF'
/>
</form></body></html>
Code-behind file:
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
public partial class panelTest : Page {
protected void process(object sender, CommandEventArgs e) {
string attachment = "attachment; filename=test.pdf";
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
htmlWriter.AddStyleAttribute("font-size", "10pt");
htmlWriter.AddStyleAttribute("color", "Black");
testPanel.RenderControl(htmlWriter);
using (Document document = new Document()) {
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
StringReader stringReader = new StringReader(stringWriter.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(stringReader);
}
Response.End();
}
}
With that being said - if the Panel contains child controls or complex HTML (images for example), you will have problems. You need to take into account that HTMLWorker is a very simple parser.
Upvotes: 1