SfDatagrid not exporting in Pdf columns with Greek characters

Hi, 

I need some help, I think the title is self-explanatory.

When exporting in Excel everything works just fine.
When exporting in Pdf  the Columns containing names (in Greek) are exported as blank fields! The rest containing dates and numbers are exported as expected.
Is that a bug, or am I doing something wrong?

Here is the code:

            DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
            MemoryStream stream = new MemoryStream();
            var doc = pdfExport.ExportToPdf(this.DataGridRShifts);
            doc.Save(stream);
            doc.Close(true);

            Xamarin.Forms.DependencyService.Get<ISave>().Save("DataGrid.pdf", "application/pdf", stream);

3 Replies

AN Ashok N Syncfusion Team October 13, 2017 08:43 AM UTC

Hi Jonathan, 
 
Thanks for contacting Syncfusion support. 
 
You can export the Greek text into PDF with help of Unicode font. We need to must embed Unicode font with application. Please check the below code snippet for more details: 
 
Stream fontStream; 
private void PDFExport_Clicked(object sender, EventArgs e) 
{ 
    fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("XamarinForms.ARIALUNI.TTF"); 
    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController(); 
    MemoryStream stream = new MemoryStream(); 
    var pdfDoc = new PdfDocument(); 
    PdfPage page = pdfDoc.Pages.Add(); 
    PdfGraphics graphics = page.Graphics; 
    pdfDoc.PageSettings.Orientation = PdfPageOrientation.Landscape; 
    pdfExport.CellExporting += PdfExport_CellExporting; 
    var doc = pdfExport.ExportToPdf(this.dataGrid, new DataGridPdfExportOption() { 
        FitAllColumnsInOnePage = true, 
        PdfDocument = pdfDoc }); 
    doc.Save(stream); 
    doc.Close(true); 
 
    if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows) 
        Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("DataGrid.pdf", "application/pdf", stream); 
    else 
        Xamarin.Forms.DependencyService.Get<ISave>().Save("DataGrid.pdf", "application/pdf", stream); 
 
} 
 
private void PdfExport_CellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    if (e.CellValue != null) 
    { 
        if (IsUnicode(e.CellValue.ToString())) 
        { 
            PdfFont font = e.PdfGridCell.Style.Font; 
            PdfTrueTypeFont unicodeFont = new PdfTrueTypeFont(fontStream, font.Size, font.Style); 
            e.PdfGridCell.Style.Font = unicodeFont; 
        } 
    } 
} 
 
 
Regards, 
Ashok 



JP Jonathan Pappas October 15, 2017 05:51 AM UTC

Thank you for the help. Unfortunatelly I 'had to spend many more hours to make it work, because there were missing info.

Also the sample does not work as it is.

In case anyone else has the same problem, please keep in mind there are 2 more important steps :

#1

ARIALUNI.TTF should be included in your pcl as a resource file (the sample does not include this file and fails)

#2

In case you don't check the sample file, the following function is missing from the previous answer (but is included in the sample)

        public bool IsUnicode(string value)

        {

            if (value == null)

                throw new ArgumentNullException("value");


            return Encoding.UTF8.GetByteCount(value) != value.Length;

        }


Thank you



AN Ashok N Syncfusion Team October 16, 2017 10:49 AM UTC

Hi Jonathan.  
  
Thanks for your update.   
  
Regarding Issue 1:   
In our attached sample having ARIALUNI.TTF file in pcl project. For your reference we have attached the screenshot in the below location, please refer it.  
Screenshot  
  
Regarding IsUnicode Code missing:  
  
We have included the IsUnicode code snippet also in below, please refer it.  
  
public partial class SfDataGridPage : ContentPage  
{  
    public SfDataGridPage()  
    {  
        InitializeComponent();  
    }  
    Stream fontStream;  
    private void PDFExport_Clicked(object sender, EventArgs e)  
    {  
        fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("XamarinForms.ARIALUNI.TTF");  
        DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();  
        MemoryStream stream = new MemoryStream();  
        var pdfDoc = new PdfDocument();  
        PdfPage page = pdfDoc.Pages.Add();  
        PdfGraphics graphics = page.Graphics;  
        pdfDoc.PageSettings.Orientation = PdfPageOrientation.Landscape;  
        pdfExport.CellExporting += PdfExport_CellExporting;  
        var doc = pdfExport.ExportToPdf(this.dataGrid, new DataGridPdfExportOption() {  
            FitAllColumnsInOnePage = true,  
            PdfDocument = pdfDoc });  
        doc.Save(stream);  
        doc.Close(true);  
   
        if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)  
            Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("DataGrid.pdf", "application/pdf", stream);  
        else  
            Xamarin.Forms.DependencyService.Get<ISave>().Save("DataGrid.pdf", "application/pdf", stream);  
   
    }  
   
    private void PdfExport_CellExporting(object sender, DataGridCellPdfExportingEventArgs e)  
    {  
        if (e.CellValue != null)  
        {  
            if (IsUnicode(e.CellValue.ToString()))  
            {  
                PdfFont font = e.PdfGridCell.Style.Font;  
                PdfTrueTypeFont unicodeFont = new PdfTrueTypeFont(fontStream, font.Size, font.Style);  
                e.PdfGridCell.Style.Font = unicodeFont;  
            }  
        }  
    }  
   
    public  bool IsUnicode(string value)  
    {  
        if (value == null)  
            throw new ArgumentNullException("value");  
   
        return Encoding.UTF8.GetByteCount(value) != value.Length;  
    }  
}  
  
Regards,   
Ashok 


Loader.
Up arrow icon