|
| 1 | +using DocumentFormat.OpenXml.Presentation; |
| 2 | +using FileFormat.Slides.Common; |
| 3 | +using FileFormat.Slides.Common.Enumerations; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | + |
| 9 | +namespace FileFormat.Slides.Examples |
| 10 | +{ |
| 11 | + public class CommentExamples |
| 12 | + { |
| 13 | + private const string newDocsDirectory = "../../../Presentations/New"; |
| 14 | + private const string existingDocsDirectory = "../../../Presentations/Existing"; |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the <see cref="CommentExamples"/> class. |
| 17 | + /// Prepares the directory 'Presentations/New' for storing or loading PowerPoint(PPT or PPTX) presentations |
| 18 | + /// at the root of the project. |
| 19 | + /// If the directory doesn't exist, it is created. If it already exists, |
| 20 | + /// existing files are deleted, and the directory is cleaned up. |
| 21 | + /// </summary> |
| 22 | + public CommentExamples() |
| 23 | + { |
| 24 | + if (!System.IO.Directory.Exists(newDocsDirectory)) |
| 25 | + { |
| 26 | + // If it doesn't exist, create the directory |
| 27 | + System.IO.Directory.CreateDirectory(newDocsDirectory); |
| 28 | + System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " + |
| 29 | + $"created successfully."); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + var files = System.IO.Directory.GetFiles(System.IO.Path.GetFullPath(newDocsDirectory)); |
| 34 | + foreach (var file in files) |
| 35 | + { |
| 36 | + System.IO.File.Delete(file); |
| 37 | + System.Console.WriteLine($"File deleted: {file}"); |
| 38 | + } |
| 39 | + System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " + |
| 40 | + $"cleaned up."); |
| 41 | + } |
| 42 | + } |
| 43 | + /// <summary> |
| 44 | + /// Creates comment in a slide of a PowerPoint presentation. |
| 45 | + /// </summary> |
| 46 | + /// <param name="documentDirectory">The directory where the PowerPoint presentation is located. Default is 'Presentations/Existing'.</param> |
| 47 | + /// <param name="filename">The name of the PowerPoint file. Default is 'test.pptx'.</param> |
| 48 | + |
| 49 | + public void CreateCommentInASlide(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + // Create instance of presentation |
| 54 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 55 | + // Get 1st slide |
| 56 | + Slide slide = presentation.GetSlides()[0]; |
| 57 | + // Create comment |
| 58 | + Comment comment1 = new Comment(); |
| 59 | + // Set comment author |
| 60 | + comment1.AuthorId = 1; |
| 61 | + // Add comment content |
| 62 | + comment1.Text = "2nd Programmatic comment in an existing presentation"; |
| 63 | + // Set comment time |
| 64 | + comment1.InsertedAt = DateTime.Now; |
| 65 | + // Add comment to slide |
| 66 | + slide.AddComment(comment1); |
| 67 | + // Save presentation |
| 68 | + presentation.Save(); |
| 69 | + |
| 70 | + } |
| 71 | + catch (System.Exception ex) |
| 72 | + { |
| 73 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 74 | + } |
| 75 | + } |
| 76 | + /// <summary> |
| 77 | + /// Remove a comment from a slide. |
| 78 | + /// </summary> |
| 79 | + /// <param name="documentDirectory">The directory where the PowerPoint presentation is located. Default is 'Presentations/Existing'.</param> |
| 80 | + /// <param name="filename">The name of the PowerPoint file. Default is 'test.pptx'.</param> |
| 81 | + |
| 82 | + public void RemoveACommentFromASlide(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 83 | + { |
| 84 | + try |
| 85 | + { |
| 86 | + // Create instance of presentation |
| 87 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 88 | + // Get 1st slide |
| 89 | + Slide slide = presentation.GetSlides()[0]; |
| 90 | + // Get comments of a slide |
| 91 | + var comments = slide.GetComments(); |
| 92 | + // Remove 1st comment |
| 93 | + comments[0].Remove(); |
| 94 | + // Save presentation |
| 95 | + presentation.Save(); |
| 96 | + |
| 97 | + } |
| 98 | + catch (System.Exception ex) |
| 99 | + { |
| 100 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 101 | + } |
| 102 | + } |
| 103 | + /// <summary> |
| 104 | + /// Add comment with new comment author. |
| 105 | + /// </summary> |
| 106 | + /// <param name="documentDirectory">The directory where the PowerPoint presentation is located. Default is 'Presentations/Existing'.</param> |
| 107 | + /// <param name="filename">The name of the PowerPoint file. Default is 'test.pptx'.</param> |
| 108 | + |
| 109 | + public void AddCommentWithExistingCommentAuthor(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 110 | + { |
| 111 | + try |
| 112 | + { |
| 113 | + // Create instance of presentation |
| 114 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 115 | + // Get 1st slide |
| 116 | + Slide slide = presentation.GetSlides()[0]; |
| 117 | + // Create comment |
| 118 | + Comment comment1 = new Comment(); |
| 119 | + // Get existing saved comment author |
| 120 | + CommentAuthor author = presentation.GetCommentAuthors()[0]; |
| 121 | + // Set authorId |
| 122 | + comment1.AuthorId = author.Id; |
| 123 | + // Add comment content |
| 124 | + comment1.Text = "2nd Programmatic comment in an existing presentation"; |
| 125 | + // Set comment time |
| 126 | + comment1.InsertedAt = DateTime.Now; |
| 127 | + // Add comment to slide |
| 128 | + slide.AddComment(comment1); |
| 129 | + // Save presentation |
| 130 | + presentation.Save(); |
| 131 | + |
| 132 | + } |
| 133 | + catch (System.Exception ex) |
| 134 | + { |
| 135 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments