Answer 18
Hi
the problem is that itextsharp wants to put a image to the pdf with iTextSharp.text.Image and writing only short path in image tag confuses the parser who dont know where the full path is.
i have done this with ordinary pdfs and when you input an image you use this-->
iTextSharp.text.Image.GetInstance(Server.MapPath("/") + "/images/Mypic.jpg"); as an example.
But when you have a html "text" it wants the actual url - (for a reason i dont really know)
so to solve your problem parsing it on the webserver you can add this before your html-string
String UrlDirectory = Request.Url.GetLeftPart(UriPartial.Path);
UrlDirectory = UrlDirectory.Substring(0, UrlDirectory.LastIndexOf("/")+1);
that give you the url (http://yoursite/subcat/)
to this you just add the imagetab -->
<img border='0' src='" + UrlDirectory + "/images/ResumeBottomBorderBrown.jpg' width='600' height='10'>
so it can find the picture
so the solution would be like this -->(all code)
i have tried it with your code and it works on VS 2008 and itextsharp 5.0.2
hope this will help you out
good luck!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Collections;
using iTextSharp.text.xml;
using iTextSharp.text.html;
public partial class Default3 : System.Web.UI.Page
{
String strSelectUserListBuilder = "";
protected void Page_Load(object sender, EventArgs e)
{
//create document
//Response.Write(Server.MapPath("."));
Document document = new Document();
try
{
String UrlDirectory = Request.Url.GetLeftPart(UriPartial.Path);
UrlDirectory = UrlDirectory.Substring(0, UrlDirectory.LastIndexOf("/")+1);
Response.Write(UrlDirectory);
//writer - have our own path!!!
PdfWriter.GetInstance(document, new FileStream(Server.MapPath(".") + @"HTML-to-PDF.pdf", FileMode.Create));
document.Open();
strSelectUserListBuilder = "<table border='0' width='600' cellspacing='0' cellpadding='0'>" +
"<tr>" +
"<td>" +
"<p align='center'><img border='0' src='" + UrlDirectory + "/images/ResumeBottomBorderBrown.jpg' width='600' height='10'><br>" +
"<font face='Arial' size='3' color='#876E3A'><b>Consultants<br></b></font>" +
"<img border='0' src='" + UrlDirectory + "images/ResumeBottomBorderBrown.jpg' width='600' height='10'>" +
"</td>" +
"</tr>" +
" <tr><td>" +
"</td></tr>" +
// FOOTER
"<tr>" +
"<td>" +
"<p align='center'><img border='0' src='" + UrlDirectory + "images/ResumeBottomBorderBrown.jpg' width='600' height='10'>" +
"</td>" +
"</tr>" +
"</table>" +
"<br><br><br>";
//Here is where your HTML source goes................
String htmlText = strSelectUserListBuilder.ToString();
//make an arraylist ....with STRINGREADER since its no IO reading file...
List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
IElement x = (IElement)htmlarraylist[k];
Response.Write(x.Type.ToString() + "#<br>");
// document.Add((IElement)htmlarraylist[k]);
}
//document.Add(new Paragraph("And the same with indentation...."));
// or add the collection to an paragraph
// if you add it to an existing non emtpy paragraph it will insert it from
//the point youwrite -
Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder"
mypara.IndentationLeft = 36;
mypara.InsertRange(0, htmlarraylist);
document.Add(mypara);
document.Close();
}
catch (Exception exx)
{
Response.Write("<br>____________________________________<br>");
Response.Write("<br>Error: " + exx + "<br>");
Response.Write("<br>StackTrace: " + exx.StackTrace + "<br>");
Response.Write("<br>strSelectUserListBuilder: " + strSelectUserListBuilder.ToString() + "<br>");
//Console.Error.WriteLine(exx.StackTrace);
//Console.Error.WriteLine(exx.StackTrace);
}
finally
{
//document.Close();
}
}
}