Page 1 of 1

Crash when exporting chart to PDF

Posted: Fri Jul 11, 2014 2:19 pm
by 15669377
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?

Re: Crash when exporting chart to PDF

Posted: Mon Jul 14, 2014 8:43 am
by Christopher
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;
        }
      }
    }