Click here to Skip to main content
15,887,945 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:

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));
Posted
Updated 25-Jun-18 21:54pm
Comments
Richard MacCutchan 26-Jun-18 3:46am    
You need to measure the string first to see if it will fit within the space allocated to it. You can then adjust its font or number of lines as necessary.

1 solution

Use MeasureString

Graphics.MeasureString, méthode (System.Drawing)[^]

Ex:
private void Form1_Paint(object sender, PaintEventArgs e)
        {
            int columnSize = 100;
            e.Graphics.DrawString("Particulars", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(35, 300));
           Font font = fitText(e.Graphics, "Metaformin Hydrochloride Sustained", new Font("Arial", 15, FontStyle.Regular), columnSize);
            e.Graphics.DrawString("Metaformin Hydrochloride Sustained", font, Brushes.Black, new Point(35, 350));            
            e.Graphics.DrawString("HSNCode", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(columnSize*2, 300));
            e.Graphics.DrawString("Unit", new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(columnSize*3, 300));
            e.Graphics.Dispose();
        }

        private Font fitText(Graphics e,string text,Font font,double maxWidth)
        {            
            while (e.MeasureString(text, font).Width > maxWidth)
               font = new Font(font.FontFamily, font.Size - 0.1F,font.Style);

            return font;
        }
 
Share this answer
 
v2

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