2011-02-08 84 views
5

我使用ITextSharp庫將html轉換爲pdf。 我的用戶在她/他的html文件中使用了波斯語的句子,所以這個庫不能轉換波斯語。通過ITextSharp創建PDF文件的波斯語html文件

爲了解決這個和從右到左的問題,我用波紋管代碼:

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

我的左和轉換波斯的話語權得到了解決,但還有一個問題。

我的算法無法解析和轉換html文件中使用的表標籤的內容。

現在的問題是:如何解析具有表格標籤,div和段落標記與波斯語的句子,並將其轉換爲PDF格式的HTML文件?

+0

「我的算法不能分析和轉換,在HTML文件中使用table標籤的內容「。 - 是否意味着對象不包含原始HTML文檔的表格或什麼? – Roman 2011-02-26 10:34:13

+1

Hello Kia _Salam aziz;)_看到這個鏈接http://hasheminezhad.com/itextsharp – Shahin 2011-03-06 22:04:05

回答

1

嘗試使用此 http://code.google.com/p/wkhtmltopdf/

該應用程序在一個HTML頁面讀取,並將其保存爲PDF。只需使用shell腳本在C#中運行該功能即可。

+0

謝謝你的迴應。我明白了,但我想使用iTextSharp庫。你有使用iTextSharp庫的解決方案嗎? – 2011-04-23 09:55:07

3

iTextSharp也可以解析表格標籤。但它不設置其屬性的RTL,你需要自己解決它:

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

更多信息:(^

相關問題