Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
protected void ConvertHTMLToPDF(string HTMLCode)
   {
       HttpContext context = HttpContext.Current;

       //Render PlaceHolder to temporary stream
       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

C#
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);
       }

   }
Posted
Updated 21-Jan-16 9:49am

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