Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to print invoice in which Particular,HSNCode and Unit, Batch, Exp and so on.. these items are in my invoice but when particular text size is like "Metaformin Hydrochloride Sustained" or more than this it overlap with HSNCode and Unit,
how to adjust text size in a "Particular Column"


What I have tried:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
       {
           Image image = Resources.ppreview;
           e.Graphics.DrawImage(image, 0, 0, image.Width, image.Height);

           Image img = Resources.printlogo2;
           e.Graphics.DrawImage(img, 510, 01, image.Width, image.Height);




           e.Graphics.DrawString("Name: " + textBox2.Text.Trim(), new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(35, 220));

           e.Graphics.DrawString("Mobile No : " + textBox3.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(35, 250));
           //Bill
           e.Graphics.DrawString("Bill No : " + textBox14.Text, new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(510, 220));
           //Date
           e.Graphics.DrawString("Date : " + DateTime.Now, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(510, 250));
           //Line
           e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------- ", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(35, 280));
           //Details
           e.Graphics.DrawString("Particulars", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(35, 300));
           e.Graphics.DrawString("HSNCode", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(250, 300));
           e.Graphics.DrawString("Unit", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(390, 300));
           e.Graphics.DrawString("Batch", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(450, 300));
           e.Graphics.DrawString("Qty", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(520, 300));
           e.Graphics.DrawString("Exp.", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(570, 300));
           e.Graphics.DrawString("Rate", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(645, 300));
           e.Graphics.DrawString("Amount", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(720, 300));
           //Line
           e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------- ", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(35, 320));

           int yPos = 350;
           foreach (var i in Product)
           {
               e.Graphics.DrawString(i.Particular, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(35, yPos));

               e.Graphics.DrawString(i.HSNCode, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(270, yPos));

               e.Graphics.DrawString(i.Unit, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(400, yPos));

               e.Graphics.DrawString(i.Batch, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(470, yPos));

               e.Graphics.DrawString(i.Qty, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(525, yPos));

               e.Graphics.DrawString(i.Exp, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(560, yPos));

               e.Graphics.DrawString(i.Rate, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(650, yPos));

               e.Graphics.DrawString(i.Amount.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(730, yPos));

               yPos += 30;
           }
           e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------- ", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(35, yPos));
           e.Graphics.DrawString("SubTotal      : " + textBox1.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(600,yPos+30));

           e.Graphics.DrawString("Total to Pay  : " + textBox11.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(600, yPos + 90));



       }
Posted
Updated 26-Jun-18 1:07am

It seems to be an awkward way to 'print' something... I would say you should learn about creating PDF from your code...
In any case DrawString has version that writes inside a rectangle, so you can set the border for your text and it will not flow over its neighbors... but will be cut...
Graphics.DrawString Method (String, Font, Brush, RectangleF) (System.Drawing)[^]
 
Share this answer
 
You have already been given an answer at https://www.codeproject.com/Questions/1249927/How-do-I-print-long-name-in-a-row-with-auto-adjust[^]. Please do not repost the same question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900