In this article I will explain how to export ASP.Net Web Page to
PDF i.e. Portable Document Format.
Step1: you need
to download Itextsharp you can refer to this article how you can download Itextsharp
and add its reference to your project
Step2: Create a page with image some text and an HTML table. Also there’s an export button on click of which we will export the page to PDF.
<div>
<img alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjGdMyPJD_N0rauzUJj2vqH70orjQZrthEpaYoYNn00xuR300bAHEC1lxKteRlpzPwzUe2DAyC8gp_8GY9JkRlu8Qdxp6OIyUA_5OjPmiK17u4jWG-4bsjumMBSZ-304WTeIO3PIoJY-8/s1600/happy-face.png"
/>
</div>
<br />
<div style = "font-family:Arial; color:Fuchsia;">This
is Demo to convert html page to pdf using ItextSharp<br />
<br />
</div>
<div>
<table border = "1" width = "100" style="background-color:green;color:white;" >
<tr><td>Name</td><td>Age</td></tr>
<tr><td>Tes1</td><td>14</td></tr>
<tr><td>Tes2</td><td>13</td></tr>
<tr><td>Tes3</td><td>12</td></tr>
</table>
</div>
<div>
<asp:Button ID="btnExport"
runat="server"
Text="Export"
onclick="btnExport_Click"
/></div>
<asp:Label ID="Label1" runat="server"
Text="This label
and Above button will not appear in exported file"></asp:Label>
Step3:
You will need to import the following namespaces in your code.
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
Step4:
The below event handler is executed on the click of the export
button.
public override
void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void
btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=ConvertedPDF.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new
StringWriter();
HtmlTextWriter hw = new
HtmlTextWriter(sw);
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();
}
If you observe above code I added one function
that is VerifyRenderingInServerForm this function is used
to avoid the error like “control must be placed in inside of form tag”. If we set VerifyRenderingInServerForm function then compiler will think that controls rendered before
exporting and our functionality will work perfectly
Note: If in your Exported pdf images are coming this is due to path issue you must check image path you have this issue
Note: If in your Exported pdf images are coming this is due to path issue you must check image path you have this issue
i m getting error:
ReplyDeletethe error is:input string was not in correct format
can you please give me line which is giving error
DeleteUnable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Rectangle'.
ReplyDeleteHello Mansi
Deletethis error this is due to different itextsharp version. if you use same version of dll that is given in code than this code will work. in latest version
`HTMLWorker' has been deprecated in favor of XMLWorker
Thanks
when the above code using
ReplyDeleteim geting this type of error:
RegisterForEventValidation can only be called during Render();
kindly help
either use
Deletepublic override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
or
Delete@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"
I want to convert panel to the pdf but in that panel there is one image tag & it gives error :
ReplyDeletecould not find the part of path of image placed inside the panel
please check your image path it is not pointing to correct path
Deleteor you can give me the code
Response.ContentType = "application/pdf";
DeleteResponse.AddHeader("content-disposition", "attachment;filename=Receipt.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
receipt.RenderControl(hw);
PanelPayment1.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();
I put image tag in the receipt panel & assign the ImageUrl from the .cs file
Deleteas the code below
logo.ImageUrl = "~/Profile_Pic/" + dr["Logo"].ToString();
& when i tried to convert into the pdf the error occor as:
DeleteCould not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\Profile_Pic\index.jpg'.
it search the image on wrong path
The easiest solution should be to change: use
Deleteimg src="http://www.yourwebsite.com/Images/flower.jpg" />
Otherwise you can use Server.MapPath("Profile_Pic/index.jpg) to get the full URL by code. However that implies you having to know the relative path to the image (meaning access to the raw html) at compile-time, which makes your code less maintainable.
you can refer to below link for more information
http://www.mikesdotnetting.com/article/87/itextsharp-working-with-images
thanks for the reply
DeleteI prefer first solution & that error solved but new error occur now:
Font size too small: 0
but now this same code is executed in another form & generated pdf
ReplyDeletebut in that pdf the alignment & size of image is changed even alignment of the text content also not proper
i'm getting error " Illegal characters in path"
ReplyDeleteon the line
htmlparser.Parse(sr);
please provide the solution ASP
thanks
i'm getting error " Illegal characters in path"
ReplyDeleteon the line
htmlparser.Parse(sr);
please provide the solution ASP
thanks
am also getting the same error.did u clear ur error? how?
Deletei am getting this error "A page can have only one server-side Form tag."
ReplyDeleteCan you please help me
can u show me your aspx page code.
Deletegive me your code
DeleteThis comment has been removed by the author.
ReplyDeleteWhen I try to run the progrm I am getting an error saying
ReplyDeleteAn exception of type 'System.ArgumentException' occurred in System.Web.Extensions.dll but was not handled in user code
Additional information: Extender control 'MaskedEditExtenderCP' is not a registered extender control. Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors().
Manish I'm getting register through RegisterScriptControl() before calling RegisterScriptDescriptors(). I followed the above steps: public override void VerifyRenderingInServerForm(Control control)
ReplyDelete{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
EnableValidation=false nothing seems to be working
hi, i want the outside border in pdf generation. want to remove the td and tr borders.
ReplyDeletemultiple language not suppported..how do u do make it as multiple language supported one
ReplyDeleteHi, i have a task to export html code to pdf file. i tried it with your code, but there is an error at the line " HTMLWorker htmlparser = new HTMLWorker(pdfDoc);" , the 'HTMLWorker' has a green line. tq :)
ReplyDeleteam getting a warning of htmlworker is obsolete
ReplyDelete