在网格单元格内生成带有阿拉伯文文本的 PDF

Generate PDF with arabic text inside grid cell

我需要生成一个包含 table 的 PDF,其中包含 xamarin.forms 中英语和阿拉伯语的数据。我一直在寻找很多。我找到了 syncfusion 文档并尝试应用它。这个不包括网格,但它告诉我们如何添加一个简单的阿拉伯语文本:https://blog.syncfusion.com/blogs/post/adding-rtl-support-to-your-pdf-in-xamarin.aspx 这个有效。所以我尝试使用它并进行修改以添加网格。这是我在 main.xaml.cs:

中写的
 private void btn_Clicked(object sender, EventArgs e)
        {
            PdfDocument doc = new PdfDocument();
            //Add a page.
            PdfPage page = doc.Pages.Add();
            //Create a PdfGrid.
           Syncfusion.Pdf.Grid.PdfGrid pdfGrid = new Syncfusion.Pdf.Grid.PdfGrid();
            //Add values to list
            List<object> data = new List<object>();
            Object row1 = new { ID = "E01", Name = "رنا" };
            Object row2 = new { ID = "E02", Name = "رامي" };
            Object row3 = new { ID = "E03", Name = "Andrew" };
            Object row4 = new { ID = "E04", Name = "Paul" };
            Object row5 = new { ID = "E05", Name = "Gray" };
            data.Add(row1);
            data.Add(row2);
            data.Add(row3);
            data.Add(row4);
            data.Add(row5);
            //Add list to IEnumerable
            IEnumerable<object> dataTable = data;
            //Assign data source.
            pdfGrid.DataSource = dataTable;
            //Draw grid to the page of PDF document.
            pdfGrid.Draw(page, new PointF(10, 10));
            //Save the PDF document to stream.
            MemoryStream ms = new MemoryStream();
            
            //Create a new PDF document.
           

            //Add a new PDF page.
           

            //Load font.
            Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("RTLDemo.Assets.arial.ttf");

            //Create PDF true type font.
            PdfFont pdfFont = new PdfTrueTypeFont(fontStream, 12);

            //String format 
            PdfStringFormat format = new PdfStringFormat();

            //Set the format as right to left.
            format.TextDirection = PdfTextDirection.RightToLeft;

            //Set the alignment.
            format.Alignment = PdfTextAlignment.Right;

            SizeF pageSize = page.GetClientSize();
            PdfGridCell pdfGridCell = new PdfGridCell();
            PdfGridCellStyle style = new PdfGridCellStyle();
            style.Font = pdfFont;
        
            style.StringFormat.TextDirection = format.TextDirection;
            //Set style to grid  
            pdfGridCell.Style = style;

            //Save the document.
            doc.Save(ms);

            //Close the document 
            doc.Close(true);

            ms.Position = 0;

            if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
                Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("RTLText.pdf", "application/pdf", ms);
            else
                Xamarin.Forms.DependencyService.Get<ISave>().Save("RTLText.pdf", "application/pdf", ms);

        }

我不断收到异常:System.NullReferenceException:** 'Object reference not set to an instance of an object.'style.StringFormat.TextDirection = format.TextDirection; 行。我遵循了本文档中的步骤:https://www.syncfusion.com/forums/135954/how-to-create-unicode-font-with-pdftruetypefont-or-the-proper-way-to-draw-a-unicode-string 我尝试了很多方法,但没有任何效果。我该怎么办?

更新:

我用新代码替换了上面的代码:

 PdfDocument doc = new PdfDocument();
            //Add a page.
            PdfPage page = doc.Pages.Add();
            //Create a PdfGrid.
            PdfGrid pdfGrid = new PdfGrid();
            //Create a DataTable.
            DataTable dataTable = new DataTable();
            //Add columns to the DataTable
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Name");
            //Add rows to the DataTable.
            dataTable.Rows.Add(new object[] { "E01", "رنا" });
            dataTable.Rows.Add(new object[] { "E02", "Thomas" });
            //Assign data source.
            pdfGrid.DataSource = dataTable;
            //Using the Column collection
            pdfGrid.Columns[0].Width = 100;
            //Adding grid cell style
            PdfGridCellStyle cellStyle = new PdfGridCellStyle();
            //Create new PDF string format instance.
            PdfStringFormat format = new PdfStringFormat();
            format.Alignment = PdfTextAlignment.Center;
            format.TextDirection = PdfTextDirection.RightToLeft;
            //Set string format to grid cell.
            cellStyle.StringFormat = format;
            //Set borders.
            PdfBorders borders = new PdfBorders();
            borders.All = PdfPens.Red;
            cellStyle.Borders = borders;
            //Set background image.

            Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("RTLDemo.Assets.arial.ttf");

            PdfFont pdfFont = new PdfTrueTypeFont(fontStream, 12);
            cellStyle.Font = pdfFont;
            //Set cell paddings.
            cellStyle.CellPadding = new PdfPaddings(5, 5, 5, 5);
            //Applying style to grid
            pdfGrid.Rows[0].Cells[0].Style = cellStyle;
            //Draw grid to the page of PDF document.
            pdfGrid.Draw(page, new PointF(10, 10));
            MemoryStream ms = new MemoryStream();
            //Save the document.
            doc.Save(ms);

            //Close the document 
            doc.Close(true);
            ms.Position = 0;

            if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
                Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("RTLText.pdf", "application/pdf", ms);
            else
                Xamarin.Forms.DependencyService.Get<ISave>().Save("RTLText.pdf", "application/pdf", ms);

之前的异常没有出现,pdf生成了但是阿拉伯文字没有出现,我错过了什么?

这直接取自documentation

//Adding grid cell style
PdfGridCellStyle cellStyle = new PdfGridCellStyle();
//Create new PDF string format instance.
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
//Set string format to grid cell.
cellStyle.StringFormat = format;

我们可以在 Xamarin.Forms 应用程序中使用 Syncfusion PDF 库在 PDF 网格中绘制 RTL 文本。请在下面找到示例代码,

//Create a new PDF document.
PdfDocument document = new PdfDocument();

//Add a new PDF page.
PdfPage page = document.Pages.Add();

//Get the font file as stream.
Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("UnicodeText.Assets.arial.ttf");

//Create a new PdfTrueTypeFont instance.
PdfTrueTypeFont font = new PdfTrueTypeFont(fontStream, 14);

//Create a new bold stylePdfTrueTypeFont instance.
PdfTrueTypeFont boldFont = new PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Bold);

page.Graphics.DrawString("PdfGrid", boldFont, PdfBrushes.Black, PointF.Empty);

//Create PdfGrid.
PdfGrid pdfGrid = new PdfGrid();

//Add values to list
List<object> data = new List<object>();
data.Add(new { ID = "E01", Name = "رنا" });
data.Add(new { ID = "E02", Name = "رامي" });
data.Add(new { ID = "E03", Name = "Andrew" });
data.Add(new { ID = "E04", Name = "Paul" });
data.Add(new { ID = "E05", Name = "Clay" });

//Add list to IEnumerable.
IEnumerable<object> dataTable = data;

//Assign data source.
pdfGrid.DataSource = dataTable;        

//Assign bold font to pdfGrid header.
pdfGrid.Headers[0].Style.Font = boldFont;

//Assign font to PdfGrid.
pdfGrid.Style.Font = font;

//Create String format with RTL text direction and center text alignment.
PdfStringFormat format = new PdfStringFormat();
format.TextDirection = PdfTextDirection.RightToLeft;
format.Alignment = PdfTextAlignment.Center;

//Assign string format to draw RTL text with center alsignment
pdfGrid.Rows[0].Cells[1].StringFormat = format;
pdfGrid.Rows[1].Cells[1].StringFormat = format;

//Draw grid to the page of PDF document.
pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(0, 20));

MemoryStream stream = new MemoryStream();

//Save the document.
document.Save(stream);

//Close the document.
document.Close(true);

请从 https://www.syncfusion.com/downloads/support/directtrac/general/ze/UnicodeText1046384115. Please find the sample output PDF created from https://www.syncfusion.com/downloads/support/directtrac/general/ze/Output-609045962 中查找示例。

注意:示例中分配的字符串格式不适用于具有阿拉伯文本的相应 PDF 网格单元格。我们必须根据提供的示例代码将单元格索引提供为 1。