精确控制行距
打开CaseStyudy目录下的文件"PDFCreator示例.Table"后运行本节的示例代码。
DrawString方法并没有控制行距的参数,如果要控制行距,只能逐行回执。
参考代码:
Dim
file
As
String
=
"c:\temp\test.pdf"
Dim
pdc
As
New
PDFCreator()
Dim
rectPage
As
RectangleF = pdc.PageRectangle()
rectPage.Inflate( - 72, - 72)
'参数tRowSpacing用于指定行距,0是标准行距,正数表示增加行距,负数表示缩小行距
Dim
DrawText =
Function
(tText
As
String,
tFont
As
Font, tRect
As
RectangleF, tRowSpacing
As
Integer)
Dim
nextChar
As
Integer
= 0
Dim
lastChar
As
Integer
= 0
tRect.Height = tFont.Height
If
tRect.Bottom > rectPage.Bottom
Then
'如果剩余空间不够一行,则另起一页
pdc.NewPage()
tRect.Y = rectPage.Y
End
If
tText = tText.Replace(vbcrlf, vblf).Replace(vbcr, vblf)
'统一换行符为vblf,适用不同的换行格式
While
nextChar < tText.Length
nextChar = pdc.DrawString(tText, tFont, Brushes.Black, tRect, nextChar)
'绘制文本
'因为是逐行绘制,如果上一行的结尾是的换行符,那么换行符是不会绘制的,需要人工排除,以免印象接下来的绘制
If
nextChar < tText.Length
AndAlso
tText(nextChar) = vbLf
Then
nextChar = nextChar + 1
End
If
trect.Offset(0, trect.Height + tRowSpacing)
'移到下一行
If
nextChar < tText.Length
AndAlso
tRect.Bottom > rectPage.Bottom
Then
'如果剩余空间不够一行,则另起一页
pdc.NewPage()
tRect.Y = rectPage.Y
End
If
End
While
End
Function
Dim
fnt
As
New
Font("宋体",
12)
Dim
rect
As
RectangleF = rectPage
Dim
txt
As
String
= FileSys.ReadAllText(ProjectPath &
"flow.txt")
'内容来自于一个文本文件
DrawText.Invoke(txt, fnt, rect, 0)
'在标准行距的基础上加大5磅的行距
pdc.Save(file)
Process.Start(file)