tags:

views:

28

answers:

1

Hi all, I am able to print a chart from my c# project using:

chart1.Printing.PrintDocument.DocumentName = "Graph of data";

But is it possible to add a title to this? I was hoping the document name would achieve this, but apparently not!

Thanks..

A: 

Here is a workaround solution to your problem, if you place the ChartingControl inside a Panel control on the Windows Form. You can then print the panel, inside the panel you can add the document heading as a label and whatever other stuff you want to add.

Firstly from the toolbox add a PrintDocument control and call it MyPrintDocument

Then add a Panel control and put your chart inside it.

Make sure you have imported the System.Drawing namespace, then you can print the panel like this.

    Bitmap MyChartPanel = new Bitmap(panel1.Width, panel1.Height);
    panel1.DrawToBitmap(MyChartPanel, new Rectangle(0, 0, panel1.Width, panel1.Height));

    PrintDialog MyPrintDialog = new PrintDialog();

    if (MyPrintDialog.ShowDialog() == DialogResult.OK)
    {
        System.Drawing.Printing.PrinterSettings values;
        values = MyPrintDialog.PrinterSettings;
        MyPrintDialog.Document = MyPrintDocument;
        MyPrintDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
        MyPrintDocument.Print();
    }

    MyPrintDocument.Dispose();

This code converts the panel into a Bitmap and then prints that Bitmap.

You could condense this into a function like:

public void PrintPanel(Panel MyPanel)
{
   // Add code from above in here, changing panel1 to MyPanel...
}
kyndigs
Very clever! Will give it a go.. Thanks.
Mark
Let me know if it works for you, I didnt test it fully as I have no printer!
kyndigs
It causes some problems with scaling the chart if the program was not fullscreen when told to print. So I may well stick to using the built in printing function. However, your solution led me to a way of doing it - capturing the PrintPage event and drawing the string manually over the top. The problem now is understanding why the margins aren't where they say they are so that it doesn't write in the middle of the chart!
Mark