阿拉伯语将数据导出为pdf

时间:2016-06-14 14:22:30

标签: c# asp.net pdf itextsharp arabic

这是我的代码

Cell cellColss = new Cell();    
BaseFont bfArialUniCode = BaseFont.CreateFont(@"D:\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bfArialUniCode, 12);
Chunk chunkColss = new Chunk("مسلسل",font);
cellColss.Add(chunkColss);
pdfTable.AddCell(cellColss);

导出,数据如下所示: formatted data

它似乎是“مسلسل”,因为“لسللسم”

我添加此代码后

PdfPCell pdfCell = new PdfPCell(new Phrase("مسلسل", font));
pdfCell.RunDirection=PdfWriter.RUN_DIRECTION_RTL;

而不是

Chunk chunkColss = new Chunk("مسلسل",font);`

什么都没有

注意: - 我安装了最新版本的iTextShare

这是完整代码

  Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
            int cols = dataTable.Columns.Count;
            int rows = dataTable.Rows.Count;
            pdfDoc.Open();

            iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(3, rows);
            pdfTable.BorderWidth = 1;
            pdfTable.Width = 100;
            pdfTable.Padding = 1;
            pdfTable.Spacing = 1;
            BaseFont bfArialUniCode = BaseFont.CreateFont(@"D:\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bfArialUniCode, 12);           
            for (int i = 0; i < cols; i++)
            {
                if (i == 0)
                {
                    Cell cellColss = new Cell();               
                    Chunk chunkColss = new Chunk("مسلسل", font);
                    string ss = "مسلسل";
                    string bytes = Encoding.UTF8.GetEncoder().ToString();
                    PdfPCell pdfCell = new PdfPCell(new Phrase(chunkColss));
                    pdfCell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                   cellColss.Add(pdfCell);
                    pdfTable.AddCell(cellColss);
                }

                if (dataTable.Columns[i].ColumnName == "Barcode")
                {
                    Cell cellCols = new Cell();
                    Chunk chunkCols = new Chunk("باركود", font);
                    cellCols.Add(chunkCols);
                    pdfTable.AddCell(cellCols);
                }
                else if (dataTable.Columns[i].ColumnName == "Description")
                {
                    Cell cellCols = new Cell();
                    Chunk chunkCols = new Chunk("اسم الصنف بالعربى والانجليزى", font);
                    cellCols.Add(chunkCols);
                    pdfTable.AddCell(cellCols);
                }

            }
           for (int k = 0; k < rows; k++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (j == 0)
                    {
                        Cell cellRows1 = new Cell();                       
                        Chunk chunkRows2 = new Chunk((k + 1).ToString(), font);
                        cellRows1.Add(chunkRows2);
                        pdfTable.AddCell(cellRows1);
                    }

                    if (dataTable.Columns[j].ColumnName == "Barcode") 
                    {
                        Cell cellRows = new Cell();                      
                        Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), font);
                        cellRows.Add(chunkRows);
                        pdfTable.AddCell(cellRows);
                    }
                    else if (dataTable.Columns[j].ColumnName == "Description")
                    {
                        Cell cellRows = new Cell();                       
                        Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), font);
                        cellRows.Add(chunkRows);
                        pdfTable.AddCell(cellRows);

                    }
                }
            }

            pdfDoc.Add(pdfTable);
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
            Response.Clear();
            Response.BinaryWrite(mStream.ToArray());
            Response.End();

1 个答案:

答案 0 :(得分:1)

您可以使用:

PdfPCell pdfCell = new PdfPCell(new Phrase("مسلسل", font));  
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); 

以下链接中的类似示例: Brandon Anzaldi