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 ?
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:
- Right-click on the attached image at Resources/Images/image0.png.
- Go to Properties.
- 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
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)
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 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
- 3 Replies
- 2 Participants
-
TH Tobias Horn
- Oct 24, 2024 08:27 PM UTC
- Oct 28, 2024 02:09 PM UTC