'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystemImportsSystem.IOImportsSystem.DrawingImportsSystem.LinqImportsSystem.Collections.GenericImportsGrapeCity.Documents.PdfImportsGrapeCity.Documents.TextImportsGrapeCity.Documents.SvgImports GCTEXT = GrapeCity.Documents.Text '' 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.PublicClassSvgFontAwesomePrivate_rndAsRandom = Util.NewRandom() FunctionCreatePDF(ByVal stream AsStream, Optional paramsIdx AsInteger = 0) AsIntegerReturnCreatePDF(stream, GetSampleParamsList()(paramsIdx))EndFunction FunctionCreatePDF(ByVal stream AsStream, ByVal sampleParams AsString()) AsInteger'' Load Font Awesome glyphs from the resources:Dim images = NewList(Of (String, GcSvgDocument))()Dim colorize = sampleParams(3) = "colorize" If colorize ThenForEach fname InDirectory.GetFiles(Path.Combine("Resources", "FontAwesome"), "*.svg", SearchOption.AllDirectories) images.Add((fname, GcSvgDocument.FromFile(fname)))Next'' Randomize image order: images.Shuffle()ElseForEach fname InDirectory.GetFiles(Path.Combine("Resources", "FontAwesome", sampleParams(3)), "*.svg", SearchOption.TopDirectoryOnly) images.Add((fname, GcSvgDocument.FromFile(fname)))Next'' Sort images by file name: images.Sort(NewComparison(Of (String, GcSvgDocument))(Function(t1_, t2_) StringComparer.OrdinalIgnoreCase.Compare(t1_.Item1, t2_.Item1)))EndIf Dim doc = NewGcPdfDocument()'' Font and format for captions:ConstsMarginAsSingle = 72.0F / 8Dim font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf"))Dim tf = NewTextFormat() With {.Font = font, .FontSize = sMargin * 0.65F} '' Set up a layout grid:Dim background = Color.WhiteDim foreground = Color.BlackConstmarginAsSingle = 36ConstrowsAsInteger = 12ConstcolsAsInteger = 9Dim gapx AsSingle = 72.0F / 8Dim gapy AsSingle = gapxDim sWidth AsSingle = (doc.PageSize.Width - margin * 2 + gapx) / colsDim sHeight AsSingle = (doc.PageSize.Height - margin * 2 + gapy) / rowsIf sWidth > sHeight Then gapx += sWidth - sHeight sWidth = sHeightElse gapy += sHeight - sWidth sHeight = sWidthEndIfDim ip = NewPointF(margin, margin) '' Render all images within the grid, adding new pages as needed:Dim g = doc.NewPage().GraphicsFor i = 0To images.Count - 1'' Draw border around image:Dim rect = NewRectangleF(ip, NewSizeF(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).Item2Dim s = svg.GetIntrinsicSize(SvgLengthUnits.Points)If s.Width > 0AndAlso s.Height > 0Then'' If image proportions are different from our target rectangle,'' we resize the rectangle centering the image in it:Dim qSrc = s.Width / s.HeightDim qTgt = rect.Width / rect.HeightIf 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)EndIfEndIf If colorize Then svg.RootSvg.Fill = NewSvgPaint(Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256)))EndIf '' 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,NewRectangleF(rect.X, rect.Bottom, rect.Width, sMargin),TextAlignment.Center, ParagraphAlignment.Near, False) ip.X += sWidthIf ip.X + sWidth > doc.PageSize.WidthAndAlso i < images.Count - 1Then ip.X = margin ip.Y += sHeightIf ip.Y + sHeight > doc.PageSize.HeightThen g = doc.NewPage().Graphics ip.Y = marginEndIfEndIfNext '' Done: doc.Save(stream)'' Dispose images after saving the PDF: images.ForEach(Sub(t_) t_.Item2.Dispose())Return doc.Pages.CountEndFunction PublicSharedFunctionGetSampleParamsList() AsList(OfString())'' Strings are name, description, info, rest are arbitrary strings:ReturnNewList(OfString())() From {NewString() {"@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"},NewString() {"@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"},NewString() {"@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"},NewString() {"@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"} }EndFunctionEndClass