2011-02-08 23 views
5

Utilizo la biblioteca ITextSharp para convertir html a pdf. Mis usuarios usan una oración en persiana en sus archivos html, por lo que esta biblioteca no puede convertir palabra persa.Crear pdf desde un archivo html persa por ITextSharp

Para resolver este problema y de derecha a izquierda i use el código de abajo:

 Document document = new Document(PageSize.A4, 80, 50, 30, 65); 
     PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create)); 
     document.Open(); 

     ArrayList objects; 
     document.NewPage(); 

     var stream = new StreamReader(strHTMLpath, Encoding.Default).ReadToEnd(); 
     objects = iTextSharp.text.html.simpleparser. 
     HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.UTF8), styles);    

     BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\Tahoma.ttf", 
             BaseFont.IDENTITY_H, true); 
     for (int k = 0; k < objects.Count; k++) 
     { 
      PdfPTable table = new PdfPTable(1); 
      table.RunDirection = PdfWriter.RUN_DIRECTION_RTL; 

      var els = (IElement)objects[k]; 
      foreach (Chunk el in els.Chunks) 
      { 
       #region set persian font 
       iTextSharp.text.Font f2 = new iTextSharp.text.Font(bf, el.Font.Size, 
               el.Font.Style, el.Font.Color); 
       el.Font = f2; 
       #endregion set persian font 

       #region Set right to left for persian words 
       PdfPCell cell = new PdfPCell(new Phrase(10, el.Content, el.Font)); 
       cell.BorderWidth = 0; 
       table.AddCell(cell); 
       #endregion Set right to left for persian words 
      } 
      //document.Add((IElement)objects[k]);     
      document.Add(table); 
     } 

     document.Close(); 
     Response.Write(strPDFpath); 
     Response.ClearContent(); 
     Response.ClearHeaders(); 
     Response.AddHeader("Content-Disposition", "attachment; filename=" + strPDFpath); 
     Response.ContentType = "application/octet-stream"; 
     Response.WriteFile(strPDFpath); 
     Response.Flush(); 
     Response.Close(); 
     if (File.Exists(strPDFpath)) 
     { 
      File.Delete(strPDFpath); 
     } 

Mi derecha a izquierda y convertir palabras persas se resolvió, pero tiene otro problema.

Mi algoritmo no puede analizar y convertir el contenido de la etiqueta de tabla que se utiliza en el archivo html.

Ahora la pregunta es: ¿Cómo analizar el archivo html que tiene la etiqueta de tabla, div y la etiqueta de párrafo con la oración en persa y convertirlo a PDF?

+0

"Mi algoritmo no puede analizar y convertir el contenido de la etiqueta de la tabla que se utiliza en el archivo html " - Significa que los objetos no contienen tablas de documentos html originales o qué? – Roman

+1

Hola Kia _Salam aziz;) _ ver este enlace http://hasheminezhad.com/itextsharp – Shahin

Respuesta

1

Trate de usar este http://code.google.com/p/wkhtmltopdf/

Esa aplicación lee en una página HTML y la guarda como un archivo PDF. Simplemente ejecute esa cosa en C# usando script de shell.

+0

Gracias por su respuesta. Veo eso, pero quiero usar la biblioteca iTextSharp. ¿Tienes una solución usando la biblioteca iTextSharp? –

+0

El enlace está roto .. = ( – oCcSking

3

iTextSharp también puede analizar las etiquetas de la tabla. pero no establece sus propiedades RTL y hay que repararlo usted mismo:

  foreach (var htmlElement in parsedHtmlElements) 
      { 
       fixRunDirection(htmlElement); 
       pdfCell.AddElement(htmlElement); 
      } 

... 

     private static void fixRunDirection(IElement htmlElement) 
     { 
      if (!(htmlElement is PdfPTable)) return; 

      var table = (PdfPTable)htmlElement; 
      table.RunDirection = PdfWriter.RUN_DIRECTION_RTL; 

      foreach (var row in table.Rows) 
      { 
       foreach (var cell in row.GetCells()) 
       { 
        cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL; 
        foreach (var element in cell.CompositeElements) 
        { 
         fixRunDirection(element); 
        } 
       } 
      } 
     } 

Más información: (^)

+0

más información aquí: http://www.dotnettips.info/post/1464 – VahidN

Cuestiones relacionadas