//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingSystem.Linq;usingSystem.Collections.Generic;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text;usingGrapeCity.Documents.Svg;usingGCTEXT = GrapeCity.Documents.Text;usingGCDRAW = GrapeCity.Documents.Drawing;usingDsPdfWeb.Demos.Common; namespaceDsPdfWeb.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.publicclassSvgFontAwesome {privateRandom_rnd = Util.NewRandom(); publicintCreatePDF(Stream stream, int paramsIdx = 0) {returnCreatePDF(stream, GetSampleParamsList()[paramsIdx]); } publicintCreatePDF(Stream stream, string[] sampleParams) {// Load Font Awesome glyphs from the resources:var images = newList<(string, GcSvgDocument)>();var colorize = sampleParams[3] == "colorize"; if (colorize) {foreach (var fname inDirectory.GetFiles(Path.Combine("Resources", "FontAwesome"), "*.svg", SearchOption.AllDirectories)) images.Add((fname, GcSvgDocument.FromFile(fname)));// Randomize image order: images.Shuffle(); }else {foreach (var fname inDirectory.GetFiles(Path.Combine("Resources", "FontAwesome", sampleParams[3]), "*.svg", SearchOption.TopDirectoryOnly)) images.Add((fname, GcSvgDocument.FromFile(fname)));// Sort images by file name: images.Sort(newComparison<(string, GcSvgDocument)>((t1_, t2_) => StringComparer.OrdinalIgnoreCase.Compare(t1_.Item1, t2_.Item1))); } var doc = newGcPdfDocument();// Font and format for captions:constfloatsMargin = 72f / 8;var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf"));var tf = newTextFormat() { Font = font, FontSize = sMargin * 0.65f }; // Set up a layout grid:var background = Color.White;var foreground = Color.Black;constfloatmargin = 36;constintrows = 12;constintcols = 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 = newPointF(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 = newRectangleF(ip, newSizeF(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);elseif (qSrc > qTgt) rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2); }//if (colorize) svg.RootSvg.Fill = newSvgPaint(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,newRectangleF(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; } publicstaticList<string[]> GetSampleParamsList() {// Strings are name, description, info, rest are arbitrary strings:returnnewList<string[]>() {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" }, }; } }}