ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Creating PDF with .NET Framework
Posted by Philip Watkinson on Jan-08-2021 09:15
Hi Peter,

I evaluated your software last fall. Purchased a licence and have not been disappointed!!!

I've used your sample C# code to create a PDF of a XY Chart. To get all the data, I have to zoom out. The x-axis is time. I get a PDF with all the data but I can't see the details. To get the details I have to zoom in to a certain level, create a PDF, then scroll to the right, create a PDF and continue this process until I have all the data in the PDFs.

Is there a way to do this with C# code and eliminate the manual effort?

Do you have any sample code?

Regards,
Philip

  Re: Creating PDF with .NET Framework
Posted by Peter Kwan on Jan-09-2021 01:08
Hi Philip,

Since you already have the code to plot the chart for any time range you like and output that chart in PDF, you can certainly use a loop to plot the chart for multiple time ranges and output multiple PDFs. An example is like:

// Draw 10 PDF, with each PDF covers 10% of the time range
for (int i = 0; i < 10; ++i)
   drawPDFChart(i / 10.0, (i + 1) / 10.0, "filename_" + i + ".pdf");

The drawPDFChart is essentially the same as the "drawChart" method in the "Simple Zooming and Scrolling" sample code. In the sample code, it starts with:

private void drawChart(WPFChartViewer viewer)
{
     // Get the start date and end date that are visible on the chart.
     DateTime viewPortStartDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft));
     DateTime viewPortEndDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft + .ViewPortWidth));

     ,,, draw the chart from viewPortStartDate to viewPortEndDate
     ,,, display the chart in the WPFChartViewer
}

In your code, instead of using the user selected viewport value, you can pass in the viewport values, and instead of displaying the chart in the WPF, you can save it as a PDF.

private void drawPDFChart(double viewPortLeft, double viewPortRight, string filename)
{
     // Get the start date and end date that are visible on the chart.
     DateTime viewPortStartDate = Chart.NTime(viewer.getValueAtViewPort("x", viewPortLeft));
     DateTime viewPortEndDate = Chart.NTime(viewer.getValueAtViewPort("x", viewPortRight));

     ,,, draw the chart from viewPortStartDate to viewPortEndDate
     ,,, save the chart using the given filename
}

Hope this can help.

Regards
Peter Kwan

  Re: Creating PDF with .NET Framework
Posted by Philip Watkinson on Jan-12-2021 23:59
Hi Peter,

Using the RealTimeZoomScrollWindow example, I modified savePB_Click().

In the code below

string filename = c.makeTmpFile(tempDir, Chart.PDF);

is generating a PNG file. I specified a PDF file with Chart.PDF.

What am I doing wrong?

        private void savePB_Click(object sender, RoutedEventArgs e)
        {
            // The standard Save File dialog
            /*
            SaveFileDialog fileDlg = new SaveFileDialog();
            fileDlg.Filter = "PNG (*.png)|*.png|JPG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|" +
                "SVG (*.svg)|*.svg|PDF (*.pdf)|*.pdf";
            fileDlg.FileName = "chartdirector_demo";
            var ret = fileDlg.ShowDialog(this);
            if (!(ret.HasValue && ret.Value))
                return;

            // Save the chart
            if (null != WPFChartViewer1.Chart)
                WPFChartViewer1.Chart.makeChart(fileDlg.FileName);
            */
            string tempDir = Path.GetTempPath();

            if (WPFChartViewer1.Chart != null)
            {
                MessageBox.Show(tempDir);
                XYChart c = (XYChart)WPFChartViewer1.Chart;
                string filename = c.makeTmpFile(tempDir, Chart.PDF);
                MessageBox.Show(filename);
                bool success = false;
                if (null != WPFChartViewer1.Chart)
                    success = WPFChartViewer1.Chart.makeChart(filename);
                MessageBox.Show(success.ToString());
            }
        }

  Re: Creating PDF with .NET Framework
Posted by Peter Kwan on Jan-13-2021 13:50
Hi Philip,

I have just checked. I found out the makeTmpFile does not support Chart.PDF, and it will output PNG instead.

The makeTmpFile was written long ago, and is originally intended for web applications. It was before ChartDirector supports PDF. I just found out we have not updated the code to support Chart.PDF.

The makeTmpFile has two functions:
  1. It deletes temporary files that are older than a certain time (eg. older than 10 minutes)
  2. It creates a new chart image file.

If you only needs (2: create new file), you can just use makeChart. It is like:

// This is how the filename is generated internally by ChartDirector.
string filename = "chart" + System.Guid.NewGuid().ToString().Replace("-", "") + ".pdf";
c.makeChart(Path.Combine(tempDir, filename));

If you also needs (1: purge old files), you may output a dummy chart.

// This chart is not used and has negligible overhead. Its purpose is to call makeTmpFile to purge old files.
(new PieChart(1, 1)).makeTmpFile(tempDir);

Hope this can help.

Regards
Peter Kwan

  Re: Creating PDF with .NET Framework
Posted by Philip Watkinson on Jan-14-2021 04:42
Hi Peter.

Thanks for your quick response. With your suggestion I was able to create multiple 1 page PDF files. And using the library PDFsharp, I was able to merge the 1 page PDFs into one PDF file.

Philip