I'm creating a label with Graphics and printing it with PrintDocument, before i didn't had any problems because the information on the label was repeated multiple times so i would just set copies and everything would work fine.
But now each label has a different information so it freezes the ui because i have to loop PrintDocument.Print() multiple times instead of setting copies.
i tried:
Task t = new Task(() =>
{
for (int i = 0; i < Copies; i++)
{
printDocument1.Print();
Label_Copies++;
}
});
t.Start();
I'm drawing the label on printDocument1_PrintPage with this code:
int base_y = 5;
int base_x = 7;
int fontsize_bold = 8;
int fontsize_regular = 7;
RectangleF baserect = new RectangleF(base_x, base_y + 20, 0, 0);
SolidBrush whiteBrush = new SolidBrush(System.Drawing.Color.White);
SolidBrush blackBrush = new SolidBrush(System.Drawing.Color.Black);
string font = "Cabriolli";
Pen mypen = new Pen(blackBrush);
e.Graphics.FillRectangle(whiteBrush, new Rectangle(0, 0, 320, 130));
//e.Graphics.DrawRectangle(mypen, 1, 1, 225, 140);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawString("REMETENTE:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 10;
e.Graphics.DrawString("BETA COMERCIAL IMPORTADORA LTDA",
new Font(font, fontsize_bold, System.Drawing.FontStyle.Bold),
Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("DESTINATARIO:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 10;
e.Graphics.DrawString(Etiqueta_Destinatario, // *cliente
new Font(font, fontsize_bold, System.Drawing.FontStyle.Bold),
Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("NOTA FISCAL:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x, base_y + 3, 0, 0));
e.Graphics.DrawString(Etiqueta_Nota, // *nota
new Font(font, 10, System.Drawing.FontStyle.Bold),
Brushes.Black, new RectangleF(base_x + 68, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("PEDIDO:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x, base_y + 3, 0, 0));
e.Graphics.DrawString(Etiqueta_Pedido, // *pedido
new Font(font, 10, System.Drawing.FontStyle.Bold),
Brushes.Black, new RectangleF(base_x + 43, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("Etiqueta numero:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x + 140, base_y - 20, 0, 0));
if (Copias.Checked)
{
e.Graphics.DrawString(Etiqueta_Copias.ToString(),
new Font(font, 15, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}
else
{
e.Graphics.DrawString(Etiqueta_Copia.ToString(),
new Font(font, 15, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}
e.Graphics.DrawString("VOLUMES:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x, base_y + 3, 0, 0));
e.Graphics.DrawString(peças_textbox.Text, // *quantidade
new Font(font, 10, System.Drawing.FontStyle.Bold),
Brushes.Black, new RectangleF(base_x + 53, base_y, 0, 0));
base_y += 25;
e.Graphics.DrawString("TRANSPORTADORA:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 14;
e.Graphics.DrawString(nota_transportadora_combobox.Text, // *transportadora
new Font(font, 10, System.Drawing.FontStyle.Bold),
Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
whiteBrush.Dispose();
blackBrush.Dispose();
mypen.Dispose();
but it doesn't work is there a way to fix it?
-the information comes from the ui
-500~ labels
-the only information that changes from label to label in those copies is
if (Copias.Checked)
{
e.Graphics.DrawString(Etiqueta_Copias.ToString(),
new Font(font, 15, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}
else
{
e.Graphics.DrawString(Etiqueta_Copia.ToString(),
new Font(font, 15, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}
its basically a counter for the label number...
As I see it, your code, if we ignore the printer's spooling side, is pure WinForms UI.
Since that the work is multiplied, let's say by a 100 copies, it becomes too much Continous UI.
Tasking isn't helping you because I think the printing side is "fire and forget", so even if the printing took 30 seconds per sticker, the printing side would return immediately, and let the UI start it's heavy work on the next sticker, almost immediately after it finishes the last one.
let's say the UI has to work 1 second per sticker, you are looking at a 100 seconds UI blocking...
My suggestion:
Rely on the fact that the printing behind takes 20 seconds: Launch the printing using a timer instead of a loop. Set it to 5, 10 or 20 seconds (match with your printer's speed). Each time you will fire the timer, it will UI process 1 sticker only, and then rest in peace for 5, 10, or 20 seconds...
Related
I am having problems creating a protected variable that can be access and updated by an external method.
In other words, I have got a print method that prints multiple pages.
I needs an external variable to initialize this method and keep track of the pages.
Is it possible someone could get me started in coding this class? as I knowledge on OOP is limited
Below is the print method
Thanks in advance
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
int ypos = 78;
Font f1 = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.LightSlateGray);
graphics.FillRectangle(brush, new Rectangle(10, 10, 770, 50));
Pen blackpen = new Pen(Color.Black);
Pen graypen = new Pen(Color.LightGray);
Pen redpen = new Pen(Color.Red);
graphics.DrawRectangle(blackpen, new Rectangle(10, 10, 770, 50));
Brush B = new SolidBrush(listView1.ForeColor);
graphics.DrawLine(blackpen, 10, 10, 10, 1132);
graphics.DrawString("FORENAME", f1, Brushes.Black, new Point(20, 25));
graphics.DrawLine(blackpen, 130, 10, 130, 1132);
graphics.DrawString("SURNAME", f1, Brushes.Black, new Point(140, 25));
graphics.DrawLine(blackpen, 290, 10, 290, 1132);
graphics.DrawString("EXT.", f1, Brushes.Black, new Point(300, 25));
graphics.DrawLine(blackpen, 380, 10, 380, 1132);
graphics.DrawString("JOB TITLE", f1, Brushes.Black, new Point(410, 25));
graphics.DrawLine(blackpen, 780, 10, 780, 1132);
int[] X = { 15, 140, 300, 390, 720 };
int Y = 60;
f1 = listView1.Font;
for (int I = 0; I < listView1.Items.Count; I++){
for (int J = 0; J < listView1.Items[I].SubItems.Count - 1; J++){
graphics.DrawString(listView1.Items[I].SubItems[J].Text, f1, B, X[J], Y);
}
}
Y += f1.Height;
graphics.DrawLine(graypen, 10, ypos, 780, ypos);
ypos = ypos + 17;
if (ypos > 1132){
e.HasMorePages = true;
return;
}
graphics.Dispose();
}
Try below function, and call print event
public void PrintDocument()
{
try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.printDocument1_PrintPage);
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I am getting this error:
TicketingSystem' does not contain a definition for 'DVPrintDocument_PrintPage' and no extension method could be found.
I am trying to organize my code by putting it in separate classes. I made a new class called Printing.cs which has this code:
public void DVPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap r3tsLogo = Properties.Resources.rt3slogo;
Image image1 = r3tsLogo; //image 1 is r3tsLogo
e.Graphics.DrawImage(image1, 350, 0, image1.Width, image1.Height);
// e.Graphics.DrawString("Employee Name:" + employee.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 200)); //Put to bottom of paper
e.Graphics.DrawString("Address:", new Font("Impact", 12, FontStyle.Regular), Brushes.Black, new Point(300, 90));//change the new point to put text on different part of paper.
e.Graphics.DrawString("North Building", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(370, 94)); //This line of code connects to Code line 151
e.Graphics.DrawString("Email:", new Font("Impact", 12, FontStyle.Regular), Brushes.Black, new Point(300, 120));//change the new point to put text on different part of paper.
e.Graphics.DrawString("email#email.com", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(350, 124)); //This line of code connects to Code line 154
e.Graphics.DrawString("Date: " + DateTime.Now, new Font("Arial", 13, FontStyle.Regular), Brushes.Black, new Point(300, 150));
e.Graphics.DrawString(ticketingSystem.dashes.Text, new Font("Arial", 12), Brushes.Black, new Point(0, 160));
e.Graphics.DrawString("CUSTOMER INFORMATION:", new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(275, 180));
e.Graphics.DrawString("Customer Name:" + ticketingSystem.nameTxtB.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 200));
e.Graphics.DrawString("Email:" + ticketingSystem.emailTxtBox.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 230));
e.Graphics.DrawString("Cell Number:" + ticketingSystem.cellNumberTxtB.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 260));
if(ticketingSystem.studentRadioB.Checked == true)
{
e.Graphics.DrawString("Position:" + ticketingSystem.studentRadioB.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 290));
}
else if (ticketingSystem.teacherRadioB.Checked == true)
{
e.Graphics.DrawString("Position:" + ticketingSystem.teacherRadioB.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 290));
}
e.Graphics.DrawString(ticketingSystem.dashes.Text, new Font("Arial", 12), Brushes.Black, new Point(0, 350));
//--------------------------------------Device Information -------------------------------------------------------------------------------------
int y = 493;
e.Graphics.DrawString("DEVICE INFORMATION:", new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(286, 373));
e.Graphics.DrawString("Device Type:" + ticketingSystem.devices.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 403));
if (ticketingSystem.devices.Visible == true & ticketingSystem.devices.Text == "Console")
{
e.Graphics.DrawString("Type of Console:" + ticketingSystem.consoleTextBox.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 433));
}
if (ticketingSystem.comboBox3.Visible == true) //iphone selection box is visible then show selected model on print document
{
e.Graphics.DrawString("Type of Phone:" + ticketingSystem.comboBox3.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 433));
}
e.Graphics.DrawString("Service Type:" + ticketingSystem.serviceDesc.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 463));
using (var arial15 = new Font("Arial", 15, FontStyle.Regular))
{
y += DrawWrapped("Price: $" + Convert.ToInt32(cPrice), arial15, new Point(50, 493), e.PageBounds.Size, e.Graphics);
}
using (var arial15 = new Font("Arial", 15, FontStyle.Regular))
{
y += DrawWrapped("Description:" + ticketingSystem.description4Repair.Text, arial15, new Point(50, y), e.PageBounds.Size, e.Graphics);
}
}
Now when I comment everything out in my Main.cs I get the same error:
TicketingSystem' does not contain a definition for 'DVPrintDocument_PrintPage' and no extension method could be found.
All I want to do is tell my designer.cs to look for the code in Printing.cs.
I have a problem with my c# application, because i need to print with dot matrix printer "Olivetti PR2 Plus" using X and Y Position coordinates, like VB6 did. the problem is if the string have more than 20 characters aprox, the printer is very slow and i need to print fast.
I have the following code for testing purposes and have the problem.
private void btnPrint_Click(object sender, EventArgs e)
{
Document = new PrintDocument();
Document.PrintPage += Document_PrintPage;
Document.PrinterSettings.PrinterName = ((ICollection)PrinterSettings.InstalledPrinters).OfType<string>().Where(p => p.ToUpper().Contains("OLIVETTI")).First().ToString();
Document.Print();
}
private void Document_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("asdfasdfasdfasdasdffasdfasdfasdf", new Font("Consolas", 8), Brushes.Black, 20, 0, new StringFormat());
e.Graphics.DrawString("asdfasdfasdasdaasdfsdfffasdfasdf", new Font("Consolas", 8), Brushes.Black, 20, 10, new StringFormat());
e.Graphics.DrawString("asdfasdfaasdfsasdfsadfdfasdfasdf", new Font("Consolas", 8), Brushes.Black, 20, 20, new StringFormat());
e.Graphics.DrawString("asfdasdfsadfasasdfasdfdasdfasdff", new Font("Consolas", 8), Brushes.Black, 20, 30, new StringFormat());
e.Graphics.DrawString("asfdasdsdafasadfsasdfdasdfasdfff", new Font("Consolas", 8), Brushes.Black, 20, 40, new StringFormat());
e.Graphics.DrawString("asdfaasdsadfsadffsadfsdassadfdff", new Font("Consolas", 8), Brushes.Black, 20, 50, new StringFormat());
e.Graphics.DrawString("asfdasdfsdafsadfsadfasasasdfdfdf", new Font("Consolas", 8), Brushes.Black, 20, 60, new StringFormat());
e.Graphics.DrawString("asdfasadfassadfdfssadfassadfdfdf", new Font("Consolas", 8), Brushes.Black, 20, 70, new StringFormat());
e.Graphics.DrawString("asfdasadfasadfsdfssadasasdffdfdf", new Font("Consolas", 8), Brushes.Black, 20, 80, new StringFormat());
}
I can't print in RAW "DOS" mode because the source of the data to print have specific coordinates for match with the paper formats. ¿any sugestion to improve the perfomance of the printer?
In my Windows application, I print multiple items in my PrintPriview control but when the items are more than 25, it doesn't go the next page to print the rest of the items. I know that we need to use e.Hasmorepages = true but I can't figure out how to use it correctly. Please help.
Screen shot -
Click here to see screen shot
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp = Properties.Resources.logo2;
Image image = bmp;
e.Graphics.DrawImage(image, 0, 0, image.Width, image.Height);
e.Graphics.DrawString("Date: " + DateTime.Now.ToShortDateString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 160));
e.Graphics.DrawString("Client Name: " + ClientNameTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 190));
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 235));
e.Graphics.DrawString("Item Name", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(30, 255));
e.Graphics.DrawString("Quantity", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(380, 255));
e.Graphics.DrawString("Unit Price (£)", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(510, 255));
e.Graphics.DrawString("Total Price (£)", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(660, 255));
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 270));
int yPos = 295;
foreach (var i in shoppingCart)
{
e.Graphics.DrawString(i.ItemName, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(30, yPos));
e.Graphics.DrawString(i.Quantity.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(400, yPos));
e.Graphics.DrawString(i.UnitPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos));
e.Graphics.DrawString(i.TotalPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(700, yPos));
yPos += 30;
}
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, yPos));
e.Graphics.DrawString("Total Amount: £" + TotalAmountTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 30));
e.Graphics.DrawString("Sales Tax (16%): £" + SalesTaxTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 60));
e.Graphics.DrawString("Total To Pay: £" + TotalToPayTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 90));
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, yPos + 120));
}
Thanks in advance.
Regards,
Yes, you need to
a) watch the y value
b) set HasMorePages to true when it is over a maximum page height value
c) leave the printpage event.
It'll be called again if needed, ie when you either print or preview the next page..
You also need to keep track of the cart items you have printed in a class level variable; replace the foreach by a for loop storing/using that item number! Break out of the loop and later also return from the event when y is larger than the maximum that will fit on your page!
Also design for repeating the headers, but you got that right I think! Finally another class level variable should keep track of the page numbers!
I fill up a datagridview and after that I print out the list from there some extra fix string fields. The first two printing finishing fast but after that 3-4 is slowing down very much(sometimes it freezing).
I think some buffer or something fills up and i have to empty it but how? If I tried to print via pdf printer the first file size is normal ~200KB second is 1MB and the third is 4-6MB the fourth is so big it is freezing.
It is only one page maximum two page long list. Can someone help me to find the problem and offer a solution or more?
private void button1_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
Print button
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp = Properties.Resources.ugro;
Image newImage = bmp;
e.Graphics.DrawImage(newImage, 700, 20, 100, 100);
Bitmap bmp1 = Properties.Resources.line_1;
Image newImage1 = bmp1;
e.Graphics.DrawImage(newImage1, 20, 80, 700, 1);
e.Graphics.DrawString("Text", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(280, 20));
e.Graphics.DrawString(lbl_datum.Text, new Font("Arial", 8, FontStyle.Bold), Brushes.Black, new Point(20, 20));
// e.Graphics.DrawString("Text:", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(20, 100));
//e.Graphics.DrawString(txt_BID.Text, new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(160, 80));
//e.Graphics.DrawString(txt_ID.Text, new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(160, 100));
DataGridViewCell cell = null;
foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells)
{
cell = selectedCell;
break;
}
if (cell != null)
{
DataGridViewRow row = cell.OwningRow;
}
e.Graphics.DrawString("Name", new Font("Arial", 10, FontStyle.Bold), Brushes.Black, new Point(40, 60));
e.Graphics.DrawString("Height(cm)", new Font("Arial", 10, FontStyle.Bold), Brushes.Black, new Point(150, 60));
e.Graphics.DrawString("Weight(kg)", new Font("Arial", 10, FontStyle.Bold), Brushes.Black, new Point(280, 60));
e.Graphics.DrawString("Text(cm)", new Font("Arial", 10, FontStyle.Bold), Brushes.Black, new Point(410, 60));
int height = 0;
//int width = 0;
//e.Graphics.FillRectangle(Brushes.DarkGray,new Rectangle(100,100,dataGridView1.Columns[0].Width,dataGridView1.Rows[0].Height));
// e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText.ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Rectangle(420, 80, dataGridView1.Columns[0].Width,dataGridView1.Rows[1].Height));
height = 80;
while (i < dataGridView1.Rows.Count)
{
if (height > e.MarginBounds.Height)
{
e.HasMorePages = true;
return;
}
height += 20;
//e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(20, height, dataGridView1.Columns[0].Width, 20));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(20, height, dataGridView1.Columns[0].Width+50, 40));
string sign = String.Format("_____________");
e.Graphics.DrawString(sign, dataGridView1.Font, Brushes.Black, new Rectangle(150, height, dataGridView1.Columns[0].Width + 100, 40));
e.Graphics.DrawString(sign, dataGridView1.Font, Brushes.Black, new Rectangle(280, height, dataGridView1.Columns[0].Width + 100, 40));
e.Graphics.DrawString(sign, dataGridView1.Font, Brushes.Black, new Rectangle(410, height, dataGridView1.Columns[0].Width + 100, 40));
i++;
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
i = 0;
}
Which variable is causing my problem?