Skip to main content
título editado
Enlace
user128299
user128299

¿Cómo abrir la ventana de vista previa de impresión? C# [SOLUCIONADO]

título editado
Enlace
Ana Pimentel
  • 304
  • 1
  • 7
  • 19

¿Cómo abrir la ventana de vista previa de impresión? C# [SOLUCIONADO]

se añadieron 7789 caracteres en el cuerpo
Origen Enlace
Ana Pimentel
  • 304
  • 1
  • 7
  • 19
 private void registerButton_Click(object sender, EventArgs e)
    {
        try
        {
            printDocument.Print();
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Ha ocurrido un error al procesar la impresión:\n" + ex.Message,
                            "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }               

Cuando se ejecuta printDocument.Print() se ejecuta lo siguiente:

 private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Font f = new Font(this.Font, FontStyle.Bold);
        int y = 235;

        e.Graphics.DrawString("PROGRAMACIÓN DE PAGOS", f, Brushes.Black, 330, 50);
        e.Graphics.DrawString("Vence: ", this.Font, Brushes.Black, 15, 100);
        e.Graphics.DrawString(dateCad.Value.Date.ToLongDateString(), f, Brushes.Black, 130, 100);
        e.Graphics.DrawString("Proveedor: ", this.Font, Brushes.Black, 15, 115);
        e.Graphics.DrawString(cmbProvider.Text, f, Brushes.Black, 130, 115);
        e.Graphics.DrawString("RFC: ", this.Font, Brushes.Black, 15, 130);
        e.Graphics.DrawString(txtRfc.Text, f, Brushes.Black, 130, 130);
        e.Graphics.DrawString("Datos de depósito: ", this.Font, Brushes.Black, 15, 145);
        e.Graphics.DrawString(txtBankAccount.Text, f, Brushes.Black, 130, 145);
        e.Graphics.DrawString("Talón de embarque: ", this.Font, Brushes.Black, 15, 160);
        e.Graphics.DrawString(txtFolio.Text, f, Brushes.Black, 130, 160);
        e.Graphics.DrawString("Fecha de recepción: ", this.Font, Brushes.Black, 15, 175);
        e.Graphics.DrawString(dateReceive.Value.Date.ToLongDateString(), f, Brushes.Black, 130, 175);
        e.Graphics.DrawString("Detalle", this.Font, Brushes.Black, 15, 190);
        e.Graphics.DrawString("Documento", this.Font, Brushes.Black, 15, 205);
        e.Graphics.DrawString("Número / Descripción", this.Font, Brushes.Black, 145, 205);
        e.Graphics.DrawString("Importe", this.Font, Brushes.Black, 315, 205);
        e.Graphics.DrawString("Descuentos", this.Font, Brushes.Black, 405, 205);
        e.Graphics.DrawString("Neto", this.Font, Brushes.Black, 530, 205);
        e.Graphics.DrawLine(Pens.Black, 10, 220, 380, 220);

        for (int i = 0; i < dataGridView1.Rows.Count; i++)            
        {
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].Value.ToString(), this.Font, Brushes.Black, 35, y);
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].Value.ToString(), this.Font, Brushes.Black, 90, y);
            e.Graphics.DrawString(string.Format("{0:N2}", Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value)), this.Font, Brushes.Black, 380, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            e.Graphics.DrawString(string.Format("{0:N2}", Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value)), this.Font, Brushes.Black, 480, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            e.Graphics.DrawString(string.Format("{0:N2}", Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value)), this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

            y += 15;
        }

        e.Graphics.DrawLine(Pens.Black, 480, y, 580, y);
        e.Graphics.DrawString("Sub-Total: ", this.Font, Brushes.Black, 340, y);
        e.Graphics.DrawString(lblSubTotal.Text, this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

        y += 15;

        int xPos = 100;

        if (schedule.Discount1 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount1), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount2 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount2), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount3 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount3), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount4 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount4), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount5 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount5), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }

        e.Graphics.DrawString("Desc.Adic.: ", this.Font, Brushes.Black, 340, y);
        e.Graphics.DrawString(txtDiscount.Text, this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

        if (txtAdditionalText.Text.Trim().Length != 0 || aditional != 0.0M)
        {
            y += 15;               
            e.Graphics.DrawString(txtAdditionalText.Text, this.Font, Brushes.Black, 15, y);

            e.Graphics.DrawString("Adicional: ", this.Font, Brushes.Black, 340, y);

            if (aditional > 0.0M)
                e.Graphics.DrawString(txtAditional.Text + "+", this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            else if (aditional < 0.0M)
                e.Graphics.DrawString(txtAditional.Text + "-", this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            else
                e.Graphics.DrawString(txtAditional.Text, this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
        }

        y += 15;

        e.Graphics.DrawLine(Pens.Black, 480, y, 580, y);
        e.Graphics.DrawString("Total: ", this.Font, Brushes.Black, 340, y);
        e.Graphics.DrawString(lblTotal.Text, f, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

        y += 25;
        e.Graphics.DrawString("DESCUENTOS INCLUIDOS:", this.Font, Brushes.Black, 15, y);
        y += 15;

        foreach (PayDetail paid in collection)
        {
            foreach (Discounts disc in paid.Discounts)
            {
                e.Graphics.DrawString(disc.Quantity.ToString(), this.Font, Brushes.Black, 35, y);
                e.Graphics.DrawString(disc.Description, this.Font, Brushes.Black, 90, y);
                e.Graphics.DrawString(string.Format("{0:N2}", disc.Price), this.Font, Brushes.Black, 480, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
                e.Graphics.DrawString(string.Format("{0:N2}", disc.Quantity * disc.Price), this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
                y += 15;
            }
        }
        y += 20;

        e.Graphics.DrawString("Observaciones: ", this.Font, Brushes.Black, 15, y);
        y += 15;
        e.Graphics.DrawString(txtNotes.Text.Trim(), this.Font, Brushes.Black, 30, y);

        y += 100;
        e.Graphics.DrawLine(Pens.Black, 130, y, 250, y);
        e.Graphics.DrawLine(Pens.Black, 370, y, 490, y);
        y += 15;
        e.Graphics.DrawString("Revisó", this.Font, Brushes.Black, 170, y);
        e.Graphics.DrawString("Autorizó", this.Font, Brushes.Black, 410, y);

        e.HasMorePages = false;

        
    }

¿cómo puedo pre visualizar el resultado de printDocument_PrintPage()?

 private void registerButton_Click(object sender, EventArgs e)
    {
        try
        {
            printDocument.Print();
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Ha ocurrido un error al procesar la impresión:\n" + ex.Message,
                            "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }               
    
 private void registerButton_Click(object sender, EventArgs e)
    {
        try
        {
            printDocument.Print();
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Ha ocurrido un error al procesar la impresión:\n" + ex.Message,
                            "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }               

Cuando se ejecuta printDocument.Print() se ejecuta lo siguiente:

 private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Font f = new Font(this.Font, FontStyle.Bold);
        int y = 235;

        e.Graphics.DrawString("PROGRAMACIÓN DE PAGOS", f, Brushes.Black, 330, 50);
        e.Graphics.DrawString("Vence: ", this.Font, Brushes.Black, 15, 100);
        e.Graphics.DrawString(dateCad.Value.Date.ToLongDateString(), f, Brushes.Black, 130, 100);
        e.Graphics.DrawString("Proveedor: ", this.Font, Brushes.Black, 15, 115);
        e.Graphics.DrawString(cmbProvider.Text, f, Brushes.Black, 130, 115);
        e.Graphics.DrawString("RFC: ", this.Font, Brushes.Black, 15, 130);
        e.Graphics.DrawString(txtRfc.Text, f, Brushes.Black, 130, 130);
        e.Graphics.DrawString("Datos de depósito: ", this.Font, Brushes.Black, 15, 145);
        e.Graphics.DrawString(txtBankAccount.Text, f, Brushes.Black, 130, 145);
        e.Graphics.DrawString("Talón de embarque: ", this.Font, Brushes.Black, 15, 160);
        e.Graphics.DrawString(txtFolio.Text, f, Brushes.Black, 130, 160);
        e.Graphics.DrawString("Fecha de recepción: ", this.Font, Brushes.Black, 15, 175);
        e.Graphics.DrawString(dateReceive.Value.Date.ToLongDateString(), f, Brushes.Black, 130, 175);
        e.Graphics.DrawString("Detalle", this.Font, Brushes.Black, 15, 190);
        e.Graphics.DrawString("Documento", this.Font, Brushes.Black, 15, 205);
        e.Graphics.DrawString("Número / Descripción", this.Font, Brushes.Black, 145, 205);
        e.Graphics.DrawString("Importe", this.Font, Brushes.Black, 315, 205);
        e.Graphics.DrawString("Descuentos", this.Font, Brushes.Black, 405, 205);
        e.Graphics.DrawString("Neto", this.Font, Brushes.Black, 530, 205);
        e.Graphics.DrawLine(Pens.Black, 10, 220, 380, 220);

        for (int i = 0; i < dataGridView1.Rows.Count; i++)            
        {
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].Value.ToString(), this.Font, Brushes.Black, 35, y);
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].Value.ToString(), this.Font, Brushes.Black, 90, y);
            e.Graphics.DrawString(string.Format("{0:N2}", Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value)), this.Font, Brushes.Black, 380, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            e.Graphics.DrawString(string.Format("{0:N2}", Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value)), this.Font, Brushes.Black, 480, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            e.Graphics.DrawString(string.Format("{0:N2}", Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value)), this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

            y += 15;
        }

        e.Graphics.DrawLine(Pens.Black, 480, y, 580, y);
        e.Graphics.DrawString("Sub-Total: ", this.Font, Brushes.Black, 340, y);
        e.Graphics.DrawString(lblSubTotal.Text, this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

        y += 15;

        int xPos = 100;

        if (schedule.Discount1 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount1), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount2 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount2), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount3 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount3), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount4 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount4), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }
        if (schedule.Discount5 != 0.0f)
        {
            e.Graphics.DrawString(string.Format("{0:N2}-", schedule.Discount5), this.Font, Brushes.Black, xPos, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            xPos += 40;
        }

        e.Graphics.DrawString("Desc.Adic.: ", this.Font, Brushes.Black, 340, y);
        e.Graphics.DrawString(txtDiscount.Text, this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

        if (txtAdditionalText.Text.Trim().Length != 0 || aditional != 0.0M)
        {
            y += 15;               
            e.Graphics.DrawString(txtAdditionalText.Text, this.Font, Brushes.Black, 15, y);

            e.Graphics.DrawString("Adicional: ", this.Font, Brushes.Black, 340, y);

            if (aditional > 0.0M)
                e.Graphics.DrawString(txtAditional.Text + "+", this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            else if (aditional < 0.0M)
                e.Graphics.DrawString(txtAditional.Text + "-", this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            else
                e.Graphics.DrawString(txtAditional.Text, this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
        }

        y += 15;

        e.Graphics.DrawLine(Pens.Black, 480, y, 580, y);
        e.Graphics.DrawString("Total: ", this.Font, Brushes.Black, 340, y);
        e.Graphics.DrawString(lblTotal.Text, f, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));

        y += 25;
        e.Graphics.DrawString("DESCUENTOS INCLUIDOS:", this.Font, Brushes.Black, 15, y);
        y += 15;

        foreach (PayDetail paid in collection)
        {
            foreach (Discounts disc in paid.Discounts)
            {
                e.Graphics.DrawString(disc.Quantity.ToString(), this.Font, Brushes.Black, 35, y);
                e.Graphics.DrawString(disc.Description, this.Font, Brushes.Black, 90, y);
                e.Graphics.DrawString(string.Format("{0:N2}", disc.Price), this.Font, Brushes.Black, 480, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
                e.Graphics.DrawString(string.Format("{0:N2}", disc.Quantity * disc.Price), this.Font, Brushes.Black, 580, y, new StringFormat(StringFormatFlags.DirectionRightToLeft));
                y += 15;
            }
        }
        y += 20;

        e.Graphics.DrawString("Observaciones: ", this.Font, Brushes.Black, 15, y);
        y += 15;
        e.Graphics.DrawString(txtNotes.Text.Trim(), this.Font, Brushes.Black, 30, y);

        y += 100;
        e.Graphics.DrawLine(Pens.Black, 130, y, 250, y);
        e.Graphics.DrawLine(Pens.Black, 370, y, 490, y);
        y += 15;
        e.Graphics.DrawString("Revisó", this.Font, Brushes.Black, 170, y);
        e.Graphics.DrawString("Autorizó", this.Font, Brushes.Black, 410, y);

        e.HasMorePages = false;

        
    }

¿cómo puedo pre visualizar el resultado de printDocument_PrintPage()?

Origen Enlace
Ana Pimentel
  • 304
  • 1
  • 7
  • 19
Loading