使用itext将阿拉伯数据导出为pdf

时间:2014-11-18 18:19:29

标签: pdf gridview itextsharp

我想使用iTextsharp将gridview数据导出为PDF。 gridview数据包含波斯/阿拉伯数据。它导致只有一个空白的PDF文件......可能是什么原因?

以下是我的代码:

    GridView1.DataSource = Session("dt")
    GridView1.AllowPaging = False
    GridView1.DataBind()

    Dim bf As BaseFont = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") & "\fonts\tahoma.ttf", BaseFont.IDENTITY_H, True)
    Dim font As New iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL)
    Dim table As New iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count)
    Dim widths As Integer() = New Integer(GridView1.Columns.Count - 1) {}
    For x As Integer = 0 To GridView1.Columns.Count - 1
        widths(x) = CInt(GridView1.Columns(x).ItemStyle.Width.Value)
        Dim cellText As String = Server.HtmlDecode(GridView1.HeaderRow.Cells(x).Text)
        Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))
        cell.BackgroundColor = New BaseColor(GridView1.HeaderStyle.BackColor)
        'cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#008000"))
        cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
        table.AddCell(cell)
    Next
    table.SetWidths(widths)

    For i As Integer = 0 To GridView1.Rows.Count - 1
        If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
            For j As Integer = 0 To GridView1.Columns.Count - 1
                Dim cellText As String = Server.HtmlDecode(GridView1.Rows(i).Cells(j).Text)
                Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))
                'Set Color of Alternating row
                If i Mod 2 <> 0 Then
                    'cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#C2D69B"))
                    cell.BackgroundColor = New BaseColor(GridView1.RowStyle.BackColor)
                End If
                cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
                table.AddCell(cell)
            Next
        End If
    Next

    'Create the PDF Document
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    pdfDoc.Add(table)
    pdfDoc.Close()
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.Write(pdfDoc)
    Response.[End]()

1 个答案:

答案 0 :(得分:1)

您的问题有些误导,因为您遇到的问题并非由您使用阿拉伯语引起的。如果要将文档写入文件,您会注意到文件正常。

这些行中存在严重错误:

PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
...
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.[End]()

首先使用Response.OutputStream向其中写入PDF字节,然后使用Response对象设置内容标题。这是非法语法:您应始终在向输出流发送任何字节之前定义标题

更多:您应该将PDF字节写入输出流,而不是Document对象。以下行完全没有意义:

Response.Write(pdfDoc)

有关如何将content个字节发送到Response.OutputStream的示例,请阅读以下问题的答案:iTextSharp generated PDF: How to send the pdf to the client and add a prompt?

如果您不知道如何创建content字节,请查看以下代码段:

using (MemoryStream myMemoryStream = new MemoryStream()) {    
    Document document = new Document();    
    PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);
    document.Open();
    ...
    document.Close();
    byte[] content = myMemoryStream.ToArray();
    ...
}

永远不要将document等对象写入输出流,始终使用byte[]