I have tryede to change a virtuel path toek editor to the physical path from my CkEditor filebrowser. But i can only get tekst to work, and not the image. When i pick and Image i get error
System.IO.DirectoryNotFoundException: a part of the path 'C:\Users\Martin-PC\Admin\OpretPdf\Media\Chrysanthemum.jpg' was not found.
And i know its because its expeting a virtuel path.
Its my first time i worked with Itext and im pretty lost how i get it to work.
Here is my convert html to pdf methode
protected void ConvertHTMLToPDF(string HTMLCode)
{
HttpContext context = HttpContext.Current;
System.IO.StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
StringReader reader = new StringReader(HTMLCode);
Document doc = new Document(PageSize.A4);
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
var interfaceProps = new Dictionary<string, Object>();
var ih = new ImageHander() { BaseUri = Request.Url.ToString() };
interfaceProps.Add(HTMLWorker.IMG_PROVIDER, ih);
foreach (IElement element in HTMLWorker.ParseToList(
new StringReader(HTMLCode.ToString()), null, interfaceProps))
{
doc.Add(element);
}
try
{
parser.Parse(reader);
}
catch (Exception ex)
{
Paragraph paragraph = new Paragraph("Error!" + ex.Message);
Chunk text = paragraph.Chunks[0] as Chunk;
if (text != null)
{
text.Font.Color = BaseColor.RED;
}
doc.Add(paragraph);
}
finally
{
doc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" + "filename=sample.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(doc);
Response.End();
}
}
And my image handler
public class ImageHander : IImageProvider
{
public string BaseUri;
public iTextSharp.text.Image GetImage(string src,
IDictionary<string, string> h,
ChainedProperties cprops,
IDocListener doc)
{
string imgPath = HttpContext.Current.Server.MapPath(src);
if (src.ToLower().Contains("http://") != false)
{
imgPath = HttpContext.Current.Request.Url.Scheme + "://" +
HttpContext.Current.Request.Url.Authority + src;
}
else
{
imgPath = HttpContext.Current.Server.MapPath("Media/Chrysanthemum.jpg");
}
return iTextSharp.text.Image.GetInstance(imgPath);
}
}