SvgFontAwesome.cs
C# VB
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System;using System.IO;using System.Drawing;using System.Linq;using System.Collections.Generic;using GrapeCity.Documents.Pdf;using GrapeCity.Documents.Text;using GrapeCity.Documents.Svg;using GCTEXT = GrapeCity.Documents.Text;using GCDRAW = GrapeCity.Documents.Drawing;using DsPdfWeb.Demos.Common;
namespace DsPdfWeb.Demos{ // 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 Random _rnd = Util.NewRandom();
public int CreatePDF(Stream stream, int paramsIdx = 0) { return CreatePDF(stream, GetSampleParamsList()[paramsIdx]); }
public int CreatePDF(Stream stream, string[] sampleParams) { // Load Font Awesome glyphs from the resources: var images = new List<(string, GcSvgDocument)>(); var colorize = sampleParams[3] == "colorize";
if (colorize) { foreach (var fname in Directory.GetFiles(Path.Combine("Resources", "FontAwesome"), "*.svg", SearchOption.AllDirectories)) images.Add((fname, GcSvgDocument.FromFile(fname))); // Randomize image order: images.Shuffle(); } else { foreach (var fname in Directory.GetFiles(Path.Combine("Resources", "FontAwesome", sampleParams[3]), "*.svg", SearchOption.TopDirectoryOnly)) images.Add((fname, GcSvgDocument.FromFile(fname))); // Sort images by file name: images.Sort(new Comparison<(string, GcSvgDocument)>((t1_, t2_) => StringComparer.OrdinalIgnoreCase.Compare(t1_.Item1, t2_.Item1))); }
var doc = new GcPdfDocument(); // Font and format for captions: const float sMargin = 72f / 8; var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")); var tf = new TextFormat() { Font = font, FontSize = sMargin * 0.65f };
// Set up a layout grid: var background = Color.White; var foreground = Color.Black; const float margin = 36; const int rows = 12; const int cols = 9; float gapx = 72f / 8, gapy = gapx; float sWidth = (doc.PageSize.Width - margin * 2 + gapx) / cols; float sHeight = (doc.PageSize.Height - margin * 2 + gapy) / rows; if (sWidth > sHeight) { gapx += sWidth - sHeight; sWidth = sHeight; } else { gapy += sHeight - sWidth; sHeight = sWidth; } var ip = new PointF(margin, margin);
// Render all images within the grid, adding new pages as needed: var g = doc.NewPage().Graphics; for (int i = 0; i < images.Count(); ++i) { // Draw border around image: var 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: var svg = images[i].Item2; var s = svg.GetIntrinsicSize(SvgLengthUnits.Points); if (s.Width > 0 && s.Height > 0) { // If image proportions are different from our target rectangle, // we resize the rectangle centering the image in it: var qSrc = s.Width / s.Height; var qTgt = rect.Width / rect.Height; if (qSrc < qTgt) rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2, 0); else if (qSrc > qTgt) rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2); } // if (colorize) svg.RootSvg.Fill = new SvgPaint(Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256)));
// 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 && i < images.Count() - 1) { ip.X = margin; ip.Y += sHeight; if (ip.Y + sHeight > doc.PageSize.Height) { g = doc.NewPage().Graphics; ip.Y = margin; } } } // Done: doc.Save(stream); // Dispose images after saving the PDF: images.ForEach(t_ => t_.Item2.Dispose()); return doc.Pages.Count; }
public static List<string[]> GetSampleParamsList() { // Strings are name, description, info, rest are arbitrary strings: return new List<string[]>() { 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" }, }; } }}