c# - Convert arabic"unicode" content html or xml to pdf using itextsharp -


i trying create reports in asp.net mvc3 application after lot of search found many blog posts talks itextsharp convert html/razor pdf trying parse razor view pdf follows

 public void render(viewcontext viewcontext, textwriter writer)     {         var doc = new document();          // associate output 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 string         var sb = new system.text.stringbuilder();         textwriter tw = new system.io.stringwriter(sb);         _result.view.render(viewcontext, tw);         var resultcache = sb.tostring();          //path our font         string arialunitff = path.combine(environment.getfolderpath(environment.specialfolder.fonts), "arialuni.ttf");         //register font itextsharp         itextsharp.text.fontfactory.register(arialunitff);          //create new stylesheet         itextsharp.text.html.simpleparser.stylesheet st = new itextsharp.text.html.simpleparser.stylesheet();         //set default body font our registered font's internal name         st.loadtagstyle(htmltags.body, htmltags.face, "arial unicode ms");         //set default encoding support unicode characters         st.loadtagstyle(htmltags.body, htmltags.encoding, basefont.identity_h);           //parse our html using 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();     } 

the result of code enter image description here

which not correct, arabic word should "محمد". need set document direction right left

edit @romulus

i made little changes code replaced adding element pdfpcell looping on html , set attributes

 //loop through each element, don't bother wrapping in p tags         foreach (var element in list)         {             //create cell , add text             //pdfpcell text = new pdfpcell(new phrase(element.tostring(), f));              //ensure wrapping on, otherwise right left text not display             //text.nowrap = false;              //add cell table             //table.addcell(text);             if (element 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;                     }                 }             }          } 

that's working me :)

you need use container elements support rundirection, such columntext or pdfpcell , set element.rundirection = pdfwriter.run_direction_rtl

list<ielement> list = htmlworker.parsetolist(new stringreader(resultcache), st); doc.open();  //use table can set text direction pdfptable table = new pdfptable(1); //ensure wrapping on, otherwise right left text 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 cell , add text     pdfpcell text = new pdfpcell(new phrase(element, font));      //ensure wrapping on, otherwise right left text not display     text.nowrap = false;      //add cell table     table.addcell(text); } //add table document document.add(table); doc.close(); pdfwriter.close(); 

for addition reference, have @ sample.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -