Problem of formatting tikets c #

1

I have a problem printing the tiket on a printer. as you can see the sheet is letter size, so the printer marks me error.

What I do not understand is how to adjust the sheet so that it can be printed to fit the printer.

  

Code 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);

    }
}
  

How to avoid leaving the printer selection window and go directly?

    
asked by DoubleM 14.09.2017 в 10:53
source

1 answer

1

You must set the PaperSize of the printer by which you print in PrintPageEventArgs as follows:

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

If the printer has it as the default printer, to obtain it you can add this:

PrinterSettings ps = new PrinterSettings();

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

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

This way you make sure you are taking the printer data by default from the system.

To prevent the printing window from coming out, you should remove the PrintDialog of the event button1_Click and it would look like this:

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();
}
    
answered by 14.09.2017 / 11:07
source