Related
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 am trying to print a invoice from application for my dads business, i am using the following code to print the invoice.
My attempt is below:
{
int y = 470;
while (i < dataGridView1.RowCount)
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
y = y + 20;
if (y >= pageHeight)
{
e.HasMorePages = true;
y = 0;
i++;
return;
}
else
{
e.HasMorePages = false;
}
i++;
}
}
i here is a global variable
private int i = 0;
when i click on preview button i get the output as expected but when i print it on paper only the content in the while loop is not printed. i tried using a local variable instead j of global variable as shown below and it worked.
for (int j=0; j < dataGridView1.RowCount; j++)
{
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
y = y + 20;
}
but i don't know how to add new pages if the item to be included exceeds the page height. please some help.
after iterating through the code step by step i found that the code tp print the preview is run again while printing the form. so i just had to initialize the i value to 0 so that it starts printing from the beginning.
here is the code which solved it
int j;
for (j=i; j < dataGridView1.RowCount && y<e.MarginBounds.Bottom; j++)
{
e.Graphics.DrawString(e.MarginBounds.Bottom.ToString(), DefaultFont, Brushes.Black, new Point(100, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(220, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(770, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(620, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(670, y));
e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(720, y));
y = y + 20;
}
if (j < dataGridView1.RowCount)
{
e.HasMorePages = true;
i=j;
}
else
{
e.HasMorePages = false;
i = 0;
}
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?
I have a Datagridview it has a checkboxcolumn!
when the data is loaded for standard all rows get checked! but i need to uncheck some of them and them, send it's values to a var and print it!
is it possible?
Exemple:
--------------------------------------------------------------
ColumnCheckBox | Column1 | Column2 | Column3
--------------------------------------------------------------
checked | 1251000014 | portraitx | U$ 125.00
checked | 1251000021 | notebooky | U$ 899.96
unchecked | 1265888512 | tabletx | U$ 899.96
checked | 1251444251 | iphoness | U$ 566.26
unchecked | 1255222142 | opticalreader | U$ 99.99
I want to get the values of the CHECKED Rows and send to a var and the print it! the main is ...how to send this values to a var?
thankx in advance!
Print all checked rows in a page:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
//Find all checked rows
var allCheckedRows = this.myDataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells[0].Value == true)
.ToList();
//create a stringBuilder that will contain the string for all checked rows
var builder = new StringBuilder();
//For each checked row, create string presentation of row and add to output stringBuilder
allCheckedRows.ForEach(row =>
{
//Create an array of all cell value of a row to then concatenate them using a separator
var cellValues = row.Cells.Cast<DataGridViewCell>()
.Where(cell => cell.ColumnIndex > 0)
.Select(cell => string.Format("{0}", cell.Value))
.ToArray();
//Then joiconcatenate values using ", " as separator, and added to output
builder.AppendLine(string.Join(", ", cellValues));
//Here instead of adding them to the stringBuilder, you can add int to another list.
});
//Print the output string
e.Graphics.DrawString(builder.ToString(),
this.myDataGridView.Font,
new SolidBrush(this.myDataGridView.ForeColor),
new RectangleF(0, 0, this.printDocument1.DefaultPageSettings.PrintableArea.Width, this.printDocument1.DefaultPageSettings.PrintableArea.Height));
}
Output:
1251000014, portraitx, U$ 125.00
1251000021, notebooky, U$ 899.96
1251444251, iphoness, U$ 566.26
Print each checked row in a separate page:
private int currentPrintingRowIndex = 0;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var allCheckedRows = this.myDataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells[0].Value == true)
.ToList();
if (allCheckedRows.Count > currentPrintingRowIndex)
{
var builder = new StringBuilder();
var currentCheckedRow = allCheckedRows[currentPrintingRowIndex];
var cellValues = currentCheckedRow.Cells.Cast<DataGridViewCell>()
.Where(cell => cell.ColumnIndex > 0)
.Select(cell => string.Format("{0}", cell.Value))
.ToArray();
builder.AppendLine(string.Join(", ", cellValues));
e.Graphics.DrawString(builder.ToString(),
this.myDataGridView.Font,
new SolidBrush(this.myDataGridView.ForeColor),
new RectangleF(0, 0, this.printDocument1.DefaultPageSettings.PrintableArea.Width, this.printDocument1.DefaultPageSettings.PrintableArea.Height));
currentPrintingRowIndex += 1;
e.HasMorePages = allCheckedRows.Count > currentPrintingRowIndex;
}
}
Output:
A document with 3 pages:
Page1 content: 1251000014, portraitx, U$ 125.00
Page2 content: 1251000021, notebooky, U$ 899.96
Page3 content: 1251444251, iphoness, U$ 566.26
****THIS IS THE FINAL CODE WITH THE HELPFUL CODE ABOVE FROM Reza Aghaei ****
private int currentPrintingRowIndex = 0;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Font fsystem = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
Font fdatabd = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel);
Font fdatabdstrikeout = new Font("Arial", 18, FontStyle.Strikeout, GraphicsUnit.Pixel);
Font fdiag = new Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Pixel);
Font fbarra = new Font("C39HrP24DhTt", 30, FontStyle.Regular, GraphicsUnit.Pixel);
Font fdesc = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Pixel);
var allCheckedRows = this.dgv1.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells[0].Value == true)
.ToList();
if (allCheckedRows.Count > currentPrintingRowIndex)
{
var builder = new StringBuilder();
var currentCheckedRow = allCheckedRows[currentPrintingRowIndex];
var cellValues = currentCheckedRow.Cells.Cast<DataGridViewCell>()
.Where(cell => cell.ColumnIndex > 0)
.Select(cell => string.Format("{0}", cell.Value))
.ToArray();
builder.Append(string.Join(",", cellValues));
// begin of the aditional implementation
string ldp = builder.ToString();
string[] cadaum = ldp.Split(',');
var cents = cadaum[3].Substring(0, 2);
var descr = cadaum[1].ToString();
if (descr.Length >= 30)
{
var descr2 = descr.Substring(0, 30);
var descr3 = descr.Substring(30);
// end of the aditional implementation
// begin modification
//UPPER LEFT SIDE
e.Graphics.DrawString("TRADEMARK", fsystem, Brushes.Black, 45, 8);
e.Graphics.DrawString("COMPANY'S NAME", fdatabd, Brushes.Black, 10, 30);
e.Graphics.DrawString("ANY OTHER INFORMATION", fdatabd, Brushes.Black, 10, 41);
e.Graphics.DrawString("made in china", fdatabd, Brushes.Black, 10, 53);
if (cadaum[1].Length >= 30)
{
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 10, 77);
}
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 10, 87);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 8, 105);
// UPPER RIGHT SIDE
e.Graphics.DrawString("TRADEMARK", fsystem, Brushes.Black, 205, 8);
e.Graphics.DrawString("COMPANY'S NAME", fdatabd, Brushes.Black, 170, 30);
e.Graphics.DrawString("ANY OTHER INFORMATION", fdatabd, Brushes.Black, 170, 41);
e.Graphics.DrawString("made in china", fdatabd, Brushes.Black, 170, 53);
if (cadaum[1].Length >= 30)
{
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 170, 77); }
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 170, 87);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 168, 105);
// CUTTING REFERENCE
e.Graphics.DrawString("---------------------------------------------------------------------------------------------", fdatabd, Brushes.Black, 1, 147);
//LOWER LEFT SIDE
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 10, 155);
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 10, 165);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 8, 177);
e.Graphics.DrawString("Price:", fdatabd, Brushes.Black, 10, 208);
e.Graphics.DrawString(cadaum[2].ToString() +"," + cents.ToString(), fdatabd, Brushes.Black, 80, 208);
e.Graphics.DrawString("TRADEMARK", fsystem, Brushes.Black, 45, 220);
// LOWER RIGHT SIDE
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 170, 155);
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 170, 165);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 168, 177);
e.Graphics.DrawString("Price:", fdatabd, Brushes.Black, 170, 208);
e.Graphics.DrawString(cadaum[2].ToString() + "," + cents.ToString(), fdatabd, Brushes.Black, 240, 208);
e.Graphics.DrawString("TRADEMARK", fsystem, Brushes.Black, 205, 220);
// end modification
currentPrintingRowIndex += 1;
e.HasMorePages = allCheckedRows.Count > currentPrintingRowIndex;