set printed text to justify and set spacing for each line

how to set a long text in string to justify when printed and how to se

Page 1 of 1

8 Replies - 11848 Views - Last Post: 24 November 2009 - 07:53 PM Rate Topic: -----

#1 matyus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

set printed text to justify and set spacing for each line

Post icon  Posted 24 November 2009 - 03:36 AM

i have a long text in a string and i want to print them. i can set margin top, left and right like microsoft word format, but how to justify the text and set the spacing of each line like microsoft word?

this is my example :
Public Class Form1

	Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
		PrintPreviewDialog1.Document = PrintDocument1
		PrintPreviewDialog1.ShowDialog()
	End Sub

	Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		Dim Rect As New RectangleF
		Dim PrintFont As New Font("Arial", 10)
		Dim PrintString As String

		With PrintDocument1.DefaultPageSettings
			Rect.Width = .PaperSize.Width - (.Margins.Left + .Margins.Right)
			Rect.Height = .PaperSize.Height - (.Margins.Top + .Margins.Bottom)
			Rect.Offset(.Margins.Left, .Margins.Top)
		End With

		PrintString = "Hi " & NameTextBox.Text & ". Google Chrome is a web browser released by Google " & _
		"which uses the WebKit layout engine and application framework. It was first released as a beta " & _
		"version for Microsoft Windows on 2 September 2008, and the public stable release was on " & _
		"11 December 2008. The name is derived from the graphical user interface frame, or 'chrome', " & _
		"of web browsers. As of 1 November 2009 , Chrome was the fourth most widely used browser, with " & _
		"3.6% of worldwide usage share of web browsers.Development versions of Chrome for Linux and " & _
		"Mac OS X were released in June 2009."

		e.Graphics.DrawString(PrintString, PrintFont, Brushes.Black, Rect)
	End Sub
End Class


what should i do to set the text to justify and set spacing to 1.5 for each line for example. i want print out the text just like microsoft word format.

Is This A Good Question/Topic? 0
  • +

Replies To: set printed text to justify and set spacing for each line

#2 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 08:56 AM

Use an overload of DrawString that accepts a StringFormat instance, and use its properties to fine control the way the text is rendered.

For the spacing, you're going to have to do it manually. See:
http://social.msdn.m...7-72bb11497683/
Was This Post Helpful? 0
  • +
  • -

#3 bigbug  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 27-October 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 10:06 AM

Text Alignment
'Create an instance of StringFormat
Dim fText As New StringFormat

'Alignment accepts either center or justify as args. You could also play with the FormatFlags for backwards and vertical text.
fText.Alignment = StringAlignment.Justify

'Now all you have to do is call it in your print string.
e.Graphics.DrawString(PrintString, PrintFont, Brushes.Black, Rect, fText)

Google StringFormat for more details.

Line Spacing is a factor of the font being used.
Since true line spacing does not exist you have a couple of choices:
1 - You could break your long string into short strings and render one at a time incrementing Y = Y + (font*1.5) lol there is a little more to it than that but it gets you started.

2 - You could use MeasureString to render a long string into your rectangle but you are still going to have to increment the lines by the font * 1.5.

Google MeasureString for more details.

bigbug

This post has been edited by bigbug: 24 November 2009 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 matyus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 11:38 AM

but StringFormat.Alignment property only support Near, Far and Center. I want the first and last words are aligned to the left and right margins, while the words between are evenly spaced. microsoft word has 4 option; align text left (Near), center, align text right (Far) and justify. i want that justify.

for the spacing, i can set the spacing manually if i print the texts line by line. but if i have a long texts in a variable, how can i set the spacing because when the text reach the right margin, automatically the texts printed in newlines below. what trick should i use?
Was This Post Helpful? 0
  • +
  • -

#5 bigbug  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 27-October 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 11:50 AM

View Postmatyus, on 24 Nov, 2009 - 10:38 AM, said:

for the spacing, i can set the spacing manually if i print the texts line by line. but if i have a long texts in a variable, how can i set the spacing because when the text reach the right margin, automatically the texts printed in newlines below. what trick should i use?


use #2
Was This Post Helpful? 0
  • +
  • -

#6 matyus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 12:51 PM

sorry bigbug, i really do not understand using MeasureString. in theory, i can use "fText.FormatFlags = StringFormatFlags.NoWrap" so i can get the text in first line, then i should remove that text/characters in that string so when i print text in newline, the next text can be printed. am i right?

so how to determine the last character in a line and how to remove the first character untill a certain character? or maybe another solution for that?
Was This Post Helpful? 0
  • +
  • -

#7 bigbug  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 27-October 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 01:20 PM

View Postmatyus, on 24 Nov, 2009 - 11:51 AM, said:

sorry bigbug, i really do not understand using MeasureString. in theory, i can use "fText.FormatFlags = StringFormatFlags.NoWrap" so i can get the text in first line, then i should remove that text/characters in that string so when i print text in newline, the next text can be printed. am i right?

so how to determine the last character in a line and how to remove the first character untill a certain character? or maybe another solution for that?


Google DrawString Line Spacing
There are a few great examples of using MeasureString and font* 1.5 together on long text strings.

Sorry I do not have an example in nay of my projects on this machine or I would just paste one in here for you...

bigbug
Was This Post Helpful? 0
  • +
  • -

#8 zam_cat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-November 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 07:18 PM

View Postmatyus, on 24 Nov, 2009 - 02:36 AM, said:

i have a long text in a string and i want to print them. i can set margin top, left and right like microsoft word format, but how to justify the text and set the spacing of each line like microsoft word?

this is my example :
Public Class Form1

	Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
		PrintPreviewDialog1.Document = PrintDocument1
		PrintPreviewDialog1.ShowDialog()
	End Sub

	Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		Dim Rect As New RectangleF
		Dim PrintFont As New Font("Arial", 10)
		Dim PrintString As String

		With PrintDocument1.DefaultPageSettings
			Rect.Width = .PaperSize.Width - (.Margins.Left + .Margins.Right)
			Rect.Height = .PaperSize.Height - (.Margins.Top + .Margins.Bottom)
			Rect.Offset(.Margins.Left, .Margins.Top)
		End With

		PrintString = "Hi " & NameTextBox.Text & ". Google Chrome is a web browser released by Google " & _
		"which uses the WebKit layout engine and application framework. It was first released as a beta " & _
		"version for Microsoft Windows on 2 September 2008, and the public stable release was on " & _
		"11 December 2008. The name is derived from the graphical user interface frame, or 'chrome', " & _
		"of web browsers. As of 1 November 2009 , Chrome was the fourth most widely used browser, with " & _
		"3.6% of worldwide usage share of web browsers.Development versions of Chrome for Linux and " & _
		"Mac OS X were released in June 2009."

		e.Graphics.DrawString(PrintString, PrintFont, Brushes.Black, Rect)
	End Sub
End Class


what should i do to set the text to justify and set spacing to 1.5 for each line for example. i want print out the text just like microsoft word format.

thats helpin.....
thanks
Was This Post Helpful? 0
  • +
  • -

#9 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: set printed text to justify and set spacing for each line

Posted 24 November 2009 - 07:53 PM

There was me thinking it could justify the text. Wishful thinking...

Can you use WPF for this? It has a TextFormatter class that (probably) does what you want.

Otherwise you're going to have to find or write the equivalent. I looked and couldn't find any libraries to do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1