Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently I have a task that insert custom note to a particular slide of a ppt using openxml. from referencing this link. I am trying to add note to a slide but I am getting the following Error.
C#
   Error Message :Only one instance of the type is allowed for this parent.
Stack Trace:
at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.InitPart[T](T newPart, String contentType, String id)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.InitPart[T](T newPart, String contentType)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPartInternal[T]()
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart[T]()
   at AddNoteToPPT.Program.AddNote(String docName, Int32 index) in d:\Projects\Task\Project\AddNoteToPPT\AddNoteToPPT\Program.cs:line 45


Here is my working copy

C#
NotesSlidePart notesSlidePart1 = slide.AddNewPart<NotesSlidePart>();
NotesSlide notesSlide = new NotesSlide(
new CommonSlideData(new ShapeTree(
  new P.NonVisualGroupShapeProperties(
  new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
  new P.NonVisualGroupShapeDrawingProperties(),
  new ApplicationNonVisualDrawingProperties()),
  new GroupShapeProperties(new TransformGroup()),
  new P.Shape(
  new P.NonVisualShapeProperties(
	new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "" },
	new P.NonVisualShapeDrawingProperties(new ShapeLocks() { NoGrouping = true }),
	new ApplicationNonVisualDrawingProperties(new PlaceholderShape())),
  new P.ShapeProperties(),
  new P.TextBody(
	new BodyProperties(),
	new ListStyle(),
	new Paragraph(new EndParagraphRunProperties()))))),
new ColorMapOverride(new MasterColorMapping()));
notesSlidePart1.NotesSlide = notesSlide;


using this code I am able to get the content of existing Note.
C#
slide.NotesSlidePart.SlidePart.NotesSlidePart.NotesSlide.InnerText
Posted
Updated 5-Nov-15 2:01am
v6
Comments
F-ES Sitecore 14-Sep-15 11:31am    
You've missed off the error message and only posted the stack trace
Madan Mishra 15-Sep-15 0:55am    
Ya I have update my question You can find the Error Message in question itself
Sinisa Hajnal 5-Nov-15 2:38am    
It sounds like your slide can have only one note. Therefore if it exists you should only update it, not add new one. But this is guessing based on the message. Never tried it.

1 solution

This is working for me please check with this code. Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using P = DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Drawing;

namespace MadanUpdateNotes
{
class Program
{
static void Main(string[] args)
{
string filePath = @"D:\backup\final.pptx";
AddNote(filePath, 0);
}

public static void AddNote(string docName, int index)
{
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
// Get the relationship ID of the first slide.
PresentationPart part = ppt.PresentationPart;
OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;

string relId = (slideIds[index] as SlideId).RelationshipId;

// Get the slide part from the relationship ID.
SlidePart slide = (SlidePart)part.GetPartById(relId);

// Build a StringBuilder object.
StringBuilder paragraphText = new StringBuilder();

Console.WriteLine("***************************************************");
Console.WriteLine("Enter the text you want to update");
Console.WriteLine("***************************************************");
string Userdata = Console.ReadLine();
Console.WriteLine("\n");
NotesSlidePart notesSlidePart1;
string existingSlideNote = string.Empty;
string finalNotes = string.Empty;
string Message = string.Empty;

if (slide.NotesSlidePart != null)
{
//Appened new note to existing note.
existingSlideNote = slide.NotesSlidePart.NotesSlide.InnerText + "\n";
Console.WriteLine("***************************************************");
Console.WriteLine("Available notes in this slide is : {0}", slide.NotesSlidePart.NotesSlide.InnerText);// Get the inner text of the slide:
Console.WriteLine("***************************************************");
Console.WriteLine("\n");
Console.WriteLine("***************************************************");
Console.WriteLine("Do you want to Override/Append(Y/N):");
Console.WriteLine("***************************************************");
string YesOrNo = Console.ReadLine();
Console.WriteLine("\n");

if (YesOrNo.ToLower() == "y")
{
finalNotes = Userdata;
Message = "Notes overrided successfully";
}
else
{
finalNotes = existingSlideNote + Userdata;
Message = "Notes appended successfully";
}

var val = slide.NotesSlidePart;
notesSlidePart1 = slide.AddPart<notesslidepart>(val);
}
else
{
//Add a new noteto a slide.
notesSlidePart1 = slide.AddNewPart<notesslidepart>(relId);
finalNotes = Userdata;
Message = "Notes added successfully";
}

NotesSlide notesSlide = new NotesSlide(
new CommonSlideData(new ShapeTree(
new P.NonVisualGroupShapeProperties(
new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
new P.NonVisualGroupShapeDrawingProperties(),
new ApplicationNonVisualDrawingProperties()),
new GroupShapeProperties(new A.TransformGroup()),
new P.Shape(
new P.NonVisualShapeProperties(
new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Slide Image Placeholder 1" },
new P.NonVisualShapeDrawingProperties(new A.ShapeLocks() { NoGrouping = true, NoRotation = true, NoChangeAspect = true }),
new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.SlideImage })),
new P.ShapeProperties()),
new P.Shape(
new P.NonVisualShapeProperties(
new P.NonVisualDrawingProperties() { Id = (UInt32Value)3U, Name = "Notes Placeholder 2" },
new P.NonVisualShapeDrawingProperties(new A.ShapeLocks() { NoGrouping = true }),
new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Body, Index = (UInt32Value)1U })),
new P.ShapeProperties(),
new P.TextBody(
new A.BodyProperties(),
new A.ListStyle(),
new A.Paragraph(
new A.Run(
new A.RunProperties() { Language = "en-US", Dirty = false },
new A.Text() { Text = finalNotes }),
new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false }))
))),
new ColorMapOverride(new A.MasterColorMapping()));

notesSlidePart1.NotesSlide = notesSlide;
Console.WriteLine("***************************************************");
Console.WriteLine(Message);
Console.WriteLine("***************************************************");
Console.ReadLine();

}
}
}
}
 
Share this answer
 
v3
Comments
Madan Mishra 5-Nov-15 6:20am    
woow u copy my answer from stackoveflow to answer my question only
http://stackoverflow.com/questions/32562130/how-to-add-notes-to-a-particular-slide-in-a-ppt/33520335#33520335
Richard Deeming 5-Nov-15 9:46am    
You edited that answer, but it was originally posted by a user called Boobalan[^]. That hardly counts as "your" answer.
Madan Mishra 6-Nov-15 0:21am    
hahahha can you go through the date and time on which the answers were posted
Richard Deeming 6-Nov-15 7:20am    
The revision history[^] of the StackOverflow answer you linked to is pretty clear:

* The answer was posted by "Boobalan" on 4th Novemeber at 11:11 UTC;
* It was edited by "Boobalan" at 11:15, and again at 11:21;
* You then edited it at 11:50 to tidy up the spacing on the code block;

Just because you were the last person to edit the answer doesn't mean it's "your" answer.

If you're going to accuse someone of stealing "your" answer, then you need to link to an answer which you posted.
Madan Mishra 9-Nov-15 0:43am    
There are 2 answer for the same question. Had you checked that on what date and time it was posted. if not check the same.
If you have still problem then go to the https://social.msdn.microsoft.com/Forums/en-US/8619969b-d340-4c4e-8eb4-82d38486d88e/how-to-add-notes-to-a-pirticular-slide-in-a-ppt?forum=csharpgeneral link there is only one answer was posted that's mine.
By the way why should I discuses these thing the person who answer my question he knows from where he brought that answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900