''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Linq
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Svg
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
'' This sample shows how to render SVG icons included in the
'' "Free for Web" download of Font Awesome.
'' The sample code also shows how to change the default color of the glyphs.
Public Class SvgFontAwesome
Private _rnd As Random = Util.NewRandom()
Function CreatePDF(ByVal stream As Stream, Optional paramsIdx As Integer = 0) As Integer
Return CreatePDF(stream, GetSampleParamsList()(paramsIdx))
End Function
Function CreatePDF(ByVal stream As Stream, ByVal sampleParams As String()) As Integer
'' Load Font Awesome glyphs from the resources:
Dim images = New List(Of (String, GcSvgDocument))()
Dim colorize = sampleParams(3) = "colorize"
If colorize Then
For Each fname In Directory.GetFiles(Path.Combine("Resources", "FontAwesome"), "*.svg", SearchOption.AllDirectories)
images.Add((fname, GcSvgDocument.FromFile(fname)))
Next
'' Randomize image order:
images.Shuffle()
Else
For Each fname In Directory.GetFiles(Path.Combine("Resources", "FontAwesome", sampleParams(3)), "*.svg", SearchOption.TopDirectoryOnly)
images.Add((fname, GcSvgDocument.FromFile(fname)))
Next
'' Sort images by file name:
images.Sort(New Comparison(Of (String, GcSvgDocument))(Function(t1_, t2_) StringComparer.OrdinalIgnoreCase.Compare(t1_.Item1, t2_.Item1)))
End If
Dim doc = New GcPdfDocument()
'' Font and format for captions:
Const sMargin As Single = 72.0F / 8
Dim font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf"))
Dim tf = New TextFormat() With {.Font = font, .FontSize = sMargin * 0.65F}
'' Set up a layout grid:
Dim background = Color.White
Dim foreground = Color.Black
Const margin As Single = 36
Const rows As Integer = 12
Const cols As Integer = 9
Dim gapx As Single = 72.0F / 8
Dim gapy As Single = gapx
Dim sWidth As Single = (doc.PageSize.Width - margin * 2 + gapx) / cols
Dim sHeight As Single = (doc.PageSize.Height - margin * 2 + gapy) / rows
If sWidth > sHeight Then
gapx += sWidth - sHeight
sWidth = sHeight
Else
gapy += sHeight - sWidth
sHeight = sWidth
End If
Dim ip = New PointF(margin, margin)
'' Render all images within the grid, adding new pages as needed:
Dim g = doc.NewPage().Graphics
For i = 0 To images.Count - 1
'' Draw border around image:
Dim rect = New RectangleF(ip, New SizeF(sWidth - gapx, sHeight - gapy))
g.FillRectangle(rect, background)
g.DrawRectangle(rect, foreground, 0.5F)
rect.Inflate(-sMargin, -sMargin)
'' Draw the SVG:
Dim svg = images(i).Item2
Dim s = svg.GetIntrinsicSize(SvgLengthUnits.Points)
If s.Width > 0 AndAlso s.Height > 0 Then
'' If image proportions are different from our target rectangle,
'' we resize the rectangle centering the image in it:
Dim qSrc = s.Width / s.Height
Dim qTgt = rect.Width / rect.Height
If qSrc < qTgt Then
rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2, 0)
ElseIf qSrc > qTgt Then
rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2)
End If
End If
If colorize Then
svg.RootSvg.Fill = New SvgPaint(Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256)))
End If
'' Render the SVG:
g.DrawSvg(svg, rect)
'' Print the icon file name as caption in the bottom margin:
g.DrawString(Path.GetFileNameWithoutExtension(images(i).Item1), tf,
New RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
TextAlignment.Center, ParagraphAlignment.Near, False)
ip.X += sWidth
If ip.X + sWidth > doc.PageSize.Width AndAlso i < images.Count - 1 Then
ip.X = margin
ip.Y += sHeight
If ip.Y + sHeight > doc.PageSize.Height Then
g = doc.NewPage().Graphics
ip.Y = margin
End If
End If
Next
'' Done:
doc.Save(stream)
'' Dispose images after saving the PDF:
images.ForEach(Sub(t_) t_.Item2.Dispose())
Return doc.Pages.Count
End Function
Public Shared Function GetSampleParamsList() As List(Of String())
'' Strings are name, description, info, rest are arbitrary strings:
Return New List(Of String())() From {
New String() {"@b-svg/Font Awesome - brands", "Render Font Awesome SVG glyphs from the ""svgs/brands"" directory",
"This sample renders the SVG icons included in the ""svgs/brands/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.",
"brands"},
New String() {"@b-svg/Font Awesome - regular", "Render Font Awesome SVG glyphs from the ""svgs/regular"" directory",
"This sample renders the SVG icons included in the ""svgs/regular/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.",
"regular"},
New String() {"@b-svg/Font Awesome - solid", "Render Font Awesome SVG glyphs from the ""svgs/solid"" directory",
"This sample renders the SVG icons included in the ""svgs/solid/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.",
"solid"},
New String() {"@b-svg/Font Awesome - colorize", "Render Font Awesome SVG glyphs using random order and colors",
"This sample renders the SVG icons included in the ""svgs/"" directory of the Font Awesome ""Free for Web"" download, randomizing the order of the icons and their colors.",
"colorize"}
}
End Function
End Class