2011-04-27 224 views
1

我创建标签一个WinForm。C#WinForm的PrintPreviewDialog上:打印预览显示多个标签每页

它调用在使用PrintPageEventArgs一个PrintPreviewDialog并显示信息的PrintDocumentPrintPageEventHandler

void Document_Printed(object sender, PrintPageEventArgs e) { 
    // Code goes here 
} 

如果标签是一个小的地址标签去,而不是在PrintPreviewDialog看到一个标签到8.5x11信,我想看到适合对PageSettings.PaperSize标签的数量。

示例:假设四(4)项适合选定的媒体(艾利打印机标签或任何东西)上。

  • 如果我的最终用户指定了1到4份要打印的副本,我希望我的“打印预览”对话框显示所有副本。

  • 如果我的最终用户指定超过四(4)项,打印预览对话框应该显示多个页面(我以前从来没有解决多页)。

我大小我的数据,以满足我的标签尺寸,但我不知道如何让我的PrintPageEventHandlerPrintPreviewDialog显示超过1个标签。

有人能告诉我如何做到这一点?如何显示和打印每张纸上的多个标签(文档?)?

编辑:这里是我的代码,这是基于2个的RectangleF对象:pageRect和LabelRect:

void Document_Printed(object sender, PrintPageEventArgs e) { 
    if (printPreview == null) return; 
    int labelSupport = 1; 
    RectangleF pageRect = new RectangleF(0, 0, printPreview.Document.DefaultPageSettings.PaperSize.Width, printPreview.Document.DefaultPageSettings.PaperSize.Height); 
    float fW = (pageRect.Width < LabelRect.Width) ? (pageRect.Width/LabelRect.Width) : (LabelRect.Width/pageRect.Width); 
    float fH = (pageRect.Height < LabelRect.Height) ? (pageRect.Height/LabelRect.Height) : (LabelRect.Height/pageRect.Height); 
    // START Portion I need HELP with! 
    if (1 < LabelsPerPage) { 
    if (Landscape) { 
    } else { 
    } 
    } else { 
    if (Landscape) { 
    } else { 
    } 
    } 
    // END (I think) Portion I need HELP with! 
    SizeF ratio = new SizeF(fW, fH); 
    Graphics G = e.Graphics; 
    foreach (Label item in labelList) { 
    Console.WriteLine(item.Name); 
    using (SolidBrush b = new SolidBrush(Color.Black)) { 
     using (Pen p = new Pen(b)) { 
     float x = ratio.Width * (float)item.Location.X; 
     float y = ratio.Height * (float)item.Location.Y; 
     float w = ratio.Width * (float)item.Size.Width; 
     float h = ratio.Height * (float)item.Size.Height; 
     RectangleF r = new RectangleF(x, y, w, h); 
     G.DrawString(item.Text, item.Font, b, r); 
     } 
    } 
    } 
} 

编辑2:我需要一种方法来打印1页上的2页或更多的文档。到目前为止还没有任何东西能够解决这个关键问题这是我需要的答案。

+1

了解PrintPageEventHandler – 2011-04-27 20:41:16

+0

的一些渲染代码可能会很有用。已经添加了一个代码示例,从** EDIT:**部分开始。 – jp2code 2011-04-27 20:48:52

+0

这看起来没问题。我会在该方法的顶部放置一个断点,然后逐行放置,以确保您期望发生的事情实际上正在发生。例如,您的绘图循环中的每个项目的大小和位置值是否更改?看起来他们应该但是如果你没有在别处正确地分配这些值,那么你可能再次绘制相同的东西,或者在前面的标签上绘制每个新的标签。 – 2011-04-27 21:01:44

回答

1

打印第一页后实际上很容易:只需将PrintPageEventArgs HasMorePages property设置为true即可。

棘手的部分是要知道何时通过设置HasMorePages为false,以阻止这一切。我通过将每个打印作业的数据存储在列表中并使用索引值跟踪我在此列表中的位置来完成此操作。

每次PrintPage事件触发时,我都会增加我在List中使用的索引,以便为PrintPage事件提供我想要打印的页面的详细信息,如果我在最后一个元素上,将HasMorePages设置为false。

void Document_Printed(object sender, PrintPageEventArgs e) { 
    // Get data for this page. 
    //xxx = pageDataList[indexValue]; 

    // Code to print stuff. 

    indexValue++; 
    e.HasMorePages = (pageDataList.Length == indexValue); 
} 

这种方法可以为您提供labelList我在你的代码中看到工作太,也许。既然你将要分批打印4份,你显然必须调整一下逻辑,但我认为你明白了。

+0

你知道,我看到了'HasMorePages'属性,但我只是*查看*它,而不是*设置*它。 – jp2code 2011-04-27 21:08:31

+0

大鼠。按[CR]提交注释,而不是突破下一行。 {叹气!}无论如何,我会试试看是否可以在8.5x11的一张纸上打印/显示多个小标签。 – jp2code 2011-04-27 21:09:48

+0

我不得不缩小我想要在屏幕上显示的页面数量,但否则这确实有效! – jp2code 2011-06-20 21:40:39