Printing on roll paper

2019-02-02 09:07发布

I am using C# with Winforms. I am trying to print bills on a paper roll. The width of the paper is 3in but the length of the paper is dynamic (its a roll paper). The length depends on how many items are there in the list. E.g. in a purchase if there are 100 items sold then it will be quite long roll while for a single item purchased it would be of small length.

When I print the report, after the end job, printer eject the last page more than I need. It eject paper as long as A4 size. I want to print the required lines, then stop printing. I use a roll of paper, not A4 or A3 and an Epson LQ-300 + II printer.

To be more specific, printing is always done to page-sized units. If I set the page to be 3in x 8in then I always end up with a printout that is a multiple of 8in long. If I have a 9in bill to print, I end up with a 16in printout, wasting 7in of paper. How can I print with the last page being only as long as it needs to be?

Here is the code:

private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
        {
            Font printFont = new Font("Courier New", 12);
            int y = 15;
            e.Graphics.DrawString("a Line", printFont, Brushes.Black, 0, y); y = y + 20;
            e.Graphics.DrawString(" Line", printFont, Brushes.Black, 0, y); y = y + 25;
            e.Graphics.DrawString(" Line", printFont, Brushes.Black, 0, y); y = y + 35;
            e.Graphics.DrawString(" Line", printFont, Brushes.Black, 0, y); y = y + 45;
        }

4条回答
Luminary・发光体
2楼-- · 2019-02-02 09:44

Here is how you can define a custom paper size and use it in your report.

Open printer folder (from Control Panel).

Open Server Properties from the file menu. It will open Printer and Server Properties Dialogue box.

Select Check Create a new Form

Specify page width height. I suggest you make your height 3 inches.

Now click on the Save Form button.

Your custom page is ready.

set this paper as your default paper size both in the report as well as in the printer properties.

Now you are good to go.

查看更多
走好不送
3楼-- · 2019-02-02 09:53

You can also use the print preview option to complete this process.

// This is for the print preview event
 private void printPreviewDialog1_Load(object sender, EventArgs e)
 {
     int j = 0;
     z = 185;

     while (j < dataGridView1.Rows.Count)
     {                 
         j += 1;
         z += 30;
     }

     z += 60;

     PaperSize pkCustomSize1 = new PaperSize("First custom size", 350, z);

     printDocument1.DefaultPageSettings.PaperSize = pkCustomSize1;
 }

 // This is the loop for generating print Document
 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
 {
     int i = 0; //For Gridview Row Count
     int sno = 1; //For Grid Serial Number

     e.Graphics.DrawString(
         "HEADING", 
         new Font("Calibri", 20, FontStyle.Bold), 
         Brushes.Black, 
         new Point(100, 5));

     e.Graphics.DrawString(
         "Address", 
         new Font("Calibri", 12, FontStyle.Bold), 
         Brushes.Black, 
         new Point(75, 35));

    int y = 185; //For Grid y axis start to print 

    while (i < dataGridView1.Rows.Count)
    {
        e.Graphics.DrawString(
            sno.ToString(), 
            new Font("Calibri", 10, FontStyle.Bold), 
            Brushes.Black, 
            new Point(10, y)); //For Serial Number

        e.Graphics.DrawString(
            dataGridView1.Rows[i].Cells[1].FormattedValue.ToString(), 
            new Font("Calibri", 10, FontStyle.Bold), 
            Brushes.Black, 
            new Point(240, y));

        //This is for Trim content to next line
        Graphics df1 = e.Graphics;
        SizeF ef1 = df1.MeasureString(
            dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(),
            new Font(new FontFamily("Calibri"), 12F, FontStyle.Bold),
            200); //160

        df1.DrawString(
            dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(),
            new Font(new FontFamily("Calibri"), 12F, FontStyle.Bold), 
            Brushes.Black,
            new RectangleF(new PointF(60.0F, y), ef1), //350.0
            StringFormat.GenericTypographic);

        i += 1;
        sno += 1;
        y += 30;
    }

    e.Graphics.DrawString(
        "------------------------------------------------------------------------------------",
        new Font("Calibri", 10, FontStyle.Bold), 
        Brushes.Black, 
        new Point(0, y));

    e.Graphics.DrawString(
        "Total Amount-:" + TotalAmnt_txt.Text, 
        new Font("Calibri", 10, FontStyle.Bold), 
        Brushes.Black, 
        new Point(150, y+=20));

    e.Graphics.DrawString(
        "------------------------------------------------------------------------------------", 
        new Font("Calibri", 10, FontStyle.Bold), 
        Brushes.Black, 
        new Point(0, y+=20));

    e.Graphics.DrawString(
        "***Care For You ****", 
        new Font("Calibri", 10, FontStyle.Bold), 
        Brushes.Black, 
        new Point(150, y += 20));

    i = 0;
    sno = 1;
}
查看更多
4楼-- · 2019-02-02 09:59

Have you tried using a page that is only "one line" long?

Omit the upper and lower border, and you can print non stop.

Now add a bit (So the page can be torn off) and eject that.

Try this:

            PaperSize pkCustomSize1 = new PaperSize("First custom size", 100, 200);

            printDoc.DefaultPageSettings.PaperSize = pkCustomSize1

See: http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.papersize.aspx

查看更多
小情绪 Triste *
5楼-- · 2019-02-02 10:03

You can also adjust the paper size on the fly. Less work to do it one line per page, but I'd imagine this would produce a nicer print preview if anyone were to have cause to do that:

printdoc.DefaultPageSettings.PaperSize.Height += lineheight;
查看更多
登录 后发表回答