Crash when exporting chart to PDF

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
OM Partners
Newbie
Newbie
Posts: 8
Joined: Mon Jun 02, 2014 12:00 am

Crash when exporting chart to PDF

Post by OM Partners » Fri Jul 11, 2014 2:19 pm

Hi,

When I export a chart to a PDF which is already open, TeeChart crashes.
This is the code that I used:

Code: Select all

Steema.TeeChart.TChart tChart = new Steema.TeeChart.TChart();
Steema.TeeChart.Export.Exports exp = tChart.Export;
Steema.TeeChart.Export.ImageExport imgExp = exp.Image;
Steema.TeeChart.Export.PDFFormat pfdFormat = imgExp.PDF;
pfdFormat.Save("F:\\Test.pdf");
The TeeChart version that I use is 4.1.2012.5103.

Is this issue solved in a more recent version of TeeChart?

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Crash when exporting chart to PDF

Post by Christopher » Mon Jul 14, 2014 8:43 am

Hello,
OM Partners wrote:Is this issue solved in a more recent version of TeeChart?
This issue is a common problem when saving files to disk. The issue here is that there is no way to close an open file without killing the process that has it open first. In the case of a pdf file, this means enumerating every open process on the machine and best-guessing which process is holding the file open. This is not only unreliable, it could also be very inconvenient to a user to have one of their processes killed automatically.

My best suggesting then is that you either:
1) use a unique file name for each file you save using a datetime stamp.
2) inform the user of the open file and suggest that they close it, e.g.

Code: Select all

    private void button1_Click(object sender, EventArgs e)
    {
      string fileName = @"D:\tmp\chart.pdf";

      if(!CheckFileOpen(fileName)) tChart1.Export.Image.PDF.Save(fileName);
    }

    private bool CheckFileOpen(string fileName)
    {
      if(!File.Exists(fileName)) 
      {
        return false;
      }
      else
      {
        try
        {
          Stream s = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
          s.Close();
          
          return false;
        }
        catch (Exception)
        {
          MessageBox.Show("File " + fileName + " is open. Please close it.");

          return true;
        }
      }
    }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply