1

introducir la descripción de la imagen aquí

Tengo un problema al imprimir el tiket en una impresora. como pueden ver la hoja es tamaño carta, por lo cual la impresora me marca error.

introducir la descripción de la imagen aquí

lo que no entiendo es como ajustar la hoja para que se pueda imprimir a la medida de la impresora.

Codigo C#

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        PrintDialog printDialog = new PrintDialog();
        PrintDocument printDocument = new PrintDocument();
        printDialog.Document = printDocument; //add the document to the dialog box...      
        printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateReceipt); //add an event handler that will do the printing

        //on a till you will not want to ask the user where to print but this is fine for the test envoironment.

        DialogResult result = printDialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            printDocument.Print();

        }
    }

    public void CreateReceipt(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

        PrinterSettings ps = new PrinterSettings();
        ps.PrinterName = "Brother QL-720NW"; //Para cargar las propiedades de la impresora en ps
        e.PageSettings.PaperSize = ps.DefaultPageSettings.PaperSize;

        Graphics graphic = e.Graphics;

        Font font = new Font("Courier New", 12); //must use a mono spaced font as the spaces need to line up
        float fontHeight = font.GetHeight();

        int startX = 10;
        int startY = 10;
        int offset = 40;



        graphic.DrawString("AdminEmpaq", new Font("Courier New", 18), new SolidBrush(Color.Black), startX, startY);
        string top = "Codigo Entrada: ".PadRight(10) + "1340";
        graphic.DrawString(top, font, new SolidBrush(Color.Black), startX, startY + offset);
        offset = offset + (int)fontHeight; //make the spacing consistent
        graphic.DrawString("----------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
        offset = offset + (int)fontHeight + 5; //make the spacing consistent

        offset = offset + 5; //make some room so that the total stands out.
        graphic.DrawString("Proveedor: ".PadRight(5) + String.Format("{0:c}", "Manuel Morales Ochoa"), font, new SolidBrush(Color.Black), startX, startY + offset);
        offset = offset + 15;
        graphic.DrawString("Peso: ".PadRight(5) + String.Format("{0:c}", "1780"), font, new SolidBrush(Color.Black), startX, startY + offset);
        offset = offset + 15;
        graphic.DrawString("Cajas: ".PadRight(5) + String.Format("{0:c}", "20"), font, new SolidBrush(Color.Black), startX, startY + offset);
        offset = offset + 15;
        graphic.DrawString("Tara: ".PadRight(5) + String.Format("{0:c}", "3"), font, new SolidBrush(Color.Black), startX, startY + offset);
        offset = offset + 15;
        graphic.DrawString("Peso Neto: ".PadRight(5) + String.Format("{0:c}", "1777"), new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY + offset);

        offset = offset + 30; //make some room so that the total stands out.
        graphic.DrawString("     Thank-you for your custom,", font, new SolidBrush(Color.Black), startX, startY + offset);
        offset = offset + 15;
        graphic.DrawString("       please come back soon!", font, new SolidBrush(Color.Black), startX, startY + offset);

    }
}

Como evitar que salga la ventana de seleccion de impresora y se valla directamente?

introducir la descripción de la imagen aquí

2
  • 1
    Hola Manuel, no pongas el codigo en un enlace aparte, ponlo aqui. Para formatearlo puedes seleccionarlo y pulsar ctrl+k o el boton {}. Intenta poner solo el codigo referente a este problema y mostrarnos qué intentaste para resolverlo y que errores te dio. Mira Cómo preguntar para más informacion. Un saludo
    – lois6b
    el 14 sep. 2017 a las 8:55
  • Has probado usando la propiedad PageSettings.PaperSize?
    – Pikoh
    el 14 sep. 2017 a las 9:02

1 respuesta 1

1

Debes establecer el PaperSize de la impresora por la que imprimes en el PrintPageEventArgs de la siguiente manera:

public void CreateReceipt(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    PrinterSettings ps = new PrinterSettings();
    ps.PrinterName = "Nombre de la impresora"; //Para cargar las propiedades de la impresora en ps

    e.PageSettings.PaperSize = ps.DefaultPageSettings.PaperSize;

    ...

}

EDIT

Si la impresora la tienes como impresora por defecto, para obtenerla puedes añadir esto:

PrinterSettings ps = new PrinterSettings();

foreach (PrinterSettings ps2 in PrinterSettings.InstalledPrinters)
{
    if (ps.IsDefaultPrinter)
    {
        ps = ps2;
        break;
    }
}

e.PageSettings.PaperSize = ps.DefaultPageSettings.PaperSize;

De esta manera te aseguras de que estas cogiendo los datos de la impresora por defecto del sistema.


Para evitar que salga la ventana de impresión, debes quitar el PrintDialog del evento button1_Click y quedaría de la siguiente forma:

private void button1_Click(object sender, EventArgs e)
{
    PrintDocument printDocument = new PrintDocument();

    printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateReceipt); //add an event handler that will do the printing

    printDocument.Print();
}
6
  • me sigue marcando el mismo aviso,el papel que uso es de 62mm x 8m, hay alguna propiedad donde establecer eso?
    – DoubleM
    el 14 sep. 2017 a las 9:16
  • @ManuelMorales supongo que no has mirado el enlace que te puse. Probablemente tendras que mirar en la coleccion PrinterSettings.PaperSizes y buscar la que corresponda con la impresora. Tal vez tambien tengas que ajustar PaperSource ..
    – Pikoh
    el 14 sep. 2017 a las 9:23
  • 1
    @ManuelMorales Revisa las propiedades de la impresora a ver que tamaño tiene, si lo tiene mal ponlo bien y prueba de nuevo
    – Marc
    el 14 sep. 2017 a las 9:31
  • Ya he resuelto lo de impresion con lo que ha dicho @Marc, ahora solo quiero que no aparezca la ventana de seleccion de impresión y se valla directamente a impresora predeterminada. he adjuntado una imagen.
    – DoubleM
    el 14 sep. 2017 a las 9:56
  • 1
    @ManuelMorales He editado la respuesta poniendo la solución a lo otro.
    – Marc
    el 14 sep. 2017 a las 10:03

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.