两端对齐文本
PDFCreator可以设置文本在水平方向靠左、居中、靠右对齐,但没有办法像Word或者HTML文件一样,让文本在水平方向实现两端对齐。
之前我们是借助RTF格式来实现文本两端对齐,现在看看如何通过HTML来实现。
HTML文本居中对齐的控制符格式为:
<p align="justify">文本内容</p>
示例一
在命令窗口执行下面的代码:
Dim
text
As
String
=
"THE ORIGINS OF THE Portable Document Format and the Adobe
Acrobat product family date to early 1990."
text = text & text & text
Dim
file
As
String
=
"c:\temp\test.pdf"
Dim
pdc
As
New
PDFCreator()
Dim
sf
As
New
StringFormat()
Dim
rect
As
RectangleF = pdc.PageRectangle()
rect.Inflate( - 72, - 72)
Dim
fnt
As
New
Font("微软雅黑",
12)
'两端对齐可以通过HTML格式实现
text =
"<p align='justify'>"
& text &
"</p>"
pdc.DrawStringHTML( text, fnt, Brushes.Black, rect)
pdc.Save(file)
Process.Start(file)
生成的文档为:
示例二
以前我们介绍过如何将一个文本文件转换为PDF格式,参考:绘制长文本
现在我们增加一个要求,就是转换成PDF格式后,文本必须两端对齐,代码如下:
Dim
text
As
String
= FileSys.ReadAllText(ProjectPath &
"flow.txt")
Dim
file
As
String
=
"c:\temp\test.pdf"
Dim
pdc
As
New
PDFCreator()
Dim
rect
As
RectangleF = pdc.PageRectangle()
rect.Inflate( - 72, - 72)
Dim
fnt
As
New
Font("微软雅黑",
14)
text =
"<p align='justify'>"
& text &
"</p>"
'加上水平对齐控制符
Dim
nextChar
As
Integer
= 0
pdc.Pages.Clear()
While
nextChar < text.Length - 1
pdc.NewPage()
nextChar = pdc.DrawStringHTML(text, fnt, Brushes.Black, rect, nextChar)
'用DrawStringHTML绘制
End
While
pdc.Save(file)
Process.Start(file)