Use iTextSharp.dll to create pdf from html div. Here id of my html div is "PrintPdf" which is runat="server".
Here UniqueNo is the name for generated pdf.
public bool CreatePdf(string UniqueNo)
{
Document document = new Document(PageSize._11X17, 40, 50, 30, 65);
try
{
FileStream objFileStream = null;
if (File.Exists(Server.MapPath("PDF/" + UniqueNo + ".pdf").ToString()))
{
return true;
}
else
{
objFileStream = new FileStream(Server.MapPath("PDF/" + UniqueNo + ".pdf"), FileMode.Create);
}
PdfWriter.GetInstance(document, objFileStream);
document.Open();
var sb = new StringBuilder();
string head = "<html><head><meta name=\"Generator\" content=\"EditPlus\"><meta name=\"Author\" content=\"\"><meta name=\"Keywords\" content=\"\"><meta name=\"Description\" content=\"\"><link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\" /></head><body>";
string tail = "</body></html>";
PrintPdf.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
string htmlText = head + sb.ToString() + tail;
//make an arraylist ....with STRINGREADER since its no IO reading file...
var htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
document.Add((IElement)htmlarraylist[k]);
}
document.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
If the div contains the GridView control then override following method.
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
Here UniqueNo is the name for generated pdf.
public bool CreatePdf(string UniqueNo)
{
Document document = new Document(PageSize._11X17, 40, 50, 30, 65);
try
{
FileStream objFileStream = null;
if (File.Exists(Server.MapPath("PDF/" + UniqueNo + ".pdf").ToString()))
{
return true;
}
else
{
objFileStream = new FileStream(Server.MapPath("PDF/" + UniqueNo + ".pdf"), FileMode.Create);
}
PdfWriter.GetInstance(document, objFileStream);
document.Open();
var sb = new StringBuilder();
string head = "<html><head><meta name=\"Generator\" content=\"EditPlus\"><meta name=\"Author\" content=\"\"><meta name=\"Keywords\" content=\"\"><meta name=\"Description\" content=\"\"><link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\" /></head><body>";
string tail = "</body></html>";
PrintPdf.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
string htmlText = head + sb.ToString() + tail;
//make an arraylist ....with STRINGREADER since its no IO reading file...
var htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
document.Add((IElement)htmlarraylist[k]);
}
document.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
If the div contains the GridView control then override following method.
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
No comments:
Post a Comment