System.Drawing & e.HasMorePages problems - c#

I have several days trying to solve this big problem I have in C #:
I am trying to Print 21 Articles (Bill) Ticket Format, but the paper roll has a limit and occupied several pages divided print but I can not make it do the jump from page to print the article # 17 and continue to another page with the # 18, please help..
private void DrawItems(System.Drawing.Printing.PrintPageEventArgs e)
{
int linesprinted = 0;
int linesperpage = 17;
int numberitems = items.Count; //21
//numberitems / linespage = 1.23 = 2 Pages True :)
if (linesprinted <= linesperpage)
{
linesprinted++;
e.HasMorePages = false;
}
else {
linesprinted=0;
e.HasMorePages = true;
}
//print items
OrderItem ordIt = new OrderItem('?');
gfx.DrawString("C/P DESCRIPCION TOTAL", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
DrawEspacio();
gfx.DrawString(DottedLine(), new Font(fontName, 9, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
count++;
foreach (string item in items)
{
String ItemCantidad = ordIt.GetItemCantidad(item);
String ItemPrice = ordIt.GetItemPrice(item);
Int16 not_equal = 0;
gfx.DrawString(ItemCantidad + " x", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
line = ordIt.GetItemUnitPrice(item);
line = AlignRightText(line.Length) + line;
gfx.DrawString(" " + line, printFont, myBrush, leftMargin, YPosition(), new StringFormat());
string name = ordIt.GetItemName(item);
leftMargin = 0;
if (name.Length > maxCharDescription)
{
int currentChar = 0;
int itemLenght = name.Length;
while (itemLenght > maxCharDescription)
{
line = ordIt.GetItemName(item);
gfx.DrawString(" " + line.Substring(currentChar, maxCharDescription), new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
count++;
not_equal++;
if (not_equal == 1)
{
gfx.DrawString(ItemPrice, new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
}
currentChar += maxCharDescription;
itemLenght -= maxCharDescription;
}
line = ordIt.GetItemName(item);
gfx.DrawString(" " + line.Substring(currentChar, line.Length - currentChar), new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
count++;
gfx.DrawString("-----", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
count++;
}
else
{
gfx.DrawString(" " + ordIt.GetItemName(item), new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
count++;
gfx.DrawString(ItemPrice, new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
count++;
gfx.DrawString("-----", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
count++;
}
} //end foreach
leftMargin = 0;
gfx.DrawString(DottedLine(), new Font(fontName, 9, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
DrawEspacio();
} //end function

I think that you are not doing it right. It should go something like this:
private void MyPrintDocument_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
bool more = DrawItems(e.Graphics);
if (more == true)
e.HasMorePages = true;
}
So, after PrintDocument Print event, you call your method to draw items, and it tracks last painted item in variable outside of the method, so when it is called again to know where to start from. And when it comes to item that should go to next page, it returns true.

Related

Coding a class with protected variables to be access by another method

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);
}
}

"Does Not Contain Definition and No Extension Method Found " But Exists in another Class

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.

printing on a document c# .net

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;
}

C# printing issue - printing datagridview data - 2,3,4 times the printing slowing down --Windows Form

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?

Dot matrix printer: Orientation changes to portrait for odd pages

Printer prints every odd page in portrait orientation.
private void Form1_Loaded(object sender, EventArgs e)
{
PaperSize ps = new PaperSize("Custom", 700, 325);
printDocument.DefaultPageSettings.PaperSize = ps;
label1.Text = Convert.ToString(printDocument.DefaultPageSettings.PaperSize);
//printDocument.DefaultPageSettings.Landscape = true;
printDocument.PrinterSettings.DefaultPageSettings.Landscape = true;
}
private void DrawGridBody(Graphics g, int y_value)
{
int x_value;
for (int i = 0; (i < NUM_ROWS_PER_PAGE) && ((i+m_printedRows) < ((DataTable)dataGridView.DataSource).Rows.Count); ++i)
{
DataRow dr = ((DataTable)dataGridView.DataSource).Rows[i + m_printedRows];
x_value = 0;
string text = dr.ItemArray.ElementAt(0).ToString();
g.DrawString(text+"**", this.Font, Brushes.Black, (float)x_value, (float)y_value + 10f);
y_value += ROW_HEIGHT;
g.DrawString("page " + Convert.ToString(m_printedRows), new Font("Lucidia Console", 14, FontStyle.Bold), Brushes.Black, 60, 10);
g.DrawString("1 " + dr.ItemArray.ElementAt(0).ToString(), new Font("Lucidia Console", 10), Brushes.Black, new Point(370, 20));
g.DrawString("2 " + dr.ItemArray.ElementAt(0).ToString(), new Font("Lucidia Console", 10), Brushes.Black, new Point(390, 46));
g.DrawString("3 " + dr.ItemArray.ElementAt(0).ToString(), new Font("Lucidia Console", 10), Brushes.Black, new Point(30, 40));
g.DrawString(dr.ItemArray.ElementAt(1).ToString(), new Font("Lucidia Console", 10), Brushes.Black, new Point(70, 180));
g.DrawString(dr.ItemArray.ElementAt(2).ToString() + " " + dr.ItemArray.ElementAt(3).ToString(), new Font("Lucidia Console", 10), Brushes.Black, new Point(70, 202));
g.DrawString(dr.ItemArray.ElementAt(4).ToString(), new Font("Lucidia Console", 10), Brushes.Black, new Point(70, 224));
g.DrawString(dr.ItemArray.ElementAt(5).ToString(), new Font("Lucidia Console", 10), Brushes.Black, new Point(550, 285));
g.DrawString("***********", new Font("Lucidia Console", 10), Brushes.Black, new Point(550, 300));
g.DrawString("***********", new Font("Lucidia Console", 10), Brushes.Black, new Point(550, 315));
}
m_printedRows += NUM_ROWS_PER_PAGE;
printDocument.PrinterSettings.DefaultPageSettings.Landscape = true;
printDocument.DefaultPageSettings.Landscape = true;
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int rowPosition = SPACE_ABOVE_HEADER;
rowPosition += ROW_HEIGHT;
// draw each row
DrawGridBody(e.Graphics, rowPosition);
// see if there are more pages to print
if (MoreRowToPrint())
e.HasMorePages = true;
else
m_printedRows = 0;
}
private bool MoreRowToPrint()
{
return ((DataTable)dataGridView.DataSource).Rows.Count > m_printedRows;
}

Categories

Resources