using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.Json; using Microsoft.VisualBasic.FileIO; using Aspose.Slides; using Aspose.Slides.Charts; using Aspose.Slides.DOM.Ole; using Aspose.Slides.Export; internal static class Example { public static ExampleContext Context = null!; public static void Setup() { Directory.CreateDirectory("output"); } public static string ExpectedText(string value) => Context.IsLicensed || value.Length<=5 ? value : value[..5]+"... text has been truncated due to evaluation version limitation."; public static string Choice(string licensed,string evaluation) => Context.IsLicensed ? licensed : evaluation; public static void VerifySavedText(string path,string shapeName,string expected) { using var zip=System.IO.Compression.ZipFile.OpenRead(path); foreach(var entry in zip.Entries.Where(e=>e.FullName.StartsWith("ppt/slides/slide",StringComparison.Ordinal)&&e.FullName.EndsWith(".xml",StringComparison.Ordinal))) { using var stream=entry.Open();var document=System.Xml.Linq.XDocument.Load(stream); foreach(var shape in document.Descendants().Where(e=>e.Name.LocalName=="sp")) { if(!shape.Descendants().Any(e=>e.Name.LocalName=="cNvPr"&&(string?)e.Attribute("name")==shapeName))continue; string stored=string.Concat(shape.Descendants().Where(e=>e.Name.LocalName=="t").Select(e=>e.Value)); Require(stored==expected,"Saved XML text differs from the intended transformation.");return; } } throw new InvalidDataException("Named saved shape is absent."); } public static void Require(bool value,string message) { if(!value) throw new InvalidOperationException(message); } public static string Hash(byte[] data)=>Convert.ToHexString(SHA256.HashData(data)).ToLowerInvariant(); public static string HashFile(string path)=>Hash(File.ReadAllBytes(path)); public static void Result(object checks) { File.WriteAllText("output/result.json",JsonSerializer.Serialize(new {checks},new JsonSerializerOptions{WriteIndented=true}),new UTF8Encoding(false)); } public static void Json(string path,object value) { File.WriteAllText(path,JsonSerializer.Serialize(value,new JsonSerializerOptions{WriteIndented=true}),new UTF8Encoding(false)); } public static IAutoShape Text(IShapeCollection shapes,string name,string value,float x=35,float y=35,float width=600,float height=70) { var shape=shapes.AddAutoShape(ShapeType.Rectangle,x,y,width,height); shape.Name=name; shape.TextFrame.Text=value; shape.FillFormat.FillType=FillType.Solid; shape.FillFormat.SolidFillColor.Color=Color.White; foreach(var para in shape.TextFrame.Paragraphs) foreach(var portion in para.Portions) { portion.PortionFormat.LatinFont=new FontData("Noto Sans"); portion.PortionFormat.FontHeight=20; portion.PortionFormat.FillFormat.FillType=FillType.Solid; portion.PortionFormat.FillFormat.SolidFillColor.Color=Color.FromArgb(35,65,100); } return shape; } public static IEnumerable Walk(IShapeCollection shapes) { foreach(IShape shape in shapes){yield return shape;if(shape is IGroupShape group)foreach(var child in Walk(group.Shapes))yield return child;} } public static IEnumerable Frames(ISlide slide) { foreach(var shape in Walk(slide.Shapes)) { if(shape is IAutoShape auto)yield return auto.TextFrame; if(shape is ITable table)for(int r=0;r(ISlide slide,string name) where T:class,IShape { var found=Walk(slide.Shapes).Where(s=>s.Name==name).ToArray(); if(found.Length!=1 || found[0] is not T typed)throw new InvalidDataException("Expected one named shape: "+name);return typed; } public static void Render(Presentation p,string path) { using var image=p.Slides[0].GetImage(new Size(800,450));image.Save(path,Aspose.Slides.ImageFormat.Png); } public static byte[] Png(int width,int height,Color color) => PortableImage.Png(width,height,color); public static List Csv(string text,int columns) { using var parser=new TextFieldParser(new StringReader(text));parser.SetDelimiters(",");parser.HasFieldsEnclosedInQuotes=true;parser.TrimWhiteSpace=false; var rows=new List();while(!parser.EndOfData){var row=parser.ReadFields()!;if(row.Length!=columns)throw new InvalidDataException("CSV field count differs.");rows.Add(row);}return rows; } public static string CsvCell(string text)=>"\""+text.Replace("\"","\"\"")+"\""; public static string SafeOutput(string folder,string filename) { string root=Path.GetFullPath(folder)+Path.DirectorySeparatorChar; string path=Path.GetFullPath(Path.Combine(folder,filename)); if(!path.StartsWith(root,StringComparison.OrdinalIgnoreCase))throw new InvalidDataException("Output escapes its directory.");return path; } public static IChart Chart(ISlide slide,string name) { var chart=slide.Shapes.AddChart(ChartType.ClusteredColumn,40,130,620,290);chart.Name=name;return chart; } public static void ChartData(IChart chart,string[] categories,string[] series,double[][] values) { var data=chart.ChartData;var book=data.ChartDataWorkbook;data.Series.Clear();data.Categories.Clear();book.Clear(0); for(int r=0;r