Gradients.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
'' 此示例展示了如何创建渐变填充
'' 使用 LinearGradientBrush 和 RadialGradientBrush。
Public Class Gradients
Function CreatePDF(ByVal stream As Stream) As Integer
Dim doc = New GcPdfDocument()
Dim g = doc.NewPage().Graphics
Dim testRectSize = New SizeF(72 * 4, 72)
Dim dy = 12
'' TextLayout绘制标签:
Dim tl = g.CreateTextLayout()
tl.DefaultFormat.Font = StandardFonts.Times
tl.DefaultFormat.FontSize = 18
tl.DefaultFormat.ForeColor = Color.Chartreuse
tl.MaxWidth = testRectSize.Width
tl.MaxHeight = testRectSize.Height
tl.TextAlignment = TextAlignment.Center
tl.ParagraphAlignment = ParagraphAlignment.Center
'' 注释1:
Dim rc = Util.AddNote("使用 LinearGradientBrush 的线性渐变:", doc.Pages.Last, New RectangleF(72, 36, 500, 100))
'' 文本插入点:
Dim ip = New PointF(rc.Left, rc.Bottom + dy)
'' 绘制渐变填充矩形的本地操作:
Dim drawSwatch As Action(Of GCDRAW.Brush, String) =
Sub(ByVal b_, ByVal txt_)
Dim rect = New RectangleF(ip, testRectSize)
'' 用渐变画笔填充矩形:
g.FillRectangle(rect, b_)
'' 绘制边框、文本信息等:
g.DrawRectangle(rect, Color.Magenta)
tl.Clear()
tl.Append(txt_)
tl.MaxHeight = testRectSize.Height
tl.MaxWidth = testRectSize.Width
tl.PerformLayout(True)
g.DrawTextLayout(tl, ip)
ip.Y += rect.Height + dy
End Sub
'' 水平渐变:
Dim linearGradBrush = New LinearGradientBrush(Color.Red, Color.Blue)
drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
'' 垂直渐变:
linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Green, New PointF(0, 1))
drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
'' 对角渐变(增加样本高度以更好地显示对角线):
testRectSize.Height *= 2
linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Teal, New PointF(1, 1))
drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
'' 径向渐变画笔
rc = Util.AddNote("使用 RadialGradientBrush 的径向渐变:", doc.Pages.Last, New RectangleF(ip, New SizeF(500, 100)))
ip.Y = rc.Bottom + dy
'' 居中:
Dim radialGradBrush = New RadialGradientBrush(Color.Orange, Color.Purple)
drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
'' 右下角中心:
radialGradBrush = New RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, New PointF(1, 1))
drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
''
'' 完毕:
doc.Save(stream)
Return doc.Pages.Count
End Function
End Class