C# WinForm PrintPreviewDialog:打印预览以每页显示多个标签
我有一个用于创建标签的 WinForm。
它调用 PrintPreviewDialog
并使用 PrintDocument
的 PrintPageEventHandler
中的 PrintPageEventArgs
显示信息。
void Document_Printed(object sender, PrintPageEventArgs e) {
// Code goes here
}
如果标签是一个小地址标签,大小为 8.5x11 字母,我想查看适合该 PageSettings.PaperSize 的标签数量,而不是在 PrintPreviewDialog 中看到单个标签。
示例:假设四 (4) 个项目适合所选介质(Avery 打印机标签或任何介质)。
如果我的最终用户指定要打印 1 到 4 份,我希望我的打印预览对话框显示所有副本。
如果我的最终用户指定了超过四 (4) 个项目,则打印预览对话框应显示多个页面(我以前从未处理过多个页面)。
我可以调整数据大小以适合标签大小,但我不知道如何让 PrintPageEventHandler
在 PrintPreviewDialog
上显示超过 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: 我需要一种在一页上打印 2 个或更多文档的方法。到目前为止,还没有任何东西能够解决这个关键点。这就是我需要的答案。
I have a WinForm for creating Labels.
It calls a PrintPreviewDialog
and displays information using the PrintPageEventArgs
in the PrintDocument
's PrintPageEventHandler
.
void Document_Printed(object sender, PrintPageEventArgs e) {
// Code goes here
}
If the Label is a small address label going to an 8.5x11 Letter, instead of seeing a single Label in the PrintPreviewDialog
, I want to see the number of Labels that fit on that PageSettings.PaperSize
.
Example: Say four (4) items fit on the selected media (Avery Printer Label or anything).
If my End User specifies from 1 to 4 Copies to be printed, I want my Print Preview Dialog to display all copies.
If my End User specifies more than four (4) items, the Print Preview Dialog should show multiple pages (I've never tackled multiple pages before).
I can size my data to fit my Label size, but I do not know how to get my PrintPageEventHandler
to display more than 1 Label on the PrintPreviewDialog
.
Could someone show me how this is done? How do I display and print multiple Labels (Documents?) per Sheet?
EDIT: Here is my code, which is based on 2 RectangleF objects: pageRect and 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);
}
}
}
}
EDIT 2: I need a way to print 2 or more documents on 1 page. Nothing has addressed this key point as of yet. That's the answer I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个页面之后打印页面实际上非常容易:只需设置 PrintPageEventArgs HasMorePages 属性 设置为 true。
棘手的部分是知道何时通过将
HasMorePages
设置为 false 来停止此操作。我通过将每个打印作业的数据存储在列表中并使用索引值来跟踪我在此列表中的位置来完成此操作。每次触发 PrintPage 事件时,我都会增加我在列表中使用的索引,以向 PrintPage 事件提供我想要打印的页面的详细信息,如果我在最后一个元素上,我会将 HasMorePages 设置为 false。
这种方法也适合您,也许可以使用我在您的代码中看到的 labelList。由于您要以四份为一组进行打印,因此显然必须稍微调整逻辑,但我认为您已经明白了。
Printing pages after the first is actually really easy to do: just set the PrintPageEventArgs HasMorePages property to true.
The tricky part is knowing when to stop this by setting
HasMorePages
to false. I've done this by storing the data for each print job in a List and using an index value to track where I am on this list.Each time the PrintPage event fires I increment my index which I use in my List to feed the PrintPage event the particulars of the page I want to print, and if I'm on the last element I set HasMorePages to false.
This approach can work for you too, perhaps with the labelList I see in your code. Since you're going to be printing in batches of four you'll obviously have to adjust the logic a bit but I think you get the idea.