|
| 1 | +using FileFormat.Slides; |
| 2 | +using FileFormat.Slides.Common; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +namespace FileFormat.Slides.Examples |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Provides C# code examples for creating, reading, and modifying CurvedLine segments or shapes in a Presentation |
| 12 | + /// using the <a href="https://www.nuget.org/packages/FileFormat.Slides">FileFormat.Slides</a> library. |
| 13 | + /// </summary> |
| 14 | + public class CurvedLineExamples |
| 15 | + { |
| 16 | + private const string newDocsDirectory = "../../../Presentations/New"; |
| 17 | + private const string existingDocsDirectory = "../../../Presentations/Existing"; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="CurvedLineExamples"/> class. |
| 21 | + /// Prepares the directory 'Presentations/New' for storing or loading PowerPoint(PPT or PPTX) presentations |
| 22 | + /// at the root of the project. |
| 23 | + /// If the directory doesn't exist, it is created. If it already exists, |
| 24 | + /// existing files are deleted, and the directory is cleaned up. |
| 25 | + /// </summary> |
| 26 | + public CurvedLineExamples() |
| 27 | + { |
| 28 | + if (!System.IO.Directory.Exists(newDocsDirectory)) |
| 29 | + { |
| 30 | + // If it doesn't exist, create the directory |
| 31 | + System.IO.Directory.CreateDirectory(newDocsDirectory); |
| 32 | + System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " + |
| 33 | + $"created successfully."); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + var files = System.IO.Directory.GetFiles(System.IO.Path.GetFullPath(newDocsDirectory)); |
| 38 | + foreach (var file in files) |
| 39 | + { |
| 40 | + System.IO.File.Delete(file); |
| 41 | + System.Console.WriteLine($"File deleted: {file}"); |
| 42 | + } |
| 43 | + System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " + |
| 44 | + $"cleaned up."); |
| 45 | + } |
| 46 | + } |
| 47 | + /// <summary> |
| 48 | + /// This method adds CurvedLine segment or shape in the silde of a new PowerPoint presentation. |
| 49 | + /// </summary> |
| 50 | + /// <param name="documentDirectory">Path of the presentation folder</param> |
| 51 | + /// <param name="filename">Presentation name</param> |
| 52 | + public void DrawNewCurvedLineShapeInNewSlide(string documentDirectory = newDocsDirectory, string filename = "test.pptx") |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 57 | + // Create an instance of CurvedLine |
| 58 | + CurvedLine CurvedLine = new CurvedLine(); |
| 59 | + // Set height and width |
| 60 | + CurvedLine.Width = 400.0; |
| 61 | + CurvedLine.Height = 400.0; |
| 62 | + // Set Y position |
| 63 | + CurvedLine.Y = 100.0; |
| 64 | + // First slide |
| 65 | + Slide slide = presentation.GetSlides()[1]; |
| 66 | + // Add CurvedLine shapes. |
| 67 | + slide.DrawCurvedLine(CurvedLine); |
| 68 | + // Save the PPT or PPTX |
| 69 | + presentation.Save(); |
| 70 | + |
| 71 | + } |
| 72 | + catch (System.Exception ex) |
| 73 | + { |
| 74 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 75 | + } |
| 76 | + } |
| 77 | + /// <summary> |
| 78 | + /// This method Sets the background color of a CurvedLine shape |
| 79 | + /// </summary> |
| 80 | + /// <param name="documentDirectory">Path of the presentation folder</param> |
| 81 | + /// <param name="filename">Presentation name</param> |
| 82 | + public void SetBackgroundColorOfCurvedLine(string documentDirectory = newDocsDirectory, string filename = "test.pptx") |
| 83 | + { |
| 84 | + try |
| 85 | + { |
| 86 | + |
| 87 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 88 | + // Get the slides |
| 89 | + Slide slide = presentation.GetSlides()[1]; |
| 90 | + // Get 1st CurvedLine |
| 91 | + CurvedLine CurvedLine = slide.CurvedLines[0]; |
| 92 | + // Set background of the CurvedLine |
| 93 | + CurvedLine.BackgroundColor = "289876"; |
| 94 | + |
| 95 | + // Save the PPT or PPTX |
| 96 | + presentation.Save(); |
| 97 | + |
| 98 | + } |
| 99 | + catch (System.Exception ex) |
| 100 | + { |
| 101 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Remove CurvedLine shape from an existing slide |
| 107 | + /// </summary> |
| 108 | + /// <param name="documentDirectory">Path of the presentation folder</param> |
| 109 | + /// <param name="filename">Presentation name</param> |
| 110 | + public void RemoveCurvedLineShapeExistingSlide(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 111 | + { |
| 112 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 113 | + // Get the slides |
| 114 | + Slide slide = presentation.GetSlides()[1]; |
| 115 | + // Get 1st CurvedLine |
| 116 | + CurvedLine CurvedLine = slide.CurvedLines[0]; |
| 117 | + // Remove CurvedLine |
| 118 | + CurvedLine.Remove(); |
| 119 | + // Save the PPT or PPTX |
| 120 | + presentation.Save(); |
| 121 | + |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments