Skip to content

SyncfusionExamples/How-to-export-the-chart-to-PDF-in-the-.NET-MAUI-Charts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

How to export the chart to PDF in the .NET MAUI Charts

This article describes how to export the chart to the PDF document in the .NET MAUI Charts. To export the chart to the 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 the 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.
[C#]

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.
[C#]

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.

Output:

ExportedChartAsPDF

About

This article explains how to export the chart to PDF in the .NET MAUI Charts

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6

Languages