|
| 1 | +using FileFormat.Slides.Common; |
| 2 | +using FileFormat.Slides.Common.Enumerations; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace FileFormat.Slides.Examples |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Provides C# code examples for creating, reading, and modifying slides in a Presentation |
| 11 | + /// using the <a href="https://www.nuget.org/packages/FileFormat.Slides">FileFormat.Slides</a> library. |
| 12 | + /// </summary> |
| 13 | + public class StyledListExamples |
| 14 | + { |
| 15 | + private const string newDocsDirectory = "../../../Presentations/New"; |
| 16 | + private const string existingDocsDirectory = "../../../Presentations/Existing"; |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="SlideExamples"/> class. |
| 19 | + /// Prepares the directory 'Presentations/New' for storing or loading PowerPoint(PPT or PPTX) presentations |
| 20 | + /// at the root of the project. |
| 21 | + /// If the directory doesn't exist, it is created. If it already exists, |
| 22 | + /// existing files are deleted, and the directory is cleaned up. |
| 23 | + /// </summary> |
| 24 | + public StyledListExamples () |
| 25 | + { |
| 26 | + if (!System.IO.Directory.Exists(newDocsDirectory)) |
| 27 | + { |
| 28 | + // If it doesn't exist, create the directory |
| 29 | + System.IO.Directory.CreateDirectory(newDocsDirectory); |
| 30 | + System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " + |
| 31 | + $"created successfully."); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + var files = System.IO.Directory.GetFiles(System.IO.Path.GetFullPath(newDocsDirectory)); |
| 36 | + foreach (var file in files) |
| 37 | + { |
| 38 | + System.IO.File.Delete(file); |
| 39 | + System.Console.WriteLine($"File deleted: {file}"); |
| 40 | + } |
| 41 | + System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " + |
| 42 | + $"cleaned up."); |
| 43 | + } |
| 44 | + } |
| 45 | + /// <summary> |
| 46 | + /// This method creates bulleted list in a slide |
| 47 | + /// </summary> |
| 48 | + /// <param name="documentDirectory">Path of the presentation folder</param> |
| 49 | + /// <param name="filename">Presentation name</param> |
| 50 | + public void CreateBulletedListInASlide (string documentDirectory = newDocsDirectory, string filename = "test.pptx") |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + // Create instance of presentation |
| 55 | + Presentation presentation = Presentation.Create($"{documentDirectory}/{filename}"); |
| 56 | + //Create instances of text shapes and set their properties. |
| 57 | + TextShape shape = new TextShape(); |
| 58 | + shape.Y = 20.0; |
| 59 | + shape.FontSize = 80; |
| 60 | + shape.FontFamily = "Baguet Script"; |
| 61 | + shape.Text = "Bulleted List Example"; |
| 62 | + TextShape shape2 = new TextShape(); |
| 63 | + shape2.FontSize = 100; |
| 64 | + shape2.Y = 180.0; |
| 65 | + // Create instance of Bulleted List |
| 66 | + StyledList list = new StyledList(ListType.Bulleted); |
| 67 | + // Add items to list |
| 68 | + list.AddListItem("USA"); |
| 69 | + list.AddListItem("Canada"); |
| 70 | + list.AddListItem("Brazil"); |
| 71 | + list.AddListItem("Mexico"); |
| 72 | + // Assign list to text shape |
| 73 | + shape2.TextList = list; |
| 74 | + // Create slide |
| 75 | + Slide slide = new Slide(); |
| 76 | + // Set background color of slide because default background color is black. |
| 77 | + slide.BackgroundColor = Colors.Silver; |
| 78 | + // Add text shapes. |
| 79 | + slide.AddTextShapes(shape); |
| 80 | + slide.AddTextShapes(shape2); |
| 81 | + // Adding slides |
| 82 | + presentation.AppendSlide(slide); |
| 83 | + // Save presentation |
| 84 | + presentation.Save(); |
| 85 | + |
| 86 | + } |
| 87 | + catch (System.Exception ex) |
| 88 | + { |
| 89 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 90 | + } |
| 91 | + } |
| 92 | + /// <summary> |
| 93 | + /// This method creates numbered list in a slide |
| 94 | + /// </summary> |
| 95 | + /// <param name="documentDirectory">Path of the presentation folder</param> |
| 96 | + /// <param name="filename">Presentation name</param> |
| 97 | + public void CreateNumberedListInASlide (string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 98 | + { |
| 99 | + try |
| 100 | + { |
| 101 | + |
| 102 | + // Create instance of presentation |
| 103 | + Presentation presentation = Presentation.Create($"{documentDirectory}/{filename}"); |
| 104 | + //Create instances of text shapes and set their properties. |
| 105 | + TextShape shape = new TextShape(); |
| 106 | + shape.Y = 20.0; |
| 107 | + shape.FontSize = 80; |
| 108 | + shape.FontFamily = "Amasis MT Pro Black"; |
| 109 | + shape.Text = "Numbered List Example"; |
| 110 | + TextShape shape2 = new TextShape(); |
| 111 | + shape2.FontSize = 100; |
| 112 | + shape2.Y = 180.0; |
| 113 | + // Create instance of Bulleted List |
| 114 | + StyledList list = new StyledList(ListType.Bulleted); |
| 115 | + // Add items to list |
| 116 | + list.AddListItem("Tennis"); |
| 117 | + list.AddListItem("Cricket"); |
| 118 | + list.AddListItem("Hockey"); |
| 119 | + list.AddListItem("Football"); |
| 120 | + list.AddListItem("Snooker"); |
| 121 | + // Assign list to text shape |
| 122 | + shape2.TextList = list; |
| 123 | + // Create slide |
| 124 | + Slide slide = new Slide(); |
| 125 | + // Set background color of slide because default background color is black. |
| 126 | + slide.BackgroundColor = Colors.Silver; |
| 127 | + // Add text shapes. |
| 128 | + slide.AddTextShapes(shape); |
| 129 | + slide.AddTextShapes(shape2); |
| 130 | + // Adding slides |
| 131 | + presentation.AppendSlide(slide); |
| 132 | + // Save presentation |
| 133 | + presentation.Save(); |
| 134 | + |
| 135 | + } |
| 136 | + catch (System.Exception ex) |
| 137 | + { |
| 138 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 139 | + } |
| 140 | + } |
| 141 | + /// <summary> |
| 142 | + /// This method adds the list items in an existing numbered or bulleted list. |
| 143 | + /// </summary> |
| 144 | + /// <param name="documentDirectory">Path of the presentation folder</param> |
| 145 | + /// <param name="filename">Presentation name</param> |
| 146 | + public void AddListItemsInAnExistingList(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 147 | + { |
| 148 | + try |
| 149 | + { |
| 150 | + // Create instance of presentation |
| 151 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 152 | + // Get desired slide |
| 153 | + Slide slide = presentation.GetSlides()[3]; |
| 154 | + // Create instance of Bulleted List |
| 155 | + TextShape shape = slide.TextShapes[0]; |
| 156 | + StyledList list = shape.TextList; |
| 157 | + // Add items to list |
| 158 | + list.AddListItem("Tennis"); |
| 159 | + list.AddListItem("Cricket"); |
| 160 | + list.AddListItem("Hockey"); |
| 161 | + list.AddListItem("Football"); |
| 162 | + list.AddListItem("Snooker"); |
| 163 | + // Update the list |
| 164 | + list.Update(); |
| 165 | + // Save presentation |
| 166 | + presentation.Save(); |
| 167 | + |
| 168 | + } |
| 169 | + catch (System.Exception ex) |
| 170 | + { |
| 171 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 172 | + } |
| 173 | + } |
| 174 | + /// <summary> |
| 175 | + /// This method removes the list items in an existing numbered or bulleted list. |
| 176 | + /// </summary> |
| 177 | + /// <param name="documentDirectory">Path of the presentation folder</param> |
| 178 | + /// <param name="filename">Presentation name</param> |
| 179 | + public void RemoveListItemsInAnExistingList (string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 180 | + { |
| 181 | + try |
| 182 | + { |
| 183 | + // Create instance of presentation |
| 184 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 185 | + // Get desired slide |
| 186 | + Slide slide = presentation.GetSlides()[3]; |
| 187 | + // Create instance of Bulleted List |
| 188 | + TextShape shape = slide.TextShapes[0]; |
| 189 | + StyledList list = shape.TextList; |
| 190 | + // Remove a range of list items |
| 191 | + list.ListItems.RemoveRange(0,4); |
| 192 | + // Or you can remove all items like |
| 193 | + list.ListItems.RemoveAt(1); |
| 194 | + // Update the list |
| 195 | + list.Update(); |
| 196 | + // Save presentation |
| 197 | + presentation.Save(); |
| 198 | + |
| 199 | + } |
| 200 | + catch (System.Exception ex) |
| 201 | + { |
| 202 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments