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

Adding sfchart to an existing pdfDocument in XF

Hello,

I have a pdfDocument in my app that includes some elements and pdfGrids, I add all the  elements and Grids to my pdfDoc and then I save it as a pdf file, but before saving the doc I want to add a sfchart at the end of the document, is there any way to do so?

Thank you in advance

Best regards,
Reihaneh

11 Replies

BK Bharathiraja K Syncfusion Team February 19, 2019 12:25 PM UTC

Hi Reihaneh, 
 
Greetings from Syncfusion. We have prepared a simple to achieve your requirement. Please find the sample from below link. 
 
 
Please let us know if you need any further assistance on this.  
 
Regards, 
Bharathiraja.  



RK Reihaneh Khaksaran March 2, 2019 02:36 PM UTC

Hello Bharathiraja ,

Thank you for the sample you provided, but on the line "var stream = chart.GetStream();" I get the following exception:

Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Could you please help me with this?

Thank you


BK Bharathiraja K Syncfusion Team March 6, 2019 10:47 AM UTC

Hi Reihaneh, 
 
We have analyzed your query and can reproduce the issue in android in the last updated sample. 
We can resolve this issue by removing the parent view. Please refer the below code snippet for more details. 
 
Code Snippet [C#]: SimpleSample.Droid[ChartDependencyService.cs] 
 
        public void SaveAsPDF(string filename, Stream chartStream, SfChart chart, PdfDocument document) 
        { 
     …. 
     finally 
            { 
          
         if (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent != null) 
                { 
                    (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent as ViewGroup).RemoveAllViews(); 
                } 
         .. 
     } 
        } 
 
 
Please let us know if you need any further assistance. 
 
Regards, 
Bharathi. 



RK Reihaneh Khaksaran March 11, 2019 09:13 AM UTC

Hi,

Thank you for your sample, your sample works fine, but in my project, even after adding this piece of code :
if (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent != null) 
                { 
                    (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent as ViewGroup).RemoveAllViews(); 
                } 
 I still get the same exception on the same line as before, it's kinda like that the SaveAsPDF method never gets called. I really don't know what to do!

This the xaml of my chart (the chart is placed in a Grid):

                    <chart:SfChart Grid.Row="9" Grid.Column="0" x:Name="voltageGroupAChart" HeightRequest="200" Margin="0,10,10,0">
                        <chart:SfChart.Legend>
                            <chart:ChartLegend>
                                <chart:ChartLegend.LabelStyle>
                                    <chart:ChartLegendLabelStyle FontSize="8"/>
                                </chart:ChartLegend.LabelStyle>
                            </chart:ChartLegend>
                        </chart:SfChart.Legend>
                        <chart:SfChart.Title>
                            <chart:ChartTitle Text="Voltage Group A (V)" FontSize="10"/>
                        </chart:SfChart.Title>
                        <chart:SfChart.PrimaryAxis>
                            <chart:NumericalAxis  ShowTrackballInfo="True" Minimum="0.00" Maximum="0.30" x:Name="voltageGroupXAxis">
                                <chart:NumericalAxis.Title>
                                    <chart:ChartAxisTitle Text="t/s" TextColor="Black" FontSize="10"/>
                                </chart:NumericalAxis.Title>
                            </chart:NumericalAxis>
                        </chart:SfChart.PrimaryAxis>
                        <chart:SfChart.SecondaryAxis>
                            <chart:NumericalAxis ShowTrackballInfo="True" x:Name="voltageGroupYAxis" Interval="10"/>
                        </chart:SfChart.SecondaryAxis>
                        <chart:SfChart.Series>
                            <chart:FastLineSeries x:Name="voltageGroupASeries1" Color="Red" EnableDataPointSelection="True" SelectedDataPointColor="Green" Label="VL1-E:VL1-E" LegendIcon="StraightLine" StrokeWidth="2"/>
                            <chart:ScatterSeries x:Name="voltageGroupASeries2" Color="Green" EnableDataPointSelection="True" SelectedDataPointColor="Green" Label="Actual:VL1-E:VL1-E" LegendIcon="Circle" StrokeWidth="1"/>
                        </chart:SfChart.Series>
                    </chart:SfChart>

Best regards,
Reihaneh


BK Bharathiraja K Syncfusion Team March 12, 2019 12:30 PM UTC

  
Thanks for the update. 
We have analyzed your query and we have tried to replicate the reported issue at our end by using the provided sample code snippet. We are afraid that we can’t reproduce the issue at our end. 
  
Please find the tested sample: 
  
Since we are not aware of your exact application scenario, we were not able to reproduce this issue at our end, so can you please revert us by modifying the sample based on your application along with replication procedure. This will be helpful for us to investigate further and provide you a better solution at the earliest. 
 
Regards, 
Bharathi. 



RK Reihaneh Khaksaran April 20, 2019 08:06 AM UTC

Hi Bharathiraja,

Thanks for providing the sample, your sample works fine, but I still have the same problem in my app. Actually there are 2 issues now!

1. I realized that I get this exception (Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.) whenever my chart is invisible! and sometimes I need the chart to be invisible but I also need it to be in my pdf report.

2. But when the chart is visible I get another exception! 

"System.NullReferenceException: Object reference not set to an instance of an object."

So I created a sample based on my project and getting a little help from your sample in which I get the same error, I'll be really happy if you could help me to resolve this issue, specially the second problem I'm facing

Thanks again for your great support.

Best regards,
Reihaneh

Attachment: ChartToPdfExample_80068744.zip


DD Devakumar Dhanapoosanam Syncfusion Team April 22, 2019 09:34 AM UTC

 
Thanks for the sample.  
 
Query 1: (Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.) whenever my chart is invisible! 
 
The reported issue has been resolved in the latest SfChart version 17.1.0.38. Please update to the latest SfChart version and let us know whether the issue has been resolved. 
 
Query 2: "System.NullReferenceException: Object reference not set to an instance of an object." 
 
We have analyzed your sample and found that the DependencyService was not registered in your sample attached. Please add the below code to resolve the issue. 
 
CodeSnippet[C#]: 
 
[assembly: Dependency(typeof(ChartToPdfExample.Droid.ExportChartToPdf))] 
namespace ChartToPdfExample.Droid 
{ 
    class ExportChartToPdf : IChartDependencyService 
    { 
         
    } 
}              
 
Also, please find the modified sample from the below link. 
 
Please let us know if you need any further assistance. 
 
Regards, 
Devakumar 



RK Reihaneh Khaksaran April 23, 2019 09:17 AM UTC

Hi,

Thanks for the reply
for query 1, updating to the latest version resolved the issue, thank you so much.

and query 2, you're right sorry, it was my mistake.

But now there are some other issues, after adding the chart to a pdf file the chart disappears from the main page(which is not what I want), unless I remove these lines of code:

                if (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent != null)
                {
                    (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent as ViewGroup).RemoveAllViews();
                }

 and it doesn't throw any exceptions, but will it be OK? I mean removing these lines won't cause any errors later in the future?

- and the next problem is a little more complicated and relates to the behavior I need in my app, in my application there should be some buttons, one adds some grids or texts to a pdf file, and one button saves the pdf file and shows it to the user, I mean I don't want the pdf creation and saving happens with just clicking on one button, up to this point, everything works fine, but as soon as I add the chart, I get null reference exception when I try to save the file, I provided a sample with the same behavior, you see when you click on the "Add Grid To PDF" and then click "SAVE PDF" button, it works totally fine, but when you click on "Add Chart To PDF" and then click "SAVE PDF", it throws null reference exception, but this is the behavior I need in my app, I need to keep adding pages including charts to a pdfDocument by clicking on some buttons(or one button but several times), and then at the end save the pdf file, for example, in the sample you can click on "ADD GRID TO PDF" multiple times, and then save the file then there will be multiple pages in the document, but I can't do the same thing if there is a chart included, I hope I was clear about what I need, and I'd really appreciate it if you could help me with this issue, 
Thank you so much in advance

Best Regards, 
Reihaneh

Attachment: ChartToPdfExample_a883653f.zip


BK Bharathiraja K Syncfusion Team April 25, 2019 12:00 PM UTC

Hi Reihanah,  
  
We have analyzed your query and sample provided.  
  
Query 1: But now there are some other issues, after adding the chart to a pdf file the chart disappears from the main page (which is not what I want), unless I remove these lines of code:  
  
[C#]  
if (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent != null)  
 
          (((Com.Syncfusion.Charts.SfChart)nativeChart).Parent as ViewGroup).RemoveAllViews();  
 
  
  
Yes, you can remove the above lines mentioned in sample. It doesn’t cause any issue with the latest SfChart version 17.1.0.38.   
  
Query 2:  
In ExportChartToPdf class, while try to save the chart as pdf in the SaveAsPdf() method which was calling the document.close() method after saving the document. This method removes the previously added pages in the document while clicking the "Add Chart To PDF". Because of this reason when try to save the pdf "SAVE PDF" the null exception occurs after adding chart.  
  
We have modified the sample by removing the document.close(true) in SaveAsPdf() method and now the chart image will be saved multiple times in the pdf like the Grid.  
  
Code Snippet [C#]:  
public void SaveAsPDF(Stream chartStream, SfChart chart, PdfDocument document)  
{  
      ……  
      try  
      {  
            var page = document.Pages.Add();  
            var graphics = page.Graphics;  
  
            graphics.DrawImage(PdfImage.FromStream(chartStream), 0, 0, page.GetClientSize().Width,  
            page.GetClientSize().Height);  
            MemoryStream stream = new MemoryStream();  
            document.Save(stream);  
            SavePDF(stream, chart);  
      }  
            ……  
 
  
Please find the modified sample from the below link:  
  
Please refer the below UG doc for more details   
  
  
Please let us know if you need any further assistance.   
  
Regards,  
Bharathi. 



RK Reihaneh Khaksaran April 28, 2019 11:41 AM UTC

Hi,

Thanks for your reply and your help for both queries, and for query 2, the solution worked, thank you.

But there is another issue I am facing! The first time that the app appears on the screen, my chart should be invisible, and then the user might add the chart to the pdf file, and after that if the user decides to make the chart visible, the chart is just gone!
To reproduce the issue I have modified the sample that I had created, in the sample if the first time that you run the app you add the chart to pdf and then click on "make chart visible" then you see that there is no chart to be shown but the chart has been added to the pdf, and/or if you make it visible before adding it to the pdf, you can see it, can you help me to find out what's causing this issue please?

P.S: I am not using dependency service to add the chart to pdf anymore, and I am just adding it directly as an image (but I have also tested it with dependency service, and the problem is still there)

Best regards,
Reihaneh

Attachment: ChartToPdfExample_ff89de08.zip


BK Bharathiraja K Syncfusion Team May 2, 2019 12:51 PM UTC

Hi Reihanah,   
 
We have created new Direct Track incident under your account for the reported issue, and we will update all the response in that. So, please login with your Direct Track account to get the response. 
 
 
Regards, 
Bharathi. 


Loader.
Up arrow icon