using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Threading; using System.Drawing; using System.Linq; using GrapeCity.Documents.Word; using GrapeCity.Documents.Pdf; using GrapeCity.Documents.Text; using DocumentFormat.OpenXml.ExtendedProperties; namespace DsWordWeb.Demos { public static class Util { private static int s_gen = 0; private static DateTime s_fixedTime = new DateTime(1989, 6, 21, 1, 23, 45, DateTimeKind.Local); private static string[] s_words = new[] { "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "mauris", "id", "volutpat", "tellus", "ullamcorper", "sem", "praesent", "et", "ante", "laoreet", "lobortis", "nunc", "congue", "nisi", "donec", "ac", "tempus", "sed", "feugiat", "pulvinar", "pharetra", "turpis", "nonummy", "at", "felis", "eget", "molestie", "euismod", "non", "aliquet", "diam", "proin", "mi", "nibh", "massa", "tincidunt", "ut", "dolore", "magna", "aliquam", "erat", }; public static FontCollection FontCollection { get; set; } public static bool NoRandom { get; set; } = false; public static Random NewRandom() { if (NoRandom) return new Random(0); else return new Random((int)DateTime.Now.Ticks + Interlocked.Increment(ref s_gen)); } public static DateTime TimeNow() { if (NoRandom) return s_fixedTime; else return DateTime.Now; } public static string LoremIpsumWord() { return s_words[NewRandom().Next(s_words.Length)]; } public static List LoremIpsum( int numParagraphs = 5, int minSentences = 5, int maxSentences = 10, int minWords = 5, int maxWords = 30) { List result = new List(); for (int p = 0; p < numParagraphs; p++) result.Add(LoremIpsumPar(minSentences, maxSentences, minWords, maxWords)); return result; } // https://stackoverflow.com/questions/4286487/is-there-any-lorem-ipsum-generator-in-c public static string LoremIpsumPar( int minSentences = 5, int maxSentences = 10, int minWords = 5, int maxWords = 30) { int commaMax = 10; var rnd = NewRandom(); int numSentences = rnd.Next(maxSentences - minSentences) + minSentences + 1; int numWords = rnd.Next(maxWords - minWords) + minWords + 1; List result = new List(); StringBuilder par = new StringBuilder(); for (int s = 0; s < numSentences; s++) { for (int w = 0; w < numWords; w++) { string word = LoremIpsumWord(); if (w == 0) { par.Append(char.ToUpper(word[0])); par.Append(word.Substring(1)); } else { if (rnd.Next(commaMax) == 0) par.Append(','); par.Append(' '); par.Append(word); } } par.Append(". "); } return par.ToString(); } // https://stackoverflow.com/questions/273313/randomize-a-listt public static void Shuffle(this IList list) { var rnd = NewRandom(); int n = list.Count; while (n > 1) { n--; int k = rnd.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } } /// /// Adds a note to the document - a text fragment on a yellow background. /// Used to insert meta info into sample documents. /// /// The text to draw. /// The page to draw on. /// The text bounds. If null, whole page with 1" margins all around is used. /// The actual rectangle occupied by the note. public static RectangleF AddNote(string text, Page page, RectangleF? bounds = null) { var g = page.Graphics; var tl = g.CreateTextLayout(); var pad = g.Resolution / 8f; tl.DefaultFormat.Font = StandardFonts.Helvetica; tl.DefaultFormat.FontSize = 12; if (!bounds.HasValue) { var margin = g.Resolution; // 1" margins by default bounds = new RectangleF(margin, margin, page.Size.Width - margin * 2, page.Size.Height - margin * 2); } tl.MaxWidth = bounds.Value.Width; tl.MaxHeight = bounds.Value.Height; tl.MarginLeft = tl.MarginRight = tl.MarginTop = tl.MarginBottom = pad; tl.Append(text); tl.PerformLayout(true); var rect = tl.ContentRectangle; rect.Offset(bounds.Value.Location); rect.Inflate(pad, pad); g.FillRectangle(rect, System.Drawing.Color.PaleGoldenrod); g.DrawRectangle(rect, System.Drawing.Color.DarkGoldenrod, 0.5f); g.DrawTextLayout(tl, bounds.Value.Location); return rect; } /// /// Set all page margins to 0.5". /// /// The target document. public static void SetNarrowMargins(GcWordDocument doc) { var margin = 36; doc.Body.Sections[0].PageSetup.Margin.Top = margin; doc.Body.Sections[0].PageSetup.Margin.Bottom = margin; doc.Body.Sections[0].PageSetup.Margin.Left = margin; doc.Body.Sections[0].PageSetup.Margin.Right = margin; } } }