阿拉伯转换" UNI code"内容HTML或使用iTextSharp的XML为pdf [英] Convert arabic"unicode" content html or xml to pdf using itextsharp
问题描述
我想很多搜索后创造我的asp.net MVC3申请报告我发现很多博客帖子大约 iTextSharp的
会谈转换我的 HTML /剃须刀
到全文
我试图解析剃刀以下载pdf如下:
I am trying to create reports in my asp.net MVC3 application after a lot of search I found many blog posts talks about ITextSharp
to convert my Html/Razor
to Pdf
I am trying to parse razor view 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'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();
}
这code的结果是
the result of that code is
这是不正确的,阿拉伯语单词应该是محمد。所以我需要的是设置文档的方向是从右至左
which is not correct, the arabic word should be "محمد". so what I need is to set document direction to be from right to left
修改
由于@Romulus
EDIT Thanks to @Romulus
我做我刚刚更换添加元素 PdfPCell
来循环上我的HTML他的code一点点的变化,并设置一些属性。
I made a little changes to his code i just replaced adding element to PdfPCell
to looping on my Html and set some attributes
//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;
}
}
}
}
这是为我工作现在好了感谢:)
That's working for me now well Thanks :)
推荐答案
您需要使用支持RunDirection容器元素,如ColumnText或PdfPCell,然后设置其element.RunDirection = PdfWriter.RUN_DIRECTION_RTL
You need to use container elements which support RunDirection, such as ColumnText or PdfPCell and then set their 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();
有关除了参考,看看这个<一个href=\"http://geekswithblogs.net/JaydPage/archive/2011/11/02/using-itextsharp-to-correctly-display-hebrew--arabic-text-right.aspx\"相对=nofollow>样本。
For addition reference, have a look at this sample.
这篇关于阿拉伯转换&QUOT; UNI code&QUOT;内容HTML或使用iTextSharp的XML为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!