Home > OS >  Generate PDF with arabic text inside grid cell
Generate PDF with arabic text inside grid cell

Time:10-20

I need to generate a PDF that contains a table with data in both English and arabic languages in xamarin.forms. I've been searching a lot. I found the syncfusion documentation and tried to apply it. This one doesn't include a grid but it tells how to add a simple arabic text: https://blog.syncfusion.com/blogs/post/adding-rtl-support-to-your-pdf-in-xamarin.aspx this one worked. So I tried to use it and make modifications to add a grid. This is what I wrote in the 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);

        }

I keep getting an exception: System.NullReferenceException:** 'Object reference not set to an instance of an object.' on the style.StringFormat.TextDirection = format.TextDirection; line. I followed the steps in this document: https://www.syncfusion.com/forums/135954/how-to-create-unicode-font-with-pdftruetypefont-or-the-proper-way-to-draw-a-unicode-string I tried a lot of things but nothing is wroking. What should I do?

Update:

i replaced the code above with a new code:

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

the previous exception didn't appear, the pdf was generated but the arabic text didn't appear, what am i missing?

CodePudding user response:

this is taken directly from the 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;

CodePudding user response:

We can draw RTL text in PDF grid with Syncfusion PDF library in Xamarin.Forms application. Please find the sample code below,

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

Please find the sample from 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.

Note: String format assigned in your sample is not applied on corresponding PDF grid cell which having Arabic text. We have to provide cell index as 1 as per the sample code provided.

  • Related