简体   繁体   English

在卷纸上打印

[英]Printing on roll paper

I am using C# with Winforms.我在 Winforms 中使用 C#。 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).纸张的宽度为 3 英寸,但纸张的长度是动态的(它是卷纸)。 The length depends on how many items are there in the list.长度取决于列表中有多少项。 Eg 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.例如,在一次购买中,如果有 100 件商品售出,那么它会很长,而对于购买的单个商品,它的长度会很短。

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.它弹出 A4 大小的纸张。 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.我使用的是一卷纸,不是 A4 或 A3,还有一台爱普生 LQ-300 + II 打印机。

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.如果我将页面设置为 3 英寸 x 8 英寸,那么最终打印输出的长度总是 8 英寸的倍数。 If I have a 9in bill to print, I end up with a 16in printout, wasting 7in of paper.如果我要打印一张 9 英寸的账单,我最终会打印出 16 英寸的打印输出,浪费 7 英寸的纸张。 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;
        }

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请参阅:http: //msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.papersize.aspx

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;

I'm using the VKP80II and what I do is i set the pagesettings.papersize to:我正在使用 VKP80II,我所做的是将 pagesettings.papersize 设置为:

PaperSize PaperRoll= new PaperSize("Paper Roll", 300, 0);

automatically it prints the exact length that it needs to without me actually specifying the length它会自动打印所需的确切长度,而无需我实际指定长度

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.我建议你让你的身高 3 英寸。

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.现在你可以开始了。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM