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