Question Print Chart in PDF

isosek

Member
Joined
Jun 15, 2023
Messages
6
Programming Experience
Beginner
Hi,

I search the internet and can't find a solution for my Problem.

I want to generate a PDF Page (A4) of a Chart which is drawed from data of a csv file. The problem I run into is that I only print it out of the standard printer and not on landscape and over the whole site.
How can I solve that issue without using the standard printer?
 
How are you currently sending the PDF to the printer?
 
I Hope this Helps you.

Method PrintDocument:
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // leeres Bitmap erzeugen, auf das gedruckt werden soll
            Bitmap bitmap = new Bitmap(this.Width, this.Height);

            // das Panel und alle darauf befindlichen Objekte, z.B. Labels, TextBoxen, PictureBoxen, in das Bitmap zeichnen
            pnlPage.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));

            // Skalierung einstellen
            Rectangle target = new Rectangle(0, 0, e.MarginBounds.Width, e.MarginBounds.Height);
            double xScale = (double)bitmap.Width / e.MarginBounds.Width;
            double yScale = (double)bitmap.Height / e.MarginBounds.Height;

            if (xScale < yScale)
                target.Width = (int)(xScale * target.Width / yScale);
            else
                target.Height = (int)(yScale * target.Height / xScale);

            e.Graphics.PageUnit = GraphicsUnit.Display;

            // Bitmap in das printDocument zeichnen
            e.Graphics.DrawImage(bitmap, target);
        }

Reading CSv File from fixed path:
public Form1()
        {
            InitializeComponent();
            chartDruck.Titles.Add("Pressure");
            chartVak.Titles.Add("Vacuumpressure");
            chartVak.ChartAreas[0].AxisY.Maximum = 1000; //Vakuumgraph
            chartTemp.Titles.Add("Temperature");
            chartTemp.Legends[0].Docking = Docking.Top;
            chartDruck.Legends[0].Docking = Docking.Top;
            chartVak.Legends[0].Docking = Docking.Top;
        }
        public class ReadCSV
        {
            public DataTable readCSV;

            public ReadCSV(string fileName, bool firstRowContainsFieldNames = true)
            {
                readCSV = GenerateDataTable(fileName, firstRowContainsFieldNames);
            }

            private static DataTable GenerateDataTable(string fileName, bool firstRowContainsFieldNames = true)
            {
                DataTable result = new DataTable();

                if (fileName == "")
                {
                    return result;
                }

                string delimiters = ",";
                string extension = Path.GetExtension(fileName);

                if (extension.ToLower() == "txt")
                    delimiters = "\t";
                else if (extension.ToLower() == "csv")
                    delimiters = ",";

                using (TextFieldParser tfp = new TextFieldParser(fileName))
                {
                    tfp.SetDelimiters(delimiters);

                    // Get The Column Names
                    if (!tfp.EndOfData)
                    {
                        string[] fields = tfp.ReadFields();

                        for (int i = 0; i < fields.Count(); i++)
                        {
                            if (firstRowContainsFieldNames)
                                result.Columns.Add(fields[i]);
                            else
                                result.Columns.Add("Col" + i);
                        }

                        // If first line is data then add it
                        if (!firstRowContainsFieldNames)
                            result.Rows.Add(fields);
                    }

                    // Get Remaining Rows from the CSV
                    while (!tfp.EndOfData)
                        result.Rows.Add(tfp.ReadFields());
                }

                return result;
            }
        }
        // fileName should consist of the CSV file name with full path
        private void LoadCSVOnDataGridView(string fileName)
        {
            try
            {
                ReadCSV csv = new ReadCSV(fileName);

                try
                {
                    dataGridView1.DataSource = csv.readCSV;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnLaden_Click(object sender, EventArgs e)
        {
            //Einlesen der Abtastung
            try
            {
                this.LoadCSVOnDataGridView(@"D:\MLP\SAMP01\00000\SA00000.csv");
                var colCount = dataGridView1.ColumnCount;
                int rowcount = dataGridView1.RowCount - 1;
                int lastRow = dataGridView1.Rows.Count - 2;
                object lastRowValue = dataGridView1.Rows[lastRow].Cells[2].Value;
                DateTime.TryParse(dataGridView1.Rows[0].Cells[2].Value.ToString(), out DateTime startZeit);
                DateTime.TryParse(dataGridView1.Rows[lastRow].Cells[2].Value.ToString(), out DateTime endZeit);
                TimeSpan totalZeit = endZeit - startZeit;
                double totalMinutes = totalZeit.TotalMinutes;
                int totalMinutesInt = Convert.ToInt32(totalMinutes);
                double druck1, druck2;
                double vakuumDruck;
                double tzMin;
                double tzMinOffset;
                int tzMinXPlot;
                TimeSpan zeitMin0 = startZeit - DateTime.MinValue;
                tzMinOffset = zeitMin0.TotalMinutes;
                //Druckgraph
                for (int i = 1; i < rowcount; i++)
                {
                    //IstDruck
                    /*druck1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[15].Value);// Englisches Betriebssystem*/
                    druck1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[15].Value) / 100; //Deutsches Betriebssystem*/
                    DateTime.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out DateTime Zeit);
                    TimeSpan zeitMin = Zeit - DateTime.MinValue;
                    tzMin = zeitMin.TotalMinutes + 1;
                    tzMinXPlot = Convert.ToInt32(tzMin - tzMinOffset);
                    //Solldruck
                    /*druck2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[16].Value);//Englisches Betriebssystem */
                    druck2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[16].Value) / 100;//Deutsches Betriebssystem*/
                    chartDruck.Series["Series1"].Points.AddXY(tzMinXPlot, druck1);
                    //chart1.Series["Series1"].Points.AddY(druck1);
                    chartDruck.Series["Series2"].Points.AddXY(tzMinXPlot, druck2);

                }
                //Vakuumgraph zeichnen
                for (int i = 1; i < rowcount; i++)
                {
                    //Vakuumdruck
                    vakuumDruck = Convert.ToDouble(dataGridView1.Rows[i].Cells[17].Value);
                    DateTime.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out DateTime Zeit);
                    TimeSpan zeitMin = Zeit - DateTime.MinValue;
                    tzMin = zeitMin.TotalMinutes + 1;
                    tzMinXPlot = Convert.ToInt32(tzMin - tzMinOffset);
                    chartVak.Series[0].Points.AddXY(tzMinXPlot, vakuumDruck);

                }
                //Temperaturgraph zeichnen
                double TemperaturP1, TemperaturP2, TemperaturP3;
                if (dataGridView1.Rows[1].Cells[8].Value.ToString() != "0")
                {
                    chartTemp.Series.Add("Temperatur P3");
                    chartTemp.Series[2].ChartType = SeriesChartType.Line;
                }

                for (int i = 1; i < rowcount; i++)
                {

                    /*TemperaturP1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value); //Englisches Betriebssystem*/
                    /*TemperaturP2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value); //Englisches Betriebssystem*/
                    TemperaturP1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value) / 10;//Deutsches Betriebssystem*/
                    TemperaturP2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value) / 10;//Deutsches Betriebssystem*/
                    DateTime.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out DateTime Zeit);
                    TimeSpan zeitMin = Zeit - DateTime.MinValue;
                    tzMin = zeitMin.TotalMinutes + 1;
                    tzMinXPlot = Convert.ToInt32(tzMin - tzMinOffset);
                    chartTemp.Series[0].Points.AddXY(tzMinXPlot, TemperaturP1);
                    chartTemp.Series[1].Points.AddXY(tzMinXPlot, TemperaturP2);
                    if (chartTemp.Series[2] != null)
                    {
                        /*TemperaturP3 = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value);//Englisches Betriebssystem*/
                        TemperaturP3 = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value) / 10;//Deutsches Betriebssystem*/
                        chartTemp.Series[2].Points.AddXY(tzMinXPlot, TemperaturP3);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }
Looks like this after inserting my File. Just change the ".txt" to ".csv".

Test1.png
 

Attachments

  • SA00000.txt
    303.3 KB · Views: 9
I see the PrintPage event handler, but where do you instantiate the PrintDocument class and set the PageSettings and if needed, the PrinterSettings?
 
Nowhere now. I decide to print the Panel as BMP and that's it. It works for me.

For Education I can't refigure the PageSettings and everything. I copied it from somewhere on the Internet and made Buttons for PageSettings and everything.
 
It's usually a bad idea to just copy code from the internet without understanding the code does.

Anyway, here's the official Microsoft documentation that demonstrates printing in landscape mode:
 
Back
Top Bottom