user1368535
user1368535

Reputation:

Unable to get image in my pdf created using itextsharp

I am not getting image in pdf i only get text, please help me, i'm using iteshsharp 5.2.1 from http://sourceforge.net/projects/itextsharp/

Code of default.aspx.cs

Response.ContentType = "application/pdf";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
savetopdf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Code for default.aspx

<form id="form1" runat="server">
<div id="savetopdf" runat="server">
    <asp:Label ID="lbl" runat="server" Text=" Lorem Ipsum, Dolor"></asp:Label>
    <img src="http://localhost:5583/WebSite1/images/Penguins.jpg" alt="penguins" />
    <img src="http://localhost:5583/WebSite1/images/Tulips.jpg" alt="tulips" />
</div>
</form>

Upvotes: 2

Views: 2225

Answers (2)

Uday
Uday

Reputation: 1

try like this:

string ImagePath = Server.MapPath("/Images/Printbanner1.jpg");
Document document = new Document();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=xxx.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
document.Open();

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);
htmlTable = htmlTable.ToString().Replace("'", "\"");
htmlTable = htmlTable.Replace("px", "");
StringReader sr = new StringReader(htmlTable.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ImagePath);
pdfDoc.Add(image);
htmlparser.Parse(sr);

pdfDoc.Close();

Upvotes: 0

Alexis Pigeon
Alexis Pigeon

Reputation: 7512

HTMLWorker was buggy and has been discontinued in favor of XMLWorker.

http://sourceforge.net/projects/itextsharp/files/xmlworker/

The demo :

http://demo.itextsupport.com/xmlworker/

Even though the documentation refers to the Java API, the adaptation to C# should be straightforward :

http://demo.itextsupport.com/xmlworker/itextdoc/index.html

Upvotes: 1

Related Questions