如何在ppt中的特定幻灯片上添加注释
How to add Notes to a particular slide in a ppt
目前我有一项任务是使用 openxml 将自定义注释插入到 ppt 的特定幻灯片中。从引用这个 link。我正在尝试向幻灯片添加注释,但出现以下错误。
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
这是我的工作副本
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();
// Get the inner text of the slide:
IEnumerable<A.Text> texts = slide.Slide.Descendants<A.Text>();
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;
}
使用此代码我能够获取现有注释的内容。
slide.NotesSlidePart.SlidePart.NotesSlidePart.NotesSlide.InnerText
下面的代码适用于我,但是在向幻灯片添加或附加注释后,如果你修饰注释文本,那么在下一次阅读时它会出错
下面的代码对我有用,但在向幻灯片添加或附加注释后,如果你装饰注释文本,那么在下一次阅读时它会给出错误
string relId = "rId" + (index + 1);
using (PresentationDocument ppt = PresentationDocument.Open(docName, false))
{
PresentationPart part = ppt.PresentationPart;
OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
relId = (slideIds[index] as SlideId).RelationshipId;
}
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
PresentationPart presentationPart1 = ppt.PresentationPart;
SlidePart slidePart2 = (SlidePart)presentationPart1.GetPartById(relId);
NotesSlidePart notesSlidePart1;
string existingSlideNote = "";
if (slidePart2.NotesSlidePart != null)
{
//Appened new note to existing note.
existingSlideNote = slidePart2.NotesSlidePart.NotesSlide.InnerText + "\n";
var val = (NotesSlidePart)slidePart2.GetPartById(relId);
notesSlidePart1 = slidePart2.AddPart<NotesSlidePart>(val, relId);
}
else
{
//Add a new noteto a slide.
notesSlidePart1 = slidePart2.AddNewPart<NotesSlidePart>(relId);
}
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 = existingSlideNote + "Value Updated" }),
new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false }))
))),
new ColorMapOverride(new A.MasterColorMapping()));
notesSlidePart1.NotesSlide = notesSlide;
}
请参考下面的代码,即使您在更新或将注释添加到幻灯片后修饰注释文本,该代码也能正常工作。
if (slidePart2.NotesSlidePart != null)
{
//Appened new note to existing note.
existingSlideNote = slidePart2.NotesSlidePart.NotesSlide.InnerText + "\n";
var val = slidePart2.NotesSlidePart;
notesSlidePart1 = slidePart2.AddPart<NotesSlidePart>(val);
}
else
{
//Add a new noteto a slide.
notesSlidePart1 = slidePart2.AddNewPart<NotesSlidePart>(relId);
}
目前我有一项任务是使用 openxml 将自定义注释插入到 ppt 的特定幻灯片中。从引用这个 link。我正在尝试向幻灯片添加注释,但出现以下错误。
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
这是我的工作副本
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();
// Get the inner text of the slide:
IEnumerable<A.Text> texts = slide.Slide.Descendants<A.Text>();
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;
}
使用此代码我能够获取现有注释的内容。
slide.NotesSlidePart.SlidePart.NotesSlidePart.NotesSlide.InnerText
下面的代码适用于我,但是在向幻灯片添加或附加注释后,如果你修饰注释文本,那么在下一次阅读时它会出错 下面的代码对我有用,但在向幻灯片添加或附加注释后,如果你装饰注释文本,那么在下一次阅读时它会给出错误
string relId = "rId" + (index + 1);
using (PresentationDocument ppt = PresentationDocument.Open(docName, false))
{
PresentationPart part = ppt.PresentationPart;
OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
relId = (slideIds[index] as SlideId).RelationshipId;
}
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
PresentationPart presentationPart1 = ppt.PresentationPart;
SlidePart slidePart2 = (SlidePart)presentationPart1.GetPartById(relId);
NotesSlidePart notesSlidePart1;
string existingSlideNote = "";
if (slidePart2.NotesSlidePart != null)
{
//Appened new note to existing note.
existingSlideNote = slidePart2.NotesSlidePart.NotesSlide.InnerText + "\n";
var val = (NotesSlidePart)slidePart2.GetPartById(relId);
notesSlidePart1 = slidePart2.AddPart<NotesSlidePart>(val, relId);
}
else
{
//Add a new noteto a slide.
notesSlidePart1 = slidePart2.AddNewPart<NotesSlidePart>(relId);
}
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 = existingSlideNote + "Value Updated" }),
new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false }))
))),
new ColorMapOverride(new A.MasterColorMapping()));
notesSlidePart1.NotesSlide = notesSlide;
}
请参考下面的代码,即使您在更新或将注释添加到幻灯片后修饰注释文本,该代码也能正常工作。
if (slidePart2.NotesSlidePart != null)
{
//Appened new note to existing note.
existingSlideNote = slidePart2.NotesSlidePart.NotesSlide.InnerText + "\n";
var val = slidePart2.NotesSlidePart;
notesSlidePart1 = slidePart2.AddPart<NotesSlidePart>(val);
}
else
{
//Add a new noteto a slide.
notesSlidePart1 = slidePart2.AddNewPart<NotesSlidePart>(relId);
}