hi every body,
I've developed an application for small company that take care of inventory and purchases and want to share this part of this app about drawing an static table that span just one a4 page.
code below is code which I've included in the event handler of the PrintDocument component
//Image t = put your logo (left logo)
e.Graphics.DrawImage(t, new Point(10, 10));
//Image t1 = put your logo (right logo)
e.Graphics.DrawImage(t1, new Point(590, 9));
Pen blackPen = new Pen(Color.Black, 3);
// Create coordinates of points that define line.
int x1 = 0;
int y1 = 160;
int x2 = 1075;
int y2 = 160;
// Draw line to screen.
e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);
e.Graphics.DrawLine(blackPen, x1, y1 + 10, x2, y2 + 10);
//Font myFont = new Font(new FontFamily("Comic Sans MS"), 20, FontStyle.Underline);
Font myFont = new Font(new FontFamily("Arial"), 12, FontStyle.Bold);
Font myFont1 = new Font(new FontFamily("Arial"), 12, (FontStyle.Underline));
Brush myBrush = new SolidBrush(Color.Black);
Pen dots = new Pen((myBrush));
dots.DashStyle = DashStyle.Dash;
//draw name dashed space
e.Graphics.DrawLine(dots, 170, 240, 500, 240);
//Draw name label
e.Graphics.DrawString("Name : ", myFont, new SolidBrush(Color.Black), new PointF(60, 210));
//Draw InvoiceID label
e.Graphics.DrawString("Invoice ID : " + new Random().Next(5000).ToString(), myFont1, new SolidBrush(Color.Black), new PointF(650, 210));
//Draw Invoice date label
e.Graphics.DrawString("Date : " + DateTime.Now.ToShortDateString(), myFont1, new SolidBrush(Color.Black), new PointF(650, 250));
//Draw a name
e.Graphics.DrawString(textBox1.Text, myFont, myBrush, new PointF(160, 210));
//draw address dashed space
e.Graphics.DrawLine(dots, 170, 280, 500, 280);
//Draw adress label
e.Graphics.DrawString("Adress : ", myFont, new SolidBrush(Color.Black), new PointF(60, 250));
//Draw an address
e.Graphics.DrawString(textBox2.Text, myFont, myBrush, new PointF(160, 250));
//draw address dashed space
e.Graphics.DrawLine(dots, 170, 320, 500, 320);
//Draw adress label
e.Graphics.DrawString("Tel : ", myFont, new SolidBrush(Color.Black), new PointF(60, 290));
//Draw an address
e.Graphics.DrawString(textBox3.Text, myFont, myBrush, new PointF(160, 290));
Pen straight = new Pen(new SolidBrush(Color.Black));
//Draw table x,y,width,hight
e.Graphics.DrawRectangle(straight, 50, 320, 745, 730);
Rectangle[] InvoiceTitlesRectangles = new Rectangle[4];
////////////////////////////////////////////
InvoiceTitlesRectangles[0].Location = new Point(50, 320);
InvoiceTitlesRectangles[0].Height = 35;
InvoiceTitlesRectangles[0].Width = 400;
e.Graphics.DrawString("Description", myFont, myBrush, new PointF(60, 330));
///////////Description//////////
InvoiceTitlesRectangles[1].Location = new Point(450, 320);
InvoiceTitlesRectangles[1].Height = 35;
InvoiceTitlesRectangles[1].Width = 123;
e.Graphics.DrawString("Quantity", myFont, myBrush, new PointF(460, 330));
//////////Quantity/////////////
InvoiceTitlesRectangles[2].Location = new Point(573, 320);
InvoiceTitlesRectangles[2].Height = 35;
InvoiceTitlesRectangles[2].Width = 123;
e.Graphics.DrawString("Unit Price", myFont, myBrush, new PointF(583, 330));
//////////UnitPrice////////////
InvoiceTitlesRectangles[3].Location = new Point(696, 320);
InvoiceTitlesRectangles[3].Height = 35;
InvoiceTitlesRectangles[3].Width = 100;
e.Graphics.DrawString("Amount", myFont, myBrush, new PointF(706, 330));
//////////Amount////////////
e.Graphics.DrawRectangle(straight, InvoiceTitlesRectangles[0]);
e.Graphics.DrawRectangle(straight, InvoiceTitlesRectangles[1]);
e.Graphics.DrawRectangle(straight, InvoiceTitlesRectangles[2]);
e.Graphics.DrawRectangle(straight, InvoiceTitlesRectangles[3]);
//////////////////////////////////////////////////////////
//////////////////////draw the rest of rectangles//////////
Rectangle[] DescriptionRectangles = new Rectangle[var];
Rectangle[] QuantityRectangles = new Rectangle[var];
Rectangle[] UnitPriceRectangles = new Rectangle[var];
Rectangle[] AmountRectangles = new Rectangle[var];
Point[] DescriptionRectanlesPoints = new Point[var];
Point[] QuantityRectanlesPoints = new Point[var];
Point[] UnitPriceRectanlesPoints = new Point[var];
Point[] AmountRectanglesPoints = new Point[var];
/////////////////////////////Description
InitializationDescriptionRectanlesPoints(DescriptionRectanlesPoints, 50, 355);
ChangeDescriptionRectanlesLocations(DescriptionRectanlesPoints, DescriptionRectangles);
ChangeHeight(DescriptionRectangles, 35);
ChangeWidth(DescriptionRectangles, 400);
e.Graphics.DrawRectangles(straight, DescriptionRectangles);
float x33 = 60;
float y33 = 365;
for (int i = 0; i < Index; i++)
{
e.Graphics.DrawString(your description data array, myFont, myBrush, new PointF(x33, y33));
y33 += 35;
}
////////////////////////////Quantinty
InitializationDescriptionRectanlesPoints(QuantityRectanlesPoints, 450, 320 + 35);
ChangeDescriptionRectanlesLocations(QuantityRectanlesPoints, QuantityRectangles);
ChangeHeight(QuantityRectangles, 35);
ChangeWidth(QuantityRectangles, 123);
e.Graphics.DrawRectangles(straight, QuantityRectangles);
float x22 = 460;
float y22 = 365;
for (int i = 0; i < Index; i++)
{
e.Graphics.DrawString(your quantites data array, myFont, myBrush, new PointF(x22, y22));
y22 += 35;
}
////////////////////////////UnitPrice
InitializationDescriptionRectanlesPoints(QuantityRectanlesPoints, 450 + 123, 320 + 35);
ChangeDescriptionRectanlesLocations(QuantityRectanlesPoints, UnitPriceRectangles);
ChangeHeight(UnitPriceRectangles, 35);
ChangeWidth(UnitPriceRectangles, 123);
e.Graphics.DrawRectangles(straight, UnitPriceRectangles);
float x11 = 583;
float y11 = 365;
for (int i = 0; i < Index; i++)
{
e.Graphics.DrawString(your amount data array, myFont, myBrush, new PointF(x11, y11));
y11 += 35;
}
////////////////////////////Amount
InitializationDescriptionRectanlesPoints(AmountRectanglesPoints, 696, 355);
ChangeDescriptionRectanlesLocations(AmountRectanglesPoints, AmountRectangles);
ChangeHeight(AmountRectangles, 35);
ChangeWidth(AmountRectangles, 100);
e.Graphics.DrawRectangles(straight, AmountRectangles);
float x = 706;
float y = 365;
for (int i = 0; i < myOrder.CustomerIndex; i++)
{
e.Graphics.DrawString(myOrder.totalprice[i].ToString(), myFont, myBrush, new PointF(x,y));
y += 35;
}
e.Graphics.DrawString("Total", myFont, myBrush, new PointF(60,960));
double totalamount = GetTotalAmount();
e.Graphics.DrawString(totalamount.ToString(), myFont, myBrush, new PointF(706,960));
e.Graphics.DrawString("Signture", myFont1, myBrush, new PointF(50, 1000));
}
private double GetTotalAmount()
{
double x=0;
for (int i = 0; i < Index; i++)
{
x += Order.totalprice[i];
}
return x;
}
private void ChangeDescriptionRectanlesLocations(Point[] DescriptionRectanlesPoints, Rectangle[] DescriptionRectangles)
{
for (int i = 0; i < var; i++)
{
DescriptionRectangles[i].Location = DescriptionRectanlesPoints[i];
}
}
private void InitializationDescriptionRectanlesPoints(Point[] DescriptionRectanlesPoints, int p, int p_3)
{
for (int i = 0; i < DescriptionRectanlesPoints.Length; i++)
{
DescriptionRectanlesPoints[i] = new Point(p, p_3 );
p_3 = p_3 + 35;
}
}
private void ChangeWidth(Rectangle[] DescriptionRectangles, int p)
{
for (int i = 0; i < DescriptionRectangles.Length;i++ )
{
DescriptionRectangles[i].Width = p;
}
}
private void ChangeHeight(Rectangle[] DescriptionRectangles, int p)
{
for (int i = 0; i < DescriptionRectangles.Length; i++)
{
DescriptionRectangles[i].Height = p;
}
}