Determining the number of lines in a text line?

As part of the print class, I want to be able to print long lines on multiple pages, but I don’t know how to calculate the height of the whole line that I will determine by first counting the number of lines in my line. I know that I can count the number of line breaks, but I also use word-wrap, so line breaks will be added whenever the line goes across the width of the page. Therefore, I believe that I can count the number of line breaks and find out the width of each line and find out if there is a need for line breaks for words for each line, but this seems too complicated a problem for something. I think it can be made easier.

e.Graphics.DrawString(multiPageString, new Font("Courier New", 12), Brushes.Black, new RectangleF(0, 0, 810, pageHeight));

If you have any tips please let me know thanks!

+3
source share
6 answers

I agree that @David Heffernan provides a truly standard implementation of the swamp. Its a way to complicate the value that they are going to achieve, or go with something that will give 100% reliable print results, etc. Reporting Services I feel your pain, typing painstakingly!

+1
source
int iMaxWidth = 240; // Maximum width, in pixels
string sText = @"blah-blah-blah";
Font objFont = new Font("Times New Roman", 13, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
StringFormat sfFmt = new StringFormat(StringFormatFlags.LineLimit);
Graphics g = Graphics.FromImage(new Bitmap(1, 1));
int iHeight = (int)g.MeasureString(sText, objFont, iMaxWidth, sfFmt).Height;
int iOneLineHeight = (int)g.MeasureString("Z", objFont, iMaxWidth, sfFmt).Height;
int iNumLines = (int)(iHeight/iOneLineHeight);
+4
source

, , , ?

const int PAGE_MAX_CHARS_PER_LINE = 30;
var message = "this is a really long line and it will make your eyes pop out but I don't know how to present a long line differently.  So you will have to stick with\r\nit.   I think the above line should be long enough\r\n\r\n I would love to see how this turns out.";
var lines = message.Split(new string [] {"\r\n"}, StringSplitOptions.None);
var linesCount = lines.Length;

var longLines = lines.Where(i=>i.Length > PAGE_MAX_CHARS_PER_LINE);
foreach(var l in longLines)
{
  int numberOfLines = (int)Math.Ceiling((double)l.Length / PAGE_MAX_CHARS_PER_LINE); /// Will need to embed graphics measurement mechanism here?
  linesCount += numberOfLines - 1;
}
+2

Graphics.MeasureString(, )

graphics.MeasureString("some string", new Font("Arial", 10))
+1

@Alexander, - . Graphics .

Suppose you have the maximum line length wrapWidthin a pixel, and the line you want to check is - multiPageString, you can use this function:

private int GetNumOfLines(string multiPageString, int wrapWidth, Font fnt)
{
    StringFormat sfFmt = new StringFormat(StringFormatFlags.LineLimit);
    using(Graphics g = Graphics.FromImage(new Bitmap(1, 1)))
    {
        int iHeight = (int)g.MeasureString(multiPageString, fnt, wrapWidth, sfFmt).Height;
        int iOneLineHeight = (int)g.MeasureString("Z", fnt, wrapWidth, sfFmt).Height;
        return (int)(iHeight/iOneLineHeight);
    }
}

(This function is derived from @Alexander's answer .)

+1
source

Why don't you just add it to the text block and print it? No more than 10-15 lines of code should be executed.

0
source

Source: https://habr.com/ru/post/1792592/


All Articles