We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Export Pie Chart to PDF

Hi Team,

I'm using sfchart component in my WPF project, and want to export sfchart to PDF, But i'm unable to do it. Please help me on this issue.
 
I have also attached a sample. Please add "Export to PDF" functionality on it. 

Regards,
Brijesh
      

Attachment: DynamicDataPieChart_404741cd.rar

6 Replies

MK Muneesh Kumar G Syncfusion Team January 25, 2019 04:48 AM

Hi Brijesh,   
   
Greetings, we have analyzed your requirement but unfortunately, we were unable to extract the attachment in your update.  
 
However, you can export the SfChart to PDF by getting stream of SfChart, then preparing PdfDocument with that stream as per the below code snippet.  
 
Code snippet 
  private void ChartToPDF() 
        { 
            MemoryStream outStream = new MemoryStream(); 
            chart.Save(outStream, new BmpBitmapEncoder()); 
            PdfPage page = pdfDoc.Pages.Add(); 
            PdfBitmap img = new PdfBitmap(outStream); 
            page.Graphics.DrawImage(img, 0, 0, 500, 250); 
            outStream.Close(); 
        } 
 
        private void SavePDF(string pdfFile) 
        { 
            pdfDoc.Save(pdfFile); 
            System.Diagnostics.Process.Start(pdfFile); 
        } 
         
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            pdfDoc = new PdfDocument(); 
            ChartToPDF(); 
            SavePDF("chartpdf.pdf"); 
        } 
 
We have prepared a sample based on this, please find the sample from the following location.  
 
 
Hope it helps you.  
  
Regards,    
Muneesh Kumar G.  
 



BR Brijesh January 28, 2019 04:26 AM

Hi Muneesh,

Thank you for your answer, and it worked for me.
But still i'm facing couple of issues regarding SfChart when it is exported to PDF file. 
     1) It always give black background of SfChart(when PDF is created successfully).
     2) How to make SfChart to fit in PDF page.

I have also attached a sample. I hope this time it will work.

Regards,
Brijesh

Attachment: DynamicPieChart_dfea0931.zip


BF Bruno Fernando Rodrigues January 28, 2019 01:53 PM

I've the same question.


MK Muneesh Kumar G Syncfusion Team January 28, 2019 11:22 PM

Hi Brijesh, 
 
We have analyzed your queries and prepared the required solutions. 
 
Query 1: It always give black background of SfChart(when PDF is created successfully). 
 
The default background for the SfChart is transparent, so the background appears black in PDF conversion. This can be overcome by setting white Background color for the chart as per the below code snippet. 
 
Code Snippet 
 
 
        <syncfusion:SfChart Background="White"> 
         … 
        </syncfusion:SfChart> 
 
 
Query 2: How to make SfChart to fit in PDF page. 
 
To make the chart fit in the pdf page, you have to set SfChart size and PdfGrphics (page.Graphics) size as same. 
 
SfChart 
 
 
        <syncfusion:SfChart Width="300" Height="200"> 
         … 
        </syncfusion:SfChart> 
 
 
              PdfGraphics 
               
 
        private void ChartToPDF() 
        { 
            … 
            page.Graphics.DrawImage(img, 0, 0, 300, 200); 
        } 
 
 
 
SfChart HorzontalAlignment must be left and VerticalAlignment must be top. 
 
SfChart 
 
 
        <syncfusion:SfChart HorizontalAlignment="Left" VerticalAlignment="Top"> 
         … 
        </syncfusion:SfChart> 
 
 
 
If margin is applied for the SfChart, twice the displacement has to be given for the PdfGraphics x and y position. 
 
 
private void ChartToPDF() 
        { 
            page.Graphics.DrawImage(img, (float)(-ChartMargin.Left * 2) - AdjustmentCoefficient, (float)(-ChartMargin.Top * 2) - AdjustmentCoefficient, 300, 200); 
        } 
 
 
 
We have modified the sample as per your requirement. 
 
 
Regards,
Muneesh Kumar G.
 
 



VF Victor Faria March 18, 2019 08:36 PM

So it's not possible to export na image diferente from it's screen size?



MK Muneesh Kumar G Syncfusion Team March 19, 2019 01:29 AM

Hi Brijesh, 
 
We have analyzed your requirement and you can achieve this by setting Height, Width for SfChart as per the below code snippet.  
 
Code snippet 
<Grid > 
        <Grid.DataContext> 
            <local:ViewModel></local:ViewModel> 
        </Grid.DataContext> 
        <Grid.RowDefinitions> 
            <RowDefinition Height="8*"/> 
            <RowDefinition Height="2*"/> 
        </Grid.RowDefinitions> 
        <Button Content="Save" Click="Button_Click" Height="30" Width="70"  
                Grid.Row="1" 
                Margin="5"/> 
        <chart:SfChart   x:Name="chart"  Background="White" Height="300" Width="600"    
                         HorizontalAlignment="Left" 
                         VerticalAlignment="Top"> 
                         .. 
        </chart:SfChart> 
        
    </Grid>   
 
Also, exporting rendering from (0,0) position. So, SfChart HorizontalAlignment must be left and VerticalAlignment must be top.  
 
 
Exported Path:  ...\WPF_Sample\Sample\bin\Debug 
 
Exported image:  
 
Please let us know if you have any queries.  
 
Thanks,  
Muneesh Kumar G.  
 


Loader.
Live Chat Icon For mobile
Up arrow icon