2013-04-18 28 views
2

我想了很多的搜索,我發現很多博客文章後,創建我的asp.net MVC3應用報告談到ITextSharp轉換我Html/RazorPdf我轉換阿拉伯語「統一」內容HTML或XML爲pdf試圖解析Razor視圖獲取PDF作爲如下使用iTextSharp的

public void Render(ViewContext viewContext, TextWriter writer) 
    { 
     var doc = new Document(); 

     // associate output with response stream 
     var pdfWriter = PdfWriter.GetInstance(doc, viewContext.HttpContext.Response.OutputStream); 
     pdfWriter.CloseStream = false; 

     viewContext.HttpContext.Response.ContentType = "application/pdf"; 
     viewContext.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8; 

     // generate view into string 
     var sb = new System.Text.StringBuilder(); 
     TextWriter tw = new System.IO.StringWriter(sb); 
     _result.View.Render(viewContext, tw); 
     var resultCache = sb.ToString(); 

     //Path to our font 
     string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); 
     //Register the font with iTextSharp 
     iTextSharp.text.FontFactory.Register(arialuniTff); 

     //Create a new stylesheet 
     iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet(); 
     //Set the default body font to our registered font's internal name 
     ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS"); 
     //Set the default encoding to support Unicode characters 
     ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H); 


     //Parse our HTML using the stylesheet created above 
     List<IElement> list = HTMLWorker.ParseToList(new StringReader(resultCache), ST); 
     doc.Open(); 
     //Loop through each element, don't bother wrapping in P tags 
     foreach (var element in list) 
     { 
      doc.Add(element); 
     } 

     doc.Close(); 
     pdfWriter.Close(); 
    } 

代碼的結果是 enter image description here

這是不正確的,阿拉伯語單詞應該是「محمد」。所以我需要的是設置文檔的方向是從右到左

編輯 由於@Romulus

我做我剛剛更換添加元素,他的代碼稍微改動PdfPCell到循環在我Html和設置一些屬性

//Loop through each element, don't bother wrapping in P tags 
     foreach (var element in list) 
     { 
      //Create a cell and add text to it 
      //PdfPCell text = new PdfPCell(new Phrase(element.ToString(), f)); 

      //Ensure that wrapping is on, otherwise Right to Left text will not display 
      //text.NoWrap = false; 

      //Add the cell to the table 
      //table.AddCell(text); 
      if (element is iTextSharp.text.pdf.PdfPTable) 
      { 
       table = (iTextSharp.text.pdf.PdfPTable)element; 
       table.DefaultCell.NoWrap = false; 
       table.RunDirection = PdfWriter.RUN_DIRECTION_RTL; 
       foreach (PdfPRow row in table.Rows) 
       { 
        foreach (PdfPCell cell in row.GetCells()) 
        { 
         cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL; 
         cell.NoWrap = false; 
        } 
       } 
      } 

     } 

這是爲我工作現在好了謝謝:)

回答

1

您需要使用容器元素,其蘇pport RunDirection,如ColumnText或PdfPCell然後設置其element.RunDirection = PdfWriter.RUN_DIRECTION_RTL

List<IElement> list = HTMLWorker.ParseToList(new StringReader(resultCache), ST); 
doc.Open(); 

//Use a table so that we can set the text direction 
PdfPTable table = new PdfPTable(1); 
//Ensure that wrapping is on, otherwise Right to Left text will not display 
table.DefaultCell.NoWrap = false; 
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL; 

//Loop through each element, don't bother wrapping in P tags 
foreach (var element in list) 
{ 
    //Create a cell and add text to it 
    PdfPCell text = new PdfPCell(new Phrase(element, font)); 

    //Ensure that wrapping is on, otherwise Right to Left text will not display 
    text.NoWrap = false; 

    //Add the cell to the table 
    table.AddCell(text); 
} 
//Add the table to the document 
document.Add(table); 
doc.Close(); 
pdfWriter.Close(); 

對於另外參考,看看這個sample

+0

感謝@羅穆盧斯工作正常,但我做了一點改變,因爲我的剃鬚刀就像一張桌子我將添加更改作爲對問題的編輯 –

+0

你好穆罕默德,如果它幫助你也可以請你投票,除非你需要還有什麼幫助? –