Click here to Skip to main content
15,395,181 members
Please Sign up or sign in to vote.
2.67/5 (6 votes)
See more:
I have a code bellow:
the first i using itexsharp library:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

the second i have code c#:

public partial class _Default : System.Web.UI.Page

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)


    protected void Button1_Click(object sender, System.EventArgs e)
        String htmlText = "<font  " +
         " color=\"#0000FF\"><b><i>Title One</i></b></font><font   " +
         " color=\"black\"><br><br>Some text here<br><br><br><font   " +
         " color=\"#0000FF\"><b><i>Another title here   " +
         " </i></b></font><font   " +
         " color=\"black\"><br><br>Text1<br>Text2<br><OL><LI><DIV Style='color:green'>Pham Duy Hoa</DIV></LI><LI>how are u</LI></OL><br/>"+
         "<table border='1'><tr><td style='color:red;text-align:right;width:20%'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>ASFAFA</td></tr><tr><td style='color:red;text-align:right'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>DAFSDGAFW</td></tr></table><br/>"+

        HTMLToPdf(htmlText, "PDFfile.pdf");

    public void HTMLToPdf(string HTML, string FilePath)
        Document document = new Document();

        PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Chap0101.pdf", FileMode.Create));
        Image pdfImage = Image.GetInstance(Server.MapPath("logo.png"));

        pdfImage.ScaleToFit(100, 50);

        pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);

        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        hw.Parse(new StringReader(HTML));
    private void ShowPdf(string s)
        Response.AddHeader("Content-Disposition", "inline;filename=" + s);
        Response.ContentType = "application/pdf";

but i don't insert image into string htmltext.
please help me.
Updated 22-Jun-21 21:42pm
Sunasara Imdadhusen 30-May-11 1:56am
Added PRE tag.
Mausam Bharati 9-Jul-13 7:48am
What will be Chap0101.pdf in case of table from database generated through a dll?

1 solution


 Dim jpeg As iTextSharp.text.Image
 Dim _Chunk As Chunk
jpeg = iTextSharp.text.Image.GetInstance(imagepath)
_Chunk = New Chunk(jpeg, 0, 0)

This will works!!!

hoa20101988 30-May-11 21:37pm
Thanks, but i want to insert image in HTml of code c# and change size image.
sharad_sharma82 19-Jan-12 2:25am
Hi if you can to add the image file from code file the use the below code and it will work fine for me :
1.apply the [img] that refer image tag in desgin part where you want to show the image
<td valign="top" style="text-align: right">
2.replace this [img] with image tag
string imgpath = "<img src='' style='width:50;height:50;padding-left-100px;' runat="server" id='ig' alt='' />";
contents = contents.Replace("[img]", imgpath .ToString());
//contents = contents.Replace("[ORDERDATE]", DateTime.Now.ToShortDateString());
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

// Enumerate the elements, adding each one to the Document...
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
mayur mkt 27-Sep-12 6:09am
can any one of you tell to insert color or how can i get css effects which i have applied in my page..i am using code in
Poojit 24-Apr-14 3:26am
Hi All,

I am encountring same issue with image.
Please help.

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