printing on a document c# .net - c#

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

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.

How to show multiple pages in PrintPreview control in c#

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!

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?

How to fit Graphics.Draw in a printed page?

I have next code which creates a print page with data from datagridview.
Font print10B = new Font("Lucida Console", 10, FontStyle.Bold);
Font print8B = new Font("Arial", 9, FontStyle.Regular);
Font print6B = new Font("Arial", 8, FontStyle.Regular);
e.Graphics.DrawString(" NOTA DE PLATA",print10B,Brushes.Black,10,10);
e.Graphics.DrawString("-----------------------------------------", new Font(dataGridView1.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, 10, 70);
e.Graphics.DrawString("Produs | Cant | Pret | Total ", print8B, Brushes.Black, 10, 100);
int height = 0;
int x = 10;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["DenumireProdus"].Value.ToString(), print6B, Brushes.Black, x, 120+height);
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["produs_cantitate"].Value.ToString(), print6B, Brushes.Black, x+70, 120 + height);
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["produs_pret"].Value.ToString() , print6B, Brushes.Black, x+105, 120 + height);
e.Graphics.DrawString(dataGridView1.Rows[i].Cells["produsvaloare"].Value.ToString(), print6B, Brushes.Black, x + 135, 120 + height);
height += 20;
}
e.Graphics.DrawString("-----------------------------------------", new Font(dataGridView1.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, 10, dataGridView1.Height);
e.Graphics.DrawString("Total: " + textBox1.Text+" RON", print10B, Brushes.Black,40, dataGridView1.Height + 10);
Until here:
e.Graphics.DrawString("-----------------------------------------", new Font(dataGridView1.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, 10, dataGridView1.Height);
e.Graphics.DrawString("Total: " + textBox1.Text+" RON", print10B, Brushes.Black,40, dataGridView1.Height + 10);
The printer does not print the above part ( the last 2 drawstrings). Why?
The printer page size is Proportional A4 (48 mm x 68 mm)
This isn't a direct answer to your question, but it may save you a lot of time.
There's code and a component on Codeproject.com that allows you to print/print preview a DataGrid. We use it in a couple of apps, and it's ben a life saver.
http://www.codeproject.com/Articles/36488/DataGridView-Print-Print-Preview-Solution-Part-I

Categories

Resources