打印时按需要自动缩放文本尺寸且强制换行

来源:互联网 发布:细说php微盘 编辑:程序博客网 时间:2024/06/05 19:01
Dim mark As Single   
Dim g As Graphics = e.Graphics    
Dim str As String   
Dim i As Integer   
Dim temp() As String   
Dim myfix As Drawing2D.Matrix = New Drawing2D.Matrix    
Dim size As SizeF    
Dim length As Single   
   
str = ttbBuildName.Text.TrimStart(""'所有文本    
size = g.MeasureString(str, myfont3)     
length 
= size.Width '所有文本在myfont3状态下的长度(像素)    
If length > 4082 Then '4082:供打印的文本框的长度(像素)    
    mark = 4082 / length    
Else   
    mark 
= 1    
End If   
myfix.Scale(mark, 
1'按比例压缩宽度    
g.Transform = myfix    
   
Dim j As Integer = 0    
Dim from As Integer = 0    
Dim strtemp As String = " "   
   
'第一行    
   
Do While (g.MeasureString(strtemp, myfont3).Width < 302 * (1 / mark) And 302 * (1 / mark) - g.MeasureString(strtemp, myfont3).Width >= g.MeasureString(str.Substring(from + j, 1), myfont3).Width)    
    j 
= j + 1    
    strtemp 
= str.Substring(from, j)    
    
If (j + from) = str.Length Then Exit Do   
Loop   
from 
= from + j    
g.DrawString(strtemp, myfont3, Brushes.Black, 
New RectangleF((x1 + 679* (1 / mark), y1 + 80, length, 30), formatl)    
= 0    
   
'后9行    
   
If (j + from) <= str.Length - 1 Then   
    
For i = 1 To 9    
        strtemp 
= " "   
        
Do While (g.MeasureString(strtemp, myfont3).Width < 420 * (1 / mark) And 420 * (1 / mark) - g.MeasureString(strtemp, myfont3).Width >= g.MeasureString(str.Substring(from + j, 1), myfont3).Width)    
            j 
= j + 1    
            strtemp 
= str.Substring(from, j)    
            
If (j + from) = str.Length Then Exit Do   
        
Loop   
        from 
= from + j    
        g.DrawString(strtemp, myfont3, Brushes.Black, 
New RectangleF((x1 + 561* (1 / mark), y1 + 130 + 51 * (i - 1), length, 30), formatl)    
        j 
= 0    
        
If (j + from) = str.Length Then Exit For   
    
Next   
End If   
   
myfix.Scale(
1 / mark, 1'恢复原始比例(1:1)    
g.Transform = myfix