如何在ppt中为特定幻灯片添加Notes? [英] How to add Notes to a particular slide in a ppt?

查看:2022
本文介绍了如何在ppt中为特定幻灯片添加Notes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一项任务是使用openxml将自定义注释插入到ppt的特定幻灯片中。引用此链接。我正在尝试为幻灯片添加注释但是我收到以下错误。

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.

   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

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;





使用此代码我可以获得现有Note的内容。



using this code I am able to get the content of existing Note.

slide.NotesSlidePart.SlidePart.NotesSlidePart.NotesSlide.InnerText

推荐答案

这是为我工作,请检查此代码。谢谢



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用DocumentFormat.OpenXml.Packaging;

使用P = DocumentFormat.OpenXml.Presentation;

使用A = DocumentFormat.OpenXml.Drawing;

使用DocumentFormat.OpenXml;

使用DocumentFormat.OpenXml .Presentation;

使用DocumentFormat.OpenXml.Drawing;



命名空间MadanUpdateNotes

{

class program

{

static void Main(string [] args)

{

string filePath = @D:\\\ upsup \final.pptx;

AddNote(filePath,0);

}



public static void AddNote(string docName,int index)

{

using(PresentationDocument ppt = PresentationDocument.Open(docName,true))

{

//获取第一张幻灯片的关系ID。

PresentationPart part = ppt.PresentationPart;

OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;



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



//从关系ID中获取幻灯片部分。

SlidePart slide =(SlidePart)part.GetPartById(relId);



//构建一个StringBuilder对象。

StringBuilder paragraphTe xt = new StringBuilder();



Console.WriteLine(********************** *****************************);

Console.WriteLine(输入你想要的文字更新);

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)

{

//为现有笔记添加新笔记。

existingSlideNote = slide.NotesSlidePart.NotesSlide.InnerText +\ n;

Console.WriteLine(********************* ******************************);

Console.WriteLine(此处的可用备注slide是:{0},slide.NotesSlidePart.NotesSlide.InnerText); //获取幻灯片的内部文本:

Console.WriteLine(********** *****************************************);

Console.WriteLine(\ n);

Console.WriteLine(************************ ***************************);

Console.WriteLine(你要覆盖/追加 (是/否):);

Console.WriteLine(**************************** ***********************);

string YesOrNo = Console.ReadLine();

Console.WriteLine(\ n);



if(YesOrNo.ToLower()==y)

{

finalNotes =用户数据;

消息=注释成功覆盖;

}

其他

{

finalNotes = existingSlideNote + Userdata;

Message =注释成功;

}



var val = slide.NotesSlidePart;

notesSlidePart1 = slide.AddPart< notesslidepart>(val);

}

其他

{

//添加新的注释幻灯片。

notesSlidePart1 = slide.AddNewPart< notesslidepart>(relId);

finalNotes = Userdata;

Message =Notes添加成功;

}



NotesSlide notesSlide = new NotesSlide(

新的CommonSlideData(新的ShapeTree(
$ b $) b新的P.NonVisualGroupShapeProperties(

new P.NonVisualDrawingProperties(){Id =(UInt32Value)1U,Name =},

new P.NonVisualGroupShapeDrawingProperties(),

新的ApplicationNonVisualDrawingProperties()),

新的GroupShapeProperties(新的A.TransformGroup()),

新的P.Shape(

新的P.NonVisualShapePro相关(

新P.NonVisualDrawingProperties(){Id =(UInt32Value)2U,Name =Slide Image placeholder 1},

新P.NonVisualShapeDrawingProperties(新A. ShapeLocks(){NoGrouping = true,NoRotation = true,NoChangeAspect = true}),

new ApplicationNonVisualDrawingProperties(new PlaceholderShape(){Type = PlaceholderValues.SlideImage})),

新P.ShapeProperties()),

新P.Shape(

新P.NonVisualShapeProperties(

新P.NonVisualDrawingProperties(){ Id =(UInt32Value)3U,Name =Notes placeholder 2},

new P.NonVisualShapeDrawingProperties(new A.ShapeLocks(){NoGrouping = true}),

新的ApplicationNonVisu alDrawingProperties(new PlaceholderShape(){Type = PlaceholderValues.Body,Index =(UInt32Value)1U})),

new P.ShapeProperties(),

new P.TextBody (

新A.BodyProperties(),

新A.ListStyle(),

新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(消息);

Console.WriteLine( ******************* **);

Console.ReadLine();



}

}

}

}
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();

}
}
}
}


这篇关于如何在ppt中为特定幻灯片添加Notes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆