Articles in this section
Category / Section

How to export the chart to PDF in the .NET MAUI (SfCartesianChart)?

5 mins read

This article describes how to export the chart to a PDF document in the .NET MAUI Charts. To export the chart to a PDF document, you can use the GetStreamAsync method of the SfCartesianChart and Syncfusion.Pdf.PdfDocument library. First, obtain the chart as an image stream using the GetStreamAsync method. Then, use the image stream and Syncfusion.Pdf.PdfDocument library to generate the PDF document. Here are the steps to export the chart to a PDF document:

Step 1: Create the Cartesian chart with the help of this guideline.

Step 2: Add the Syncfusion.Pdf.Net package to your project.

Step 3: Generate a PDF document for the chart.

private async void Button_Clicked(object sender, EventArgs e)
    {
        PdfDocument document = new PdfDocument();
        PdfPage page = document.Pages.Add();
        PdfGraphics graphics = page.Graphics;
       
        float width = (float)chart.Width + 75;
        float height = (float)chart.Height +125;
 
        //To reduce the width and height of the Windows and MAC platform
#if !IOS && !ANDROID
        width = (float)chart.Width / 2.5f;
        height = (float)chart.Height / 1.5f;
#endif
 
        PdfImage img = new PdfBitmap((await chart.GetStreamAsync(ImageFileFormat.Png)));
        graphics.DrawImage(img, new RectangleF(0, 0, width, height));
        MemoryStream stream = new MemoryStream();
        document.Save(stream);
        document.Close(true);
        stream.Position = 0;
        SavePDF("ChartAsPDF.pdf", stream);
        stream.Flush();
        stream.Dispose();
    }
Note:

The chart can be exported as a PDF document only when the chart view is added to the visual tree.

Step 4: Save the PDF document in the default Documents directory.

private async void SavePDF(string fileName, Stream stream)
    {
        fileName = Path.GetFileNameWithoutExtension(fileName) + ".pdf";
        
#if ANDROID
        string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).ToString();
#else
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
#endif
        string filePath = Path.Combine(path, fileName);
        using FileStream fileStream = new(filePath, FileMode.Create, FileAccess.ReadWrite);
        await stream.CopyToAsync(fileStream);
        fileStream.Flush();
        fileStream.Dispose();
    }

 

Note:

To save the PDF document on Android and Windows Phone devices, you must enable file writing permissions on the device storage.


Download the complete sample on GitHub.


Output:

Chart output and exported PDF document example in .NET MAUI Cartesian Chart


Conclusion

I hope you enjoyed learning how to export the chart to PDF in the .NET MAUI Charts.

You can refer to our .NET MAUI Cartesian Chart feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI Cartesian Chart documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Cartesian Chart and other .NET MAUI components.

Please let us know in the comment section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied