转换阿拉伯语“unicode"使用 itextsharp 将 html 或 xml 内容转换为 pdf [英] Convert arabic"unicode" content html or xml to pdf using itextsharp

查看:31
本文介绍了转换阿拉伯语“unicode"使用 itextsharp 将 html 或 xml 内容转换为 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量搜索,我试图在我的 asp.net MVC3 应用程序中创建报告我发现很多博客文章都在谈论 ITextSharp 将我的 Html/Razor 转换为Pdf 我正在尝试解析剃刀视图以获得 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();
    }

该代码的结果是

这是不正确的,阿拉伯语单词应该是محمد".所以我需要的是将文档方向设置为从右到左

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

我对他的代码做了一些改动,我只是将向 PdfPCell 添加元素替换为在我的 Html 上循环并设置一些属性

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

作为补充参考,看看这个示例.

For addition reference, have a look at this sample.

这篇关于转换阿拉伯语“unicode"使用 itextsharp 将 html 或 xml 内容转换为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆