Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how do i convert div region into pdf and excel format using c# in offline
I want to print the div region data as pdf format
Would Any one help me
Posted

you can use iTextSharp to generate PDF file. use package manager to install it.
https://www.nuget.org/packages/itextsharp/[^]

ASP.NET
<div id="MyDiv"  runat="server">
<p>div content</p>
</div>


in your code, for example button click event:
C#
protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=div.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
   
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
    MyDiv.RenderControl(htmlTextWriter);
   
    StringReader stringReader = new StringReader(stringWriter.ToString());
    Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(Doc);
    PdfWriter.GetInstance(Doc, Response.OutputStream);
   
    Doc.Open();
    htmlparser.Parse(stringReader);
    Doc.Close();
    Response.Write(Doc);
    Response.End();
}

you need to add below using statements at the top of your c# code

C#
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900