I am trying to create reports in my ASP.NET MVC3 application after a lot of searching, I found a lot of ITextSharp blog ITextSharp to convert my Html/Razor to Pdf . I am trying to make out a razor to get PDf as follows
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 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(); }
result of this code 
which is wrong, the Arabic word should be "Ω
ΨΩ
Ψ―". so I need to set the document direction from right to left
EDIT Thanks @Romulus
I made small changes to my code, I just replaced the added item on PdfPCell with a loop on my Html and set some attributes
It works fine for me now. Thanks :)
c # asp.net-mvc-3 itextsharp
Mohamed Salah Apr 18 '13 at 10:40 2013-04-18 10:40
source share