-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathProgram.cs
More file actions
104 lines (89 loc) · 4.28 KB
/
Program.cs
File metadata and controls
104 lines (89 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Common;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Office2016.Presentation.Command;
using DocumentFormat.OpenXml.Office2021.PowerPoint.Comment;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using System;
using System.IO;
using System.Linq;
using Comment = DocumentFormat.OpenXml.Office2021.PowerPoint.Comment.Comment;
using CommentList = DocumentFormat.OpenXml.Office2021.PowerPoint.Comment.CommentList;
using Text = DocumentFormat.OpenXml.Drawing.Text;
namespace PowerPointModernCommentSample
{
internal class Program
{
public static void Main(string[] args)
{
try
{
if (args.Length == 0)
{
Console.WriteLine("Usage: PowerPointModernCommentSample.exe <filepath>");
return;
}
FileInfo fileInfo = new FileInfo(args[0]);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
// Create a presentation
using PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName);
// create missing PowerPointAuthorsPart if it is null
if (presentationDocument?.PresentationPart?.authorsPart is null)
{
presentationDocument?.PresentationPart.AddNewPart<PowerPointAuthorsPart>();
}
// Add missing AuthorList if it is null
if (presentationDocument?.PresentationPart?.authorsPart?.AuthorList is null)
{
presentationDocument.PresentationPart.authorsPart.AuthorList = new AuthorList();
}
// In production you will need to retrieve actual user information
string authorId = string.Concat("{", Guid.NewGuid(), "}");
presentationDocument.PresentationPart.authorsPart.AuthorList.AddChild(new Author()
{ Id = authorId, Name = "Jose Contoso", Initials = "JC", UserId = "jose@contosot.com::9ed1aa81-8d9b-4a15-b989-8cbbfd4c4b3e", ProviderId = "AD" });
// Get the Id of the slide to add the comment to
SlideId slideId = presentationDocument.PresentationPart.Presentation.SlideIdList.Elements<SlideId>().First();
Random ran = new Random();
UInt32Value cid = Convert.ToUInt32(ran.Next(100000000, 999999999));
// Add a comment to the slide
PowerPointCommentPart powerPointCommentPart = presentationDocument.PresentationPart.SlideParts.First().AddNewPart<PowerPointCommentPart>();
powerPointCommentPart.CommentList = new CommentList(
new Comment(
new SlideMonikerList(
new DocumentMoniker(),
new SlideMoniker()
{
CId = cid,
SldId = slideId.Id,
}),
new TextBodyType(
new BodyProperties(),
new ListStyle(),
new Paragraph(new Run(new Text("Hello World!")))))
{
Id = string.Concat("{", Guid.NewGuid(), "}"),
AuthorId = authorId,
Created = DateTime.Now,
});
SlidePart slidePart = presentationDocument.PresentationPart.SlideParts.First();
Slide slide = slidePart.Slide;
slide.AppendChild(
new PresentationExtensionList(
new PresentationExtension(
new CommentRelationship()
{ Id = slidePart.GetIdOfPart(powerPointCommentPart) })
{ Uri = "{6950BFC3-D8DA-4A85-94F7-54DA5524770B}" }));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}