Click here to Skip to main content
15,920,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
// Create a Document object
        Document doc = new Document(PageSize.A4,20f,20f,0f,10f);
        // Create a new PdfWriter object, specifying the output stream
        StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
            DivToPDF.RenderControl(w);
            //TbltoPDF.RenderControl(w);
            string s = sw.GetStringBuilder().ToString();

 MemoryStream stream = new MemoryStream();
            PdfWriter writer =  PdfWriter.GetInstance(doc, stream);
           // PdfWriter.GetInstance(doc, stream);
            StringReader sr = new StringReader(s);
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            //this creates a new destination to send the action to when the document is opened. The zoom in this instance is set to 0.75f (75%). Again I use doc.PageSize.Height to set the y coordinate to the top of the page. PdfDestination.XYZ is a specific PdfDestination Type that allows us to set the location and zoom.
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 0.75f);
            
            // Open the Document for writing
            doc.Open();
            
            iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("App_Themes/BasicBlue/images/default_01.gif"));
            gif.Alignment = iTextSharp.text.Image.HEADER ;
            //gif.SetAbsolutePosition(30,760);
            gif.SetAbsolutePosition(320, 760);
            //gif.ScalePercent(75f, 75f); // change it's size
            // gif.ScaleToFit(300f, 100f);
            doc.Add(gif);
            iTextSharp.text.html.simpleparser.HTMLWorker parse = new iTextSharp.text.html.simpleparser.HTMLWorker(doc);
            parse.Style.LoadTagStyle("td", "size", "10px");
            parse.Style.LoadTagStyle("body", "family", "Zurich BT");
            //parse.Style.LoadTagStyle("td", "width", "20px");
            parse.Parse(sr);
            
            //ArrayList parsed = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
            //for (int k = 0; k < parsed.Count; k++)
            //{
            //    doc.Add((IElement)parsed[k]);
            //}
            doc.ClearTextWrap();
            Byte[] buffer = stream.GetBuffer();
            ////create a new action to send the document to our new destination.
            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
            ////set the open action for our writer object
            writer.SetOpenAction(action);
            doc.Close();
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment;filename="+MemoNo);
            Response.AddHeader("content-length",buffer.Length.ToString().Trim());
            Response.BinaryWrite(buffer);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
            Response.End();
            writer.Close();
            sr.Close();

In my table i have table ...in that i have 2 td's to which i have given
HTML
<table>
<tr>
<td width="20%></td><br mode=" hold=" /><td width=80%></td><br mode="></td></tr></table>
Posted
Updated 30-Oct-11 23:08pm
v2
Comments
coolharish123 31-Oct-11 5:28am    
mode and hole are not valid attribute error in design (.aspx) page
Sergey Alexandrovich Kryukov 31-Oct-11 12:43pm    
Read it back: "want to generate pdf that can convert html to pdf". You write that PDF is supposed to convert something to PDF (!!???). You certainly need to re-phrase things... :-)
--SA

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