Skip to main content
March 24, 2011
Answered

Length of a String

  • March 24, 2011
  • 1 reply
  • 1092 views

Hi All -

I have a VB.NET app that produces a PS file containing text and graphics.  I need to keep track, in my VB app, of where some text has printed. Specifically, I have a block of text which will need to be parsed into multiple lines to fit into a fixed width column.  So I would like to figure out how much of the text I can print on the current line before going to the next line. So I need to know the printed width of a given chunk of text.

Is there any way for VB to calculate or somehow get (through a call to ??) the 'stringwidth' of a string of text for a given font?

Thanks for your thoughts.  Bob

This topic has been closed for replies.
Correct answer Mr__Horton

Bob,

There are probably two ways, at least, to accomplish what you are after within your VB application:

1. I'm not familiar with VB's text setting capabilities, but I think you could determine string or character widths, spaces, and figure out the break positions as long as you have the same font(s) available to VB as you do for the PS job.

VB starting point:

  • Dim g As System.Drawing.Graphics = Me.CreateGraphics
  • Dim str As String = "This is the String to get the width of"
    Dim StringSize As SizeF = g.MeasureString(str, Me.Font)

    2. You could load nominal character widths (size 1) from the PS font(s) into a database table that you could access from VB and then multiply the nominal character widths times the size you are setting to determine printed character widths, etc. (This is what I am most familiar with. This technique can be expanded to include kerning pairs and it doesn't require that your VB app host has the PS printer or screen fonts installed on it).

    - Marvin

    1 reply

    Mr__Horton
    Inspiring
    March 24, 2011

    Bob,

    To clarify:

    Does your VB app need to know and do the wrapping to fit a column width prior to generating the PS code, or if you passed the column width and the string/font/size to the PS code would you get what you need if a PS procedure did the wrapping for you?

    - Marvin

    March 24, 2011

    My app needs to know how many lines of text printed. What I was thinking of doing is:

    Take a string and parse it into individual words.

    Find the PS stringlength of the first two words.

    If that length is less than my column width, add another word and repeat.

    When I have the maximum length that will fit, print the first line of text and start a new line with the next word.

    Repeat the above process until I run out of text.

    If I could do that, my app will know the current y-position so I can start the next section of output at the right location.

    I think the key is my app knowing the stringlength of some words of text so I can keep track of things.  A PS procedure would probably be OK to print the text but would not communicate the resultant position back to my app.

    I hope that clarifies things.

    Thanks for the quick response.

    Bob

    Mr__Horton
    Mr__HortonCorrect answer
    Inspiring
    March 24, 2011

    Bob,

    There are probably two ways, at least, to accomplish what you are after within your VB application:

    1. I'm not familiar with VB's text setting capabilities, but I think you could determine string or character widths, spaces, and figure out the break positions as long as you have the same font(s) available to VB as you do for the PS job.

    VB starting point:

  • Dim g As System.Drawing.Graphics = Me.CreateGraphics
  • Dim str As String = "This is the String to get the width of"
    Dim StringSize As SizeF = g.MeasureString(str, Me.Font)

    2. You could load nominal character widths (size 1) from the PS font(s) into a database table that you could access from VB and then multiply the nominal character widths times the size you are setting to determine printed character widths, etc. (This is what I am most familiar with. This technique can be expanded to include kerning pairs and it doesn't require that your VB app host has the PS printer or screen fonts installed on it).

    - Marvin