Articles in this section
Category / Section

How to apply the PDF document size to Xamarin.Forms Chart?

1 min read

Chart can be exported to pdf with the help of GetStream method of SfChart and DependencyService. Set the required size to chart before getting it as stream and pass it to pdf document. If you want to stretch the chart in pdf document, set the size of pdf document to chart using WidthRequest and HeightRequest properties of SfChart. Please refer the below code snippets to set the size of pdf document to chart and getting the stream of chart.

 

C#:

 

var document = new PdfDocument();
chart.WidthRequest = document.PageSettings.Width;
chart.HeightRequest = document.PageSettings.Height;
 
var stream = chart.GetStream();
var page = document.Pages.Add();
var graphics = page.Graphics;
graphics.DrawImage(PdfImage.FromStream(stream), 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
            
DependencyService.Get<IChartDependencyService>().SaveAsPDF("Chart.pdf", stream, chart, document);

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (2)
Please  to leave a comment
DC
dev clinicea

If 1 contentpage has multiple charts, how do we export all of them to PDF? Tx.

WI
William

This is very useful and displays the chart in the correct size on the PDF. However the scaling of the chart is all wrong, it is literally outputted as a stretched version of whatever was on screen, rather than being rendered correctly for the PDF size. This also means that, for example, if the user has zoomed in on the chart, this view is what is exported rather than the full version of the chart. I would be interested in knowing if there is a way of achieving the above but with the chart rendered correctly for it's new PDF size.

Access denied
Access denied