我一直在尝试用VB打印一些文档,我一直做得很好……直到我意识到我需要用Justify打印我的文档。
这是我在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)有没有办法用Justify或者别的什么来代替StringAlignment.Center呢?
发布于 2019-07-05 15:37:28
用于对齐左长句(或段落)的函数为你工作!
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 发布于 2014-12-15 18:18:55
你的意思是把绳子垫起来吗?
你可以试试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/
如果你想在每个单词之间有相同数量的空格,那么第一个单词在左边,最后一个单词在右边,你可能需要右转你自己的代码。
我写了这段代码,它做了我认为你想要的事情,它可以被重构:
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 Functionhttps://stackoverflow.com/questions/26923231
复制相似问题