StringAlignment.Justify in VB?

回眸只為那壹抹淺笑 提交于 2021-02-10 21:58:46

问题


I've been trying to print some documents in VB, and I've been doing just fine... until I realize I need my documents printed in Justify.

This is the code I use in my PrintDocuments:

Dim drawcenter As New StringFormat
drawcenter.Alignment = StringAlignment.Center

e.Graphics.DrawString("Terms and Conditions of Service", New Font("Times New Roman", 13, FontStyle.Bold Or FontStyle.Underline), New SolidBrush(Color.Black), New RectangleF(50, 50, 700, 30), drawcenter)

Is there a way to replace StringAlignment.Center with Justify or something instead?


回答1:


Function for Justified Left a long sentence(or Paragraph) Working for you !

   1.  Public  Function  GetJustifiedTextinLeft (text As String, width As Integer) As String
    Dim palabras As String() = text.Split(" "c)  'text-->palabras
    Dim sb1 As New System.Text.StringBuilder()
    Dim sb2 As New System.Text.StringBuilder()
    Dim length As Integer = palabras.Length     'palabras
    Dim resultado As New System.Collections.Generic.List(Of String)()
    Dim i As Integer = 0
    While i < length
        sb1.AppendFormat("{0} ", palabras(i))   'palabras
        If sb1.ToString().Length > width Then
            resultado.Add(sb2.ToString())
            sb1 = New System.Text.StringBuilder()
            sb2 = New System.Text.StringBuilder()
            System.Math.Max(System.Threading.Interlocked.Decrement(i), i + 1)
        Else
            sb2.AppendFormat("{0} ", palabras(i))
        End If
        System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
    End While
    resultado.Add(sb2.ToString())               'resultado

    Dim resultado2 As New System.Collections.Generic.List(Of String)()
    Dim temp As String

    Dim index1 As Integer, index2 As Integer, salto As Integer
    Dim target As String
    Dim limite As Integer = resultado.Count     'resultado
    For Each item As String In resultado        'resultado
        target = " "
        temp = item.ToString().Trim()
        index1 = 0
        index2 = 0
        salto = 2

        If limite <= 1 Then
            resultado2.Add(temp)
            Exit For
        End If
        While temp.Length <= width
            If temp.IndexOf(target, index2) < 0 Then
                index1 = 0
                index2 = 0
                target = target + " "
                System.Math.Max(System.Threading.Interlocked.Increment(salto), salto - 1)
            End If
            index1 = temp.IndexOf(target, index2)
            temp = temp.Insert(temp.IndexOf(target, index2), " ")

            index2 = index1 + salto
        End While
        System.Math.Max(System.Threading.Interlocked.Decrement(limite), limite + 1)
        resultado2.Add(temp)
    Next

    Dim builder As New System.Text.StringBuilder()
    For Each item As String In resultado2
        builder.Append(item).Append(chr(10))
   Next
    Return builder.ToString()
End Function


  2.    For Calling  above Function
  Dim  Resulttext as string=GetJustifiedTextinLeft (“YourString”,70) 
  '70 is the width 



回答2:


Do you mean pad the string?

You could try String.PadRight.

http://www.dotnetperls.com/padright

http://msdn.microsoft.com/en-us/library/66f6d830%28v=vs.110%29.aspx

http://www.csharp-examples.net/align-string-with-spaces/

If you want to have the same number of spaces between each word so the first word is on the left and the last word is on the right you may need to right your own code.

I wrote this which does what I think you want, it could be refactored:

Sub Main()

    System.Console.WriteLine("123456789012345678901234567890")
    Dim Words As New List(Of String)
    Words.Add("Hello")
    Words.Add("World")
    System.Console.WriteLine(SplitWordsOverSpace(30, Words))

    Words.Add("Cheese")
    System.Console.WriteLine(SplitWordsOverSpace(30, Words))

    Words.Add("a")
    System.Console.WriteLine(SplitWordsOverSpace(30, Words))

    System.Console.ReadLine()
End Sub

Public Function SplitWordsOverSpace(ByVal LineWidthCharCount As Integer, ByVal WordsonLine As List(Of String)) As String
    Try
        Dim TotalWordLength As Integer = 0
        For Each S As String In WordsonLine
            TotalWordLength += S.Length
        Next
        Dim LeftOverSpace As Integer = LineWidthCharCount - TotalWordLength

        Dim Spaces(WordsonLine.Count - 1) As String
        Dim SpaceperWord As Integer = Math.Floor(LeftOverSpace / (WordsonLine.Count - 1))
        Dim Remainder As Integer = LeftOverSpace Mod (WordsonLine.Count - 1)

        Dim sb As New Text.StringBuilder()
        For Each s As String In WordsonLine
            sb.Append(s)
            If Not String.Equals(s, WordsonLine(WordsonLine.Count - 1)) Then
                For i As Integer = 1 To SpaceperWord
                    sb.Append(" ")
                Next
                If Remainder > 0 Then
                    sb.Append(" ")
                    Remainder -= 1
                End If
            End If
        Next
        Return sb.ToString()
    Catch ex As Exception
        Throw ''Or something
    End Try
End Function


来源:https://stackoverflow.com/questions/26923231/stringalignment-justify-in-vb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!