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: 开发者_如何学GopageRect 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.
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.
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);
}
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.
精彩评论