|
| 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 | + /// <summary> |
| 12 | + /// Provides C# code examples for creating, reading, and removing comments in a slide of a PowerPoint presentations |
| 13 | + /// using the <a href="https://www.nuget.org/packages/FileFormat.Slides">FileFormat.Slides</a> library. |
| 14 | + /// </summary> |
| 15 | + public class NotesExamples |
| 16 | + { |
| 17 | + private const string newDocsDirectory = "../../../Presentations/New"; |
| 18 | + private const string existingDocsDirectory = "../../../Presentations/Existing"; |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="CommentExamples"/> 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 NotesExamples() |
| 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 | + /// Creates notes in a slide of a PowerPoint presentation. |
| 49 | + /// </summary> |
| 50 | + /// <param name="documentDirectory">The directory where the PowerPoint presentation is located. Default is 'Presentations/Existing'.</param> |
| 51 | + /// <param name="filename">The name of the PowerPoint file. Default is 'test.pptx'.</param> |
| 52 | + |
| 53 | + public void CreateNotesInASlide(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 54 | + { |
| 55 | + try |
| 56 | + { |
| 57 | + // Create instance of presentation |
| 58 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 59 | + // Get 1st slide |
| 60 | + Slide slide = presentation.GetSlides()[0]; |
| 61 | + // Create Note |
| 62 | + slide.AddNote("Here is my first note"); |
| 63 | + // Save presentation |
| 64 | + presentation.Save(); |
| 65 | + |
| 66 | + } |
| 67 | + catch (System.Exception ex) |
| 68 | + { |
| 69 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 70 | + } |
| 71 | + } |
| 72 | + /// <summary> |
| 73 | + /// Remove notes from a slide. |
| 74 | + /// </summary> |
| 75 | + /// <param name="documentDirectory">The directory where the PowerPoint presentation is located. Default is 'Presentations/Existing'.</param> |
| 76 | + /// <param name="filename">The name of the PowerPoint file. Default is 'test.pptx'.</param> |
| 77 | + |
| 78 | + public void RemoveNotesFromASlide(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + // Create instance of presentation |
| 83 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 84 | + // Get 1st slide |
| 85 | + Slide slide = presentation.GetSlides()[0]; |
| 86 | + //Remove notes |
| 87 | + slide.RemoveNote(); |
| 88 | + // Save presentation |
| 89 | + presentation.Save(); |
| 90 | + |
| 91 | + } |
| 92 | + catch (System.Exception ex) |
| 93 | + { |
| 94 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 95 | + } |
| 96 | + } |
| 97 | + /// <summary> |
| 98 | + /// Export all notes of presentation to a text file. |
| 99 | + /// </summary> |
| 100 | + /// <param name="documentDirectory">The directory where the PowerPoint presentation is located. Default is 'Presentations/Existing'.</param> |
| 101 | + /// <param name="filename">The name of the PowerPoint file. Default is 'test.pptx'.</param> |
| 102 | + |
| 103 | + public void ExportNotesToTextFile(string documentDirectory = existingDocsDirectory, string filename = "test.pptx") |
| 104 | + { |
| 105 | + try |
| 106 | + { |
| 107 | + // Create instance of presentation |
| 108 | + Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}"); |
| 109 | + // Export all notes of presentation to a text file |
| 110 | + presentation.SaveAllNotesToTextFile("notes.txt"); |
| 111 | + // Save presentation |
| 112 | + presentation.Save(); |
| 113 | + |
| 114 | + } |
| 115 | + catch (System.Exception ex) |
| 116 | + { |
| 117 | + throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments