Skip to content

Commit 38e689c

Browse files
Updated as v24.5.0
1 parent a4283d3 commit 38e689c

File tree

5 files changed

+131
-3
lines changed

5 files changed

+131
-3
lines changed

FileFormat.Slides-Test/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ static void Main()
384384
slide.AddComment(comment1);
385385
presentation.Save();
386386
*/
387+
/*
388+
Presentation presentation = Presentation.Open("D:\\AsposeSampleData\\test.pptx");
389+
Slide slide = presentation.GetSlides()[0];
390+
391+
//slide.AddNote("Serious Note");
392+
//presentation.GetSlides()[1].AddNote("Light Note");
393+
//presentation.SaveAllNotesToTextFile("D:\\AsposeSampleData\\Notes.txt");
394+
presentation.Save();*/
387395
}
388396

389397
}

FileFormat.Slides.Facade/PresentationDocumentFacade.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,37 @@ public void ExtractAndSaveImages (string outputFolder)
458458

459459
}
460460

461+
public String GetSlideRelationshipId(SlidePart slidePart)
462+
{
463+
return _PresentationPart.GetIdOfPart(slidePart);
464+
}
465+
public void SaveAllNotesToTextFile(string filePath)
466+
{
467+
// Create or overwrite the file
468+
using (StreamWriter writer = new StreamWriter(filePath))
469+
{
470+
int slideIndex = 1;
471+
// Iterate through each slide
472+
foreach (SlidePart slidePart in _PresentationPart.SlideParts)
473+
{
474+
// Check if the slide has notes
475+
if (slidePart.NotesSlidePart != null)
476+
{
477+
// Access the notes slide
478+
NotesSlide notesSlide = slidePart.NotesSlidePart.NotesSlide;
479+
480+
// Get text from notes slide
481+
string noteText = notesSlide.Descendants<D.Text>().Select(t => t.Text).FirstOrDefault();
482+
483+
// Write notes text to file
484+
writer.WriteLine($"Slide {slideIndex}:");
485+
writer.WriteLine(noteText);
486+
writer.WriteLine();
487+
slideIndex++;
488+
}
489+
}
490+
}
491+
}
461492
public String RemoveSlide (int index)
462493
{
463494

FileFormat.Slides.Facade/SlideFacade.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class SlideFacade
3535

3636
private SlideCommentsPart _CommentPart;
3737

38+
private NotesSlidePart _NotesPart;
39+
3840
public Slide PresentationSlide { get => _PresentationSlide; set => _PresentationSlide = value; }
3941
public string RelationshipId { get => _RelationshipId; set => _RelationshipId = value; }
4042
public SlidePart SlidePart { get => _SlidePart; set => _SlidePart = value; }
@@ -45,6 +47,7 @@ public class SlideFacade
4547
public List<TableFacade> TableFacades { get => _TableFacades; set => _TableFacades = value; }
4648
public CommentAuthorsPart CommentAuthorPart { get => _CommentAuthorPart; set => _CommentAuthorPart = value; }
4749
public SlideCommentsPart CommentPart { get => _CommentPart; set => _CommentPart = value; }
50+
public NotesSlidePart NotesPart { get => _NotesPart; set => _NotesPart = value; }
4851

4952
public SlideFacade (bool isNewSlide)
5053
{
@@ -210,6 +213,67 @@ public TextShapeFacade AddTextShape ( List<TextSegmentFacade> textSegmentFacades
210213
return textShapeFacade;
211214
}
212215

216+
public void AddNote(String noteText)
217+
{
218+
var relId = _RelationshipId;
219+
220+
NotesSlidePart notesSlidePart1;
221+
string existingSlideNote = noteText;
222+
223+
if (_SlidePart.NotesSlidePart != null)
224+
{
225+
//Appened new note to existing note.
226+
existingSlideNote = _SlidePart.NotesSlidePart.NotesSlide.InnerText + "\n" + noteText;
227+
//var val = (NotesSlidePart)_SlidePart.GetPartById(relId);
228+
//var val = _SlidePart.NotesSlidePart;
229+
notesSlidePart1 = _NotesPart;
230+
}
231+
else
232+
{
233+
//Add a new noteto a slide.
234+
notesSlidePart1 = _SlidePart.AddNewPart<NotesSlidePart>(relId);
235+
}
236+
237+
NotesSlide notesSlide = new NotesSlide(
238+
new CommonSlideData(new ShapeTree(
239+
new P.NonVisualGroupShapeProperties(
240+
new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
241+
new P.NonVisualGroupShapeDrawingProperties(),
242+
new ApplicationNonVisualDrawingProperties()),
243+
new GroupShapeProperties(new D.TransformGroup()),
244+
new P.Shape(
245+
new P.NonVisualShapeProperties(
246+
new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Slide Image Placeholder 1" },
247+
new P.NonVisualShapeDrawingProperties(new D.ShapeLocks() { NoGrouping = true, NoRotation = true, NoChangeAspect = true }),
248+
new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.SlideImage })),
249+
new P.ShapeProperties()),
250+
new P.Shape(
251+
new P.NonVisualShapeProperties(
252+
new P.NonVisualDrawingProperties() { Id = (UInt32Value)3U, Name = "Notes Placeholder 2" },
253+
new P.NonVisualShapeDrawingProperties(new D.ShapeLocks() { NoGrouping = true }),
254+
new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Body, Index = (UInt32Value)1U })),
255+
new P.ShapeProperties(),
256+
new P.TextBody(
257+
new D.BodyProperties(),
258+
new D.ListStyle(),
259+
new D.Paragraph(
260+
new D.Run(
261+
new D.RunProperties() { Language = "en-US", Dirty = false },
262+
new D.Text() { Text = existingSlideNote }),
263+
new D.EndParagraphRunProperties() { Language = "en-US", Dirty = false }))
264+
))),
265+
new ColorMapOverride(new D.MasterColorMapping()));
266+
267+
notesSlidePart1.NotesSlide = notesSlide;
268+
}
269+
public void RemoveNote()
270+
{
271+
if (_SlidePart.NotesSlidePart != null)
272+
{
273+
// Clear the existing notes.
274+
_SlidePart.DeletePart(_SlidePart.NotesSlidePart);
275+
}
276+
}
213277
public void AddImage (ImageFacade picture )
214278
{
215279
_PresentationSlide.CommonSlideData.ShapeTree.Append(picture.Image);

FileFormat.Slides/Presentation.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,16 @@ public List<Slide> GetSlides ()
156156
foreach (var slidepart in doc.PresentationSlideParts)
157157
{
158158
var slide = new Slide(false);
159-
159+
160160
SlideFacade slideFacade = new SlideFacade(false);
161161
slideFacade.TextShapeFacades = TextShapeFacade.PopulateTextShapes(slidepart);
162162
slideFacade.ImagesFacade = ImageFacade.PopulateImages(slidepart);
163163
slideFacade.PresentationSlide = slidepart.Slide;
164164
slideFacade.TableFacades= TableFacade.PopulateTables(slidepart);
165165
slideFacade.SlidePart = slidepart;
166166
slideFacade.CommentPart = slidepart.SlideCommentsPart;
167+
slideFacade.NotesPart = slidepart.NotesSlidePart;
168+
slideFacade.RelationshipId = doc.GetSlideRelationshipId(slidepart);
167169
slide.TextShapes = TextShape.GetTextShapes(slideFacade.TextShapeFacades);
168170
slide.Images = Image.GetImages(slideFacade.ImagesFacade);
169171
slide.Tables = Table.GetTables(slideFacade.TableFacades);
@@ -219,6 +221,15 @@ public void InsertSlideAt (int index, Slide slide)
219221
slide.SlideFacade.SlideIndex = index;
220222
doc.InsertSlide(index, slide.SlideFacade);
221223
}
224+
225+
/// <summary>
226+
/// This method exports all existing notes of a PPT/PPTX to TXT file.
227+
/// </summary>
228+
/// <param name="filePath"> File path where to save TXT file</param>
229+
public void SaveAllNotesToTextFile(string filePath)
230+
{
231+
doc.SaveAllNotesToTextFile(filePath);
232+
}
222233
/// <summary>
223234
/// Method to save the new or changed presentation.
224235
/// </summary>

FileFormat.Slides/Slide.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public Slide ()
7878
}
7979
catch (Exception ex)
8080
{
81-
string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Initialing slide");
81+
string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Initializing slide");
8282
throw new Common.FileFormatException(errorMessage, ex);
8383
}
8484

@@ -132,7 +132,21 @@ public void AddTextShapes (TextShape textShape)
132132
throw new Common.FileFormatException(errorMessage, ex);
133133
}
134134
}
135-
135+
/// <summary>
136+
/// Method to add/update note to a slide
137+
/// </summary>
138+
/// <param name="noteText">Text you want to add as note</param>
139+
public void AddNote(String noteText)
140+
{
141+
_SlideFacade.AddNote(noteText);
142+
}
143+
/// <summary>
144+
/// Method to remove Notes of a slide
145+
/// </summary>
146+
public void RemoveNote()
147+
{
148+
_SlideFacade.RemoveNote();
149+
}
136150
public void AddTextShapes (TextShape textShape, List<TextSegment> textSegments)
137151
{
138152
try

0 commit comments

Comments
 (0)