ExportToPdf not working with DataGridImageColumn

I'm trying to export my Datagrid to a pdf using ExportToPdf.

With DataGridTextColumn all works fine until I add a DataGridImageColumn to my Datagrid. Then the execution just stops without any error.


Is ExportToPdf working with DataGridImageColumn ?


3 Replies

SD Sethupathy Devarajan Syncfusion Team October 25, 2024 09:59 AM UTC

Hi Tobias Horn,

 

When exporting the DataGrid to PDF with a DataGridImageColumn, you are encountering an issue where the DataGrid’s execution stops without any error. We suspect that this issue occurs because the image is being set as a string, and the attached image might be a MAUI Image. You can resolve this by following these steps:

 

  1. Right-click on the attached image at Resources/Images/image0.png.
  2. Go to Properties.
  3. In Build Action, select Embedded Resource instead of MAUI Image.

 

In the ViewModel class, retrieve the image with the following code:

var img = ImageSource.FromResource("DataGridSample.Resources.Images.image0.png",typeof(EmployeeViewModel).GetTypeInfo().Assembly);

Kindly modify the image file name and class name according to your sample.

 

We have provided a simple sample for your reference.

Regards,

Sethupathy D.


Attachment: DataGridSample_2273d6ab.zip


TH Tobias Horn October 25, 2024 06:51 PM UTC

the image is safed to the pdf, but its distorted to fill the cell. can i set a dimension ratio for export? my image is square (300x300px)



SD Sethupathy Devarajan Syncfusion Team October 28, 2024 02:09 PM UTC

Hi Tobias Horn,


To prevent image distortion in your PDF export, set the image column width in XAML and enable CanExportColumnWidth during export. This will ensure the image retains its aspect ratio without stretching to fill the cell. We had shared the code snippet , Ug link and Sample for your reference.


Code Snippet:
 

In Xaml
 <syncfusion:SfDataGrid x:Name="dataGrid" VerticalOptions="FillAndExpand" AutoGenerateColumnsMode="None" ColumnWidthMode="Auto"

                        ItemsSource="{Binding Employees}">

     <syncfusion:SfDataGrid.Columns>

                 <syncfusion:DataGridImageColumn MappingName="EmpImage" Width="60"

                                         HeaderText="Employee Image" />

     </syncfusion:SfDataGrid.Columns>

 </syncfusion:SfDataGrid>

 

 

In .cs

private void Button_Clicked(object sender, EventArgs e)

{

    MemoryStream stream = new MemoryStream();

    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();

    DataGridPdfExportingOption option = new DataGridPdfExportingOption();

    option.CanExportColumnWidth = true;

    var pdfDoc = new PdfDocument();

    pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);

    pdfDoc.Save(stream);

    pdfDoc.Close(true);

    SaveService saveService = new();

    saveService.SaveAndView("ExportFeature.pdf", "application/pdf", stream); ;

}

 


Ug link :
ExportColumnWidth

Regards,

Sethupathy D.


Attachment: DataGridSample_(2)_8b5622f3.zip

Loader.
Up arrow icon