StringAlignment.Justify 在 VB 中? [英] StringAlignment.Justify in VB?

查看:21
本文介绍了StringAlignment.Justify 在 VB 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用 VB 打印一些文档,而且我一直做得很好......直到我意识到我需要在 Justify 中打印我的文档.

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.

这是我在 PrintDocuments 中使用的代码:

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)

有没有办法用 Justify 或其他东西代替 StringAlignment.Center ?

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

推荐答案

长句(或段落)左对齐功能为你工作!

   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 

这篇关于StringAlignment.Justify 在 VB 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆