2010-12-06 117 views
24

我正在使用C#和Winforms。我正试图在纸卷上打印账单。纸张的宽度是3英寸,但纸张的长度是动态的(它是卷纸)。长度取决于列表中有多少项目。例如。在购买时如果有100件商品出售,那么它将是相当长的时间,而对于购买的单个商品它将是很小的长度。在卷纸上打印

当我打印报告时,在结束作业后,打印机弹出最后一页,超出我的需要。它可以长达A4尺寸的纸张。我想打印所需的行,然后停止打印。 我使用一卷纸,而不是A4或A3和Epson LQ-300 + II打印机。

更具体地说,打印总是以页面大小的单位完成。如果我将页面设置为3in x 8in,那么我总是会打印出8英寸长的打印输出。如果我有一张9英寸的钞票要打印,我最终会打印一张16英寸的纸张,浪费7英寸的纸张。我怎样才能打印最后一页,只要它需要?

下面是代码:

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

您的问题是?你应该能够继续打印,直到完成。然后你会完成。这是卷纸的优点。 – 2010-12-06 10:52:28

+0

+1,只需继续绘制直到完成并完成一份声明以剪切文件即可。我曾经使用GDI来做这件事,没有Crystal报告的经验 – Anton 2010-12-06 10:56:01

回答

15

您是否尝试过使用一个页面,只是“一条线”长?

省略上下边框,您可以不停地打印。

现在添加一点(所以页面可以被撕掉)并弹出。

试试这个:

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

      printDoc.DefaultPageSettings.PaperSize = pkCustomSize1 

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

+0

鉴于这些限制,这似乎是最合乎逻辑的答案。 – NotMe 2010-12-10 15:04:05

+0

我们如何定义单行纸?在打印机设置中,我只看到了标准尺寸。 – Thunder 2010-12-13 05:44:29

4

您也可以调整上飞的纸张尺寸。较少的工作做一个每页行,但我想这将产生一个更好的打印预览,如果有人是有原因这样做:

printdoc.DefaultPageSettings.PaperSize.Height += lineheight; 
0

这里是你如何定义自定义纸张尺寸和在你的报告中使用它。

打开打印机文件夹(从控制面板)。

打开服务器属性从文件菜单中。它将打开打印机和服务器属性对话框。

选择检查创建一个新的窗体

指定页面的宽度高度。我建议你让身高3英寸。

现在点击保存表格按钮。

您的自定义页面已准备就绪。

在报告和打印机属性中都将此纸张设置为默认纸张尺寸。

现在你很好走了。

0

您也可以使用打印预览选项来完成此过程。

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