打印HTML文件
请先打开CaseStyudy目录下的文件"PDFCreator示例.Table"后再运行本节的示例代码。
利用DrawStringHTML可以将一个网页文件转换成PDF格式,参考代码:
Dim
text
As
String
= FileSys.ReadAllText(ProjectPath &
"flow.htm")
Dim
file
As
String
=
"c:\temp\test.pdf"
Dim
pdc
As
New
PDFCreator()
pdc.PaperKind = Drawing.Printing.PaperKind.A3
Dim
rect
As
RectangleF = pdc.PageRectangle()
rect.Inflate( - 72, - 72)
Dim
fnt
As
New
Font("微软雅黑",
10)
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)
如果要分栏打印,可以参考:
Dim
file
As
String
=
"c:\temp\test.pdf"
Dim
pdc
As
New
PDFCreator()
pdc.PaperKind = Drawing.Printing.PaperKind.A3
pdc.Landscape =
True
Dim
rect
As
RectangleF = pdc.PageRectangle()
rect.Inflate( - 72, - 72)
Dim
cnt
As
Integer
= 2
'分栏数
Dim
widthCol
As
Double
= (rect.Width - (cnt - 1) * 45) / cnt
'计算栏宽,假定栏间距是45
Dim
rectCols(cnt - 1)
As
RectangleF
For
i
As
Integer
= 0
To
cnt - 1
'计算各栏对应的矩形位置和尺寸
rectCols(i) = rect
rectCols(i).Width = widthCol
rectCols(i).X = rect.X + (widthCol + 45) * i
'设置栏的水平位置,假定栏间距是45
Next
Dim
txt
As
String
= FileSys.ReadAllText(ProjectPath &
"flow.htm")
'读取网页文件
Dim
nextChar
As
Integer
pdc.Pages.Clear()
While
nextChar < txt.Length - 1
pdc.NewPage()
For
i
As
Integer
= 0
To
cnt - 1
'逐栏打印
nextChar = pdc.DrawStringHtml(txt,
Nothing,
Brushes.Black, rectcols(i), nextChar)
If
nextChar > txt.Length - 1
Then
Exit
For
End
If
Next
End
While
pdc.Save(file)
Process.Start(file)