Foros del Web » Programando para Internet » ASPX (.net) »

Problema Con libreria Itexsharp

Estas en el tema de Problema Con libreria Itexsharp en el foro de ASPX (.net) en Foros del Web. Hola comunidad, tengo un pequeño problema al querer convertir html a PDF, esto lo hago con la libreria de Itexsharp. Mi problema es el siguiente: ...
  #1 (permalink)  
Antiguo 06/10/2011, 18:18
 
Fecha de Ingreso: octubre-2009
Mensajes: 24
Antigüedad: 14 años, 6 meses
Puntos: 0
Busqueda Problema Con libreria Itexsharp

Hola comunidad, tengo un pequeño problema al querer convertir html a PDF, esto lo hago con la libreria de Itexsharp.

Mi problema es el siguiente:

Al momento de asignar el html a convertir a pdf, me marca un error con las rutas de las imagenes. tengo algo como esto:
Código:
 String strSelectUserListBuilder = "";
        //string UrlDirectory = Server.MapPath("images");
        MemoryStream MStream = new MemoryStream();
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);
        try
        {
            PdfWriter writer = PdfWriter.GetInstance(document, MStream);
            document.Open();
            strSelectUserListBuilder = "<table border='0' width='600' cellspacing='0' cellpadding='0'>" +
          "<tr>" +
          "<td>" +
          "<p align='center'><img border='0' src='images/image_x_psd.png' ><br>" +
          "<font face='Arial' size='3' color='#876E3A'><b>Consultants<br></b></font>" +
            "<img border='0' src='images/image_x_psd.png' >" +
          "</td>" +
          "</tr>" +
          " <tr><td>" +
          "</td></tr>" +

          // FOOTER   
          "<tr>" +
          "<td>" +
              "<p align='center'><img border='0' src='images/image_x_psd.png'>" +
          "</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 e)
        {
            throw e;
        }
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=myPDFNew.pdf");
        HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
        HttpContext.Current.Response.End();
El error que me sale es una web exeption que dice asi:
"No se puede encontrar una parte de la ruta de acceso 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\src='images\image_x_psd.png' '."
y la ruta de la imagen no es esa, yo le especifico la del proyecto.

solo con texto me funciona de maravilla, pero el html que quiero exportar lleva imagenes, si me podrian ayudar a solucionar este pequeño inconveniente, se los agradeceria mucho.
  #2 (permalink)  
Antiguo 07/10/2011, 10:44
 
Fecha de Ingreso: octubre-2009
Mensajes: 24
Antigüedad: 14 años, 6 meses
Puntos: 0
Respuesta: Problema Con libreria Itexsharp

Bueno ya resolvi el problema que tenia, por si alguien le interesa.

lo unico que hice es poner antes de la ruta de la imagen este string:

String UrlDirectory = Request.Url.GetLeftPart(UriPartial.Path);
UrlDirectory = UrlDirectory.Substring(0, UrlDirectory.LastIndexOf("/") + 1);
algo como esto
"<img src='"+UrlDirectory +"'images/miimagen.png />;"

y listo me funciono muy bien.

Etiquetas: .net, c#, html, htmltopdf, images, pdf
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:35.