Make a StringFormat to align the text. If (justification = TextJustification.Full)ĭrawJustifiedLine(gr, rect, font, brush, line)
#How to justify a paragraph full
See if we should use full justification. RectangleF rect = new RectangleF(x, y, width, font.Height) Private void DrawLine(Graphics gr, string line, Font font,īrush brush, float x, float y, float width,
#How to justify a paragraph code
The following code shows the DrawLine method. If this is not the end of the paragraph or the program isn’t fully justifying the text, then the method calls DrawLine to draw the line with the desired justification. You would have the first word on the far left, a huge space, and the second word on the far right.) Imagine how weird it would be if the last line contained only two words. (When you fully justify text, you don’t justify the final line. If the string contains all of the remaining words, and if the program is fully justifying the text, then the method calls DrawLine to left justify this line. It then removes the final word so the string fits. The loop continues until the method runs out of words or runs out of space to draw them.Įach time through the loop, the code builds a string, adding more words until the string no longer fits in the available width.
This method breaks the paragraph into words and enters a loop to process the words. Return new RectangleF(rect.X, y, rect.Width, height) Return a RectangleF representing any unused Y += font.Height * extra_paragraph_spacing Don't indent subsequent lines in this paragraph. Make sure there's room for another line. (justification = TextJustification.Full)) See if this is the last line in the paragraph. Rect = DrawParagraph(gr, rect, font, brush, Draw the paragraph keeping track of remaining space. TextJustification justification, float line_spacing, Private RectangleF DrawParagraphs(Graphics gr, RectangleF rect, The following DrawParagraphs method draws the paragraphs with the proper alignment. This example shows how you can fully justify paragraphs of text. The answer you want, but I'm 99% certain that's the only answer.The StringFormat class makes it fairly easy to left justify, right justify, or center a line of text, but strangely it doesn’t provide a method to fully justify a line of text (so it extends all the way to both the left and right margins. So.how the text appears is up to the containing application. The text, such as bold/italic/underline. If you transfer the text elsewhere and if the containing application understands those codes it will translated and the text formatted accordingly. Now, rich text means text can be saved with character formatting codes "wrapped" around The Justify function in Word is what 'pads' the text to remove the jagged edge and it's always making modifications for you. For example if you change your margins in a Word document, add additional words, even changing to another printer can cause the text to re-wrap.
Text is essentially plain text that obtains its appearance (formatting) from the container the text is placed in. You really can't control where text will begin and end on a line.
#How to justify a paragraph plus
Plus I suspect the holes make it look worse than the jagged right edge. More than likely you'll see holes in the text from the spaces you added and a new 'rightĮdge'. Resize the Notepad window and note how the text wraps to the new size.Add some spaces to 'justify' a few lines of text.If necessary, resize the Notepad window so the text wraps to the window.From the Format menu, make sure Word Wrap is turned on (it will have a check to the left of the command).In the Notepad copy/paste some text from Word or add a bunch of text.Here's an example of what I'm referring to:
The same 'right edge' you see in Word (or alternate program) will be right edge elsewhere. If you try to pad it with spaces or something there's no guarantee How text is justified depends on text 'container', not the text itself. Suzanne may have an idea for you but I'm fairly certain there isn't an alternate method for what you want to accomplish.