将阿拉伯数据导出到pdf asp.net显示错误

时间:2014-07-26 14:23:42

标签: c# html asp.net itextsharp itext

当我将ARABIC数据导出到pdf.Microsoft adobereader显示错误时.Adobe reader无法打开文件,因为它不是受支持的文件。我的代码是关注asp.net c#.Guide me

protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Document doc = new Document(PageSize.LETTER);
            doc.Open();

            //Sample HTML
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(@"<p>This is a test: <strong>مسندم</strong></p>");

            //Path to our font
            string arialuniTff = Server.MapPath("~/tradbdo.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, "Traditional Arabic Bold");
            //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(stringBuilder.ToString()), ST);

            //Loop through each element, don't bother wrapping in P tags
            foreach (var element in list)
            {
                doc.Add(element);
            }

            doc.Close();
            Response.Write(doc);
            Response.End();
        }

2 个答案:

答案 0 :(得分:0)

我发现以下文章介绍了如何通过iTextSharp库正确导出和显示阿拉伯语内容:http://geekswithblogs.net/JaydPage/archive/2011/11/02/using-itextsharp-to-correctly-display-hebrew--arabic-text-right.aspx

以下是您可以尝试的代码示例:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;

public void WriteDocument()
{
   //Declare a itextSharp document
    Document document = new Document(PageSize.A4);

    //Create our file stream and bind the writer to the document and the stream
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Test.Pdf", FileMode.Create));

    //Open the document for writing
    document.Open();

   //Add a new page
    document.NewPage();

    //Reference a Unicode font to be sure that the symbols are present.
    BaseFont bfArialUniCode = BaseFont.CreateFont(@"C:\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    //Create a font from the base font
    Font font = new Font(bfArialUniCode, 12);

    //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;

    //Create a regex expression to detect hebrew or arabic code points
    const string regex_match_arabic_hebrew = @"[\u0600-\u06FF,\u0590-\u05FF]+";
    if (Regex.IsMatch("مسندم", regex_match_arabic_hebrew, RegexOptions.IgnoreCase))
    {
        table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
    }

    //Create a cell and add text to it
    PdfPCell text = new PdfPCell(new Phrase("مسندم", 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);

    //Close the document
    document.Close();

    //Launch the document if you have a file association set for PDF's
    Process AcrobatReader = new Process();
    AcrobatReader.StartInfo.FileName = @"C:\Test.Pdf";
    AcrobatReader.Start();

}

答案 1 :(得分:0)

iTextSharp.text.Document是一个用于帮助将ParagraphMargin等人类概念融入PDF概念的类。 网桥部分很重要。它以任何方式 PDF文件,因此永远不应将其视为PDF。这样做就像将System.Drawing.Graphics视为图像一样。这会导致您在第二行到最后一行代码中出现问题之一,即尝试将Document视为PDF,直接将其发送到输出流:

//This won't work
Response.Write(doc);

你会发现许多很多教程都是这样做的,但他们都错了。幸运的是(或者不幸的是),PDF是宽容的,并且最终允许垃圾数据,因此只有少数PDF失败,人们认为还有其他一些问题。

您的另一个问题是您错过了PdfWriter。如果Document是桥梁,PdfWriter是将PDF放在一起的实际建筑工人。但是,它也不是PDF。相反,它需要绑定到文件,内存或HttpResponse.OutputStream等流。

下面是一些显示此功能的代码。我非常强烈建议将PDF逻辑与ASPX逻辑分开。你们所有人都是PDF首先得到一个代表PDF的实际“东西”,然后用它做点什么。

开始时我们声明一个我们稍后会填写的字节数组。接下来,我们创建一个System.IO.MemoryStream,用于将PDF写入。创建Document之后,我们创建一个绑定到PdfWriter和我们的流的Document。你的内部代码是相同的,虽然我没有测试它看起来是正确的。在我们完成MemoryStream之前,我们将活动字节抓取到字节数组中。最后,我们使用BinaryWrite()方法将原始二进制PDF发送给请求客户端。

//At the end of this bytes will hold a byte array representing an actual PDF file
Byte[] bytes;

//Create a simple in-memory stream
using (var ms = new MemoryStream()){
    using (var doc = new Document()) {

        //Create a new PdfWriter bound to our document and the stream
        using (var writer = PdfWriter.GetInstance(doc, ms)) {
            doc.Open();

            //This is unchanged from the OP's code

            //Sample HTML
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(@"<p>This is a test: <strong>مسندم</strong></p>");

            //Path to our font
            string arialuniTff = Server.MapPath("~/tradbdo.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, "Traditional Arabic Bold");
            //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(stringBuilder.ToString()), ST);

            //Loop through each element, don't bother wrapping in P tags
            foreach (var element in list) {
                doc.Add(element);
            }

            doc.Close();
        }
    }

    //Right before closing the MemoryStream grab all of the active bytes
    bytes = ms.ToArray();
}

//We now have a valid PDF and can do whatever we want with it

//In this case, use BinaryWrite to send it directly to the requesting client
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.BinaryWrite(bytes);
Response.End();