C# 如何向ppt中的特定幻灯片添加注释
目前,我有一项任务,使用openxml将自定义注释插入到ppt的特定幻灯片中。从引用这个。我试图在幻灯片中添加注释,但出现以下错误C# 如何向ppt中的特定幻灯片添加注释,c#,powerpoint,openxml,openxml-sdk,C#,Powerpoint,Openxml,Openxml Sdk,目前,我有一项任务,使用openxml将自定义注释插入到ppt的特定幻灯片中。从引用这个。我试图在幻灯片中添加注释,但出现以下错误 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 conte
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;
}
下面的代码适用于我,但在幻灯片上添加或附加注释后,如果您装饰注释的文本,那么在下次阅读时,它将给出错误 下面的代码适用于我,但在幻灯片上添加或附加注释后,如果您装饰注释的文本,那么在下次阅读时,它将给出错误
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;
}
string relId=“rId”+(索引+1);
使用(PresentationDocument ppt=PresentationDocument.Open(docName,false))
{
PresentationPart=ppt.PresentationPart;
OpenXmlElementList slideIds=part.Presentation.SlideIdList.ChildElements;
relId=(slideIds[index]作为SlideId).RelationshipId;
}
使用(PresentationDocument ppt=PresentationDocument.Open(docName,true))
{
PresentationPart presentationPart1=ppt.PresentationPart;
SlidePart slidePart2=(SlidePart)presentationPart1.GetPartById(relId);
注滑动部分注滑动部分1;
字符串existingSlideNote=“”;
如果(slidePart2.NotesSlidePart!=null)
{
//将新注释附加到现有注释。
existingSlideNote=slidePart2.NotesSlidePart.NotesSlide.InnerText+“\n”;
var val=(NotesSlidePart)slidePart2.GetPartById(relId);
notesSlidePart1=slidePart2.AddPart(val,relId);
}
其他的
{
//向幻灯片中添加新注释。
notesSlidePart1=slidePart2.AddNewPart(relId);
}
NotesSlide NotesSlide=新的NotesSlide(
新数据(新形状视图)(
新的P.NonVisualGroupShapeProperties(
新的P.NonVisualDrawingProperties(){Id=(UINT32值)1U,Name=“”},
新的P.非可视组ShapedRawingProperties(),
新应用程序NonVisualDrawingProperties()),
新的GroupShapeProperties(新的A.TransformGroup()),
新P.形状(
新P.非视觉形状属性(
新的P.NonVisualDrawingProperties(){Id=(UInt32Value)2U,Name=“幻灯片图像占位符1”},
新的P.NonVisualShapeDrawingProperties(新的A.ShapeLocks(){NoGrouping=true,NoRotation=true,NoChangeAspect=true}),
新应用程序NonVisualDrawingProperties(新占位符形状(){Type=PlaceholderValues.SlideImage}),
新的P.ShapeProperties()),
新P.形状(
新P.非视觉形状属性(
新的P.NonVisualDrawingProperties(){Id=(uint32值)3U,Name=“Notes Placeholder 2”},
新的P.NonVisualShapeDrawingProperties(新的A.ShapeLocks(){NoGrouping=true}),
新应用程序NonVisualDrawingProperties(新占位符形状(){Type=PlaceholderValues.Body,Index=(UInt32Value)1U}),
新的P.ShapeProperties(),
新P.TextBody(
新的A.BodyProperties(),
新的A.ListStyle(),
新的A.段(
新跑步(
新的A.RunProperties(){Language=“en-US”,Dirty=false},
新的A.Text(){Text=existingSlideNote+“值已更新”}),
新的A.EndParagraphRunProperties(){Language=“en-US”,Dirty=false})
))),
新的ColorMapOverride(新的A.MasterColorMapping());
notesSlidePart1.NotesSlide=NotesSlide;
}
下面的代码对我很有用,但在幻灯片上添加或附加注释后,如果您装饰注释的文本,那么在下次阅读时,它将给出错误信息
下面的代码适用于我,但在幻灯片上添加或附加注释后,如果您装饰注释的文本,那么在下次阅读时,它将给出错误
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;
}
string relId=“rId”+(索引+1);
使用(PresentationDocument ppt=PresentationDocument.Open(docName,false))
{
PresentationPart=ppt.PresentationPart;
OpenXmlElementList slideIds=part.Presentation.SlideIdList.ChildElements;
relId=(slideIds[index]作为SlideId).RelationshipId;
}
使用(PresentationDocument ppt=PresentationDocument.Open(docName,true))
{
PresentationPart presentationPart1=ppt.PresentationPart;
SlidePart slidePart2=(SlidePart)presentationPart1.GetPartById(relId);
注滑动部分注滑动部分1;
字符串existingSlideNote=“”;
如果(slidePart2.NotesSlidePart!=null)
{
//将新注释附加到现有注释。
existingSlideNote=slidePart2.NotesSlidePart.NotesSlide.InnerText+“\n”;
var val=(NotesSlidePart)slidePart2.GetPartById(relId);
notesSlidePart1=slidePart2.AddPart(val,relId);
}
其他的
{
//向幻灯片中添加新注释。
notesSlidePart1=slidePart2.AddNewPart(relId);
}
NotesSlide NotesSlide=新的NotesSlide(
新数据(新形状视图)(
新的P.NonVisualGroupShapeProperties(
新的P.NonVisualDrawingProperties(){Id=(UINT32值)1U,Name=“”},
新的P.非可视组ShapedRawingProperties(),
新应用程序NonVisualDrawingProperties()),
新的GroupShapeProperties(新的A.TransformGroup()),
新P.形状(
新P.非视觉形状属性(
新的P.NonVisualDrawingProperties(){Id=(UInt32Value)2U,Name=“幻灯片图像占位符1”},
新的P.NonVisualShapeDrawingProperties(新的A.ShapeLocks(){NoGrouping=true,NoRotation=true,NoChangeAspect=true}),
新应用程序NonVisualDrawingProperties(新占位符形状(){Type=PlaceholderValues.SlideImage}),
新的P.ShapeProperties()),
新P.形状(
新P.非视觉形状属性(
新的P.NonVisualDrawingProperties(){Id=(UINT32值)3U,Name=“注释
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);
}