Skip to content

Commit ec28405

Browse files
authored
Add Sample Code (#1087)
Adds samples for: - AnimatedModel3D - DocumentTask - NamedSheetView - SVG
1 parent 3c774d4 commit ec28405

18 files changed

+842
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>$(SamplesFrameworks)</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\DocumentFormat.OpenXml\DocumentFormat.OpenXml.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

samples/AnimatedModel3DExample/Program.cs

+313
Large diffs are not rendered by default.
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Animated Model 3D
2+
3+
This sample code shows how to add an Animated Model 3D to a PowerPoint file. When adding an Animated Model 3D to a presentation, the SDK needs a .png file of the 3D image and a .glb file, which is a 3D file format for rendering in 3D.
4+
5+
The important method in Program.cs is:
6+
```csharp
7+
public static void InsertAnimatedModel3D(string pptxPath, string pngPath, string glbPath)
8+
```

samples/Directory.Build.props

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<Import Project="..\Directory.Build.props" />
4+
5+
<PropertyGroup>
6+
<OutputType>Exe</OutputType>
7+
<TargetFrameworks>$(SamplesFrameworks)</TargetFrameworks>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
</Project>

samples/Directory.Build.targets

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
4+
<ItemGroup>
5+
<Compile Include="$(MSBuildThisFileDirectory)common/*.cs" />
6+
</ItemGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\DocumentFormat.OpenXml\DocumentFormat.OpenXml.csproj" />
10+
</ItemGroup>
11+
12+
<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.targets" />
13+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\src\DocumentFormat.OpenXml.Features\DocumentFormat.OpenXml.Features.csproj" />
5+
</ItemGroup>
6+
7+
</Project>
+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using DocumentFormat.OpenXml;
5+
using DocumentFormat.OpenXml.Features;
6+
using DocumentFormat.OpenXml.Office2021.DocumentTasks;
7+
using DocumentFormat.OpenXml.Packaging;
8+
using DocumentFormat.OpenXml.Wordprocessing;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.IO;
12+
using System.Linq;
13+
using System.Text;
14+
15+
namespace DocumentTaskSample
16+
{
17+
internal class Program
18+
{
19+
private static void Main(string[] args)
20+
{
21+
if (args.Length < 1)
22+
{
23+
Common.ExampleUtilities.ShowHelp(
24+
new string[]
25+
{
26+
"DocumentTaskSample: ",
27+
"Usage: DocumentTaskSample <filename>",
28+
"Where: <filename> is the file in which to add a document task.",
29+
string.Empty,
30+
});
31+
}
32+
else if (Common.ExampleUtilities.CheckIfFilesExist(args))
33+
{
34+
AddDocumentTask(args[0]);
35+
}
36+
}
37+
38+
private static void AddDocumentTask(string filePath)
39+
{
40+
Console.WriteLine("Processing presentation: {0}", filePath);
41+
42+
using WordprocessingDocument docPackage = WordprocessingDocument.Open(filePath, true);
43+
string newFn = filePath.Replace(".docx", "_new.docx");
44+
45+
using WordprocessingDocument newDocumentPackage = (WordprocessingDocument)docPackage.SaveAs(newFn.ToString());
46+
newDocumentPackage.AddParagraphIdFeature();
47+
MainDocumentPart mdp = newDocumentPackage.MainDocumentPart;
48+
49+
string strCommentId = "3";
50+
string comment = " Here's another sentence that is just too long. Shorten it please.";
51+
string run = "The introduction to this article.";
52+
53+
// userids, providers and names will have to be programmatically accessed via directory services and must be valid. These are just for example
54+
User tony = new("S::[email protected]::3063813b-f01d-4030-9808-501a178e7963", "John Doe", "AD", "@John Doe", "[email protected]");
55+
User bruce = new("S::[email protected]::ec6240b1-52a3-46dd-9697-ef7bcc7a29e8", "Jane Doe", "AD", "@Jane Doe", "[email protected]");
56+
57+
AddNewParagraphRunWithComment(mdp, strCommentId, run);
58+
59+
AddMentionComment(mdp, strCommentId, bruce.Mention, comment, tony, bruce);
60+
61+
string taskStr = string.Concat(bruce.Mention, " ", comment);
62+
AddNewTask(mdp, taskStr, bruce, tony);
63+
}
64+
65+
private static void AddNewTask(MainDocumentPart mdp, string title, User assignee, User assigner)
66+
{
67+
// TODO: userids, providers and names will have to be programmatically accessed via directory services.
68+
// These are just examples to show what is expected.
69+
mdp.AddNewPart<DocumentTasksPart>();
70+
DocumentTasksPart taskPart = mdp.DocumentTasksPart;
71+
taskPart.Tasks = new Tasks();
72+
Tasks taskRoot = taskPart.Tasks;
73+
string guidEventId = Guid.NewGuid().ToString();
74+
string guidTaskId = Guid.NewGuid().ToString();
75+
string commonAnchorId = "546836446";
76+
77+
taskRoot.AppendChild<Task>(
78+
new Task(
79+
new TaskAnchor(
80+
new CommentAnchor() { Id = commonAnchorId }),
81+
new TaskHistory(
82+
new TaskHistoryEvent(
83+
new AttributionTaskUser()
84+
{ UserId = assigner.UserId, UserProvider = assigner.DirectoryProvider, UserName = assigner.UserName },
85+
new TaskAnchor(
86+
new CommentAnchor()
87+
{ Id = commonAnchorId }),
88+
new TaskCreateEventInfo())
89+
{ Id = guidEventId, Time = DateTime.Now },
90+
new TaskHistoryEvent(
91+
new AttributionTaskUser()
92+
{ UserId = assigner.UserId, UserProvider = assigner.DirectoryProvider, UserName = assigner.UserName },
93+
new TaskAnchor(
94+
new CommentAnchor()
95+
{ Id = commonAnchorId }),
96+
new AssignTaskUser()
97+
{ UserId = assignee.UserId, UserProvider = assignee.DirectoryProvider, UserName = assignee.UserName })
98+
{ Id = guidEventId, Time = DateTime.Now },
99+
new TaskHistoryEvent(
100+
new AttributionTaskUser() { UserId = assigner.UserId, UserProvider = assigner.DirectoryProvider, UserName = assigner.UserName },
101+
new TaskAnchor(
102+
new CommentAnchor() { Id = commonAnchorId }),
103+
new TaskTitleEventInfo() { Title = title })
104+
{ Id = guidEventId, Time = DateTime.Now }))
105+
{ Id = guidTaskId });
106+
}
107+
108+
private static void AddNewParagraphRunWithComment(MainDocumentPart mdp, string strCommentId, string strParagraphText)
109+
{
110+
mdp.Document.Body.AppendChild<Paragraph>(
111+
new Paragraph(
112+
113+
// This id MUST be unique in the comments part.
114+
new CommentRangeStart() { Id = strCommentId },
115+
new Run(
116+
new Text(strParagraphText)),
117+
new CommentRangeEnd() { Id = strCommentId },
118+
new Run(new CommentReference() { Id = strCommentId })));
119+
}
120+
121+
private static void AddMentionComment(MainDocumentPart mdp, string strCommentId, string mention, string commentText, User mentioner, User mentionee)
122+
{
123+
mdp.AddNewPart<WordprocessingCommentsPart>();
124+
mdp.WordprocessingCommentsPart.Comments = new Comments();
125+
Comments comments = mdp.WordprocessingCommentsPart.Comments;
126+
127+
comments.AppendChild<Comment>(
128+
new Comment(
129+
new Paragraph(
130+
new Run(
131+
new RunProperties(
132+
new Color() { Val = "2B579A" },
133+
new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "E6E6E6" }),
134+
new FieldChar() { FieldCharType = FieldCharValues.Begin }),
135+
new Run(
136+
new FieldCode(" HYPERLINK \"mailto:" + mentionee.Email + "\"")
137+
{ Space = SpaceProcessingModeValues.Preserve }),
138+
new BookmarkStart() { Id = "2", Name = "_@_0FD9AD1E39C946AB9F9E1352162C9910Z" },
139+
new Run(
140+
new RunProperties(
141+
new Color() { Val = "2B579A" },
142+
new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "E6E6E6" }),
143+
new FieldChar() { FieldCharType = FieldCharValues.Separate }),
144+
new BookmarkEnd() { Id = "2" },
145+
new Run(
146+
new RunProperties(
147+
new RunStyle() { Val = "Mention" },
148+
new NoProof()),
149+
new Text(mention)),
150+
new Run(
151+
new RunProperties(
152+
new Color() { Val = "2B579A" },
153+
new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "E6E6E6" }),
154+
new FieldChar()
155+
{ FieldCharType = FieldCharValues.End }),
156+
new Run(
157+
new Text(commentText) { Space = SpaceProcessingModeValues.Preserve }),
158+
new Run(
159+
new AnnotationReferenceMark()))
160+
161+
// These ids MUST be unique in the document.
162+
{ TextId = "FFFFFF04" })
163+
164+
// This id MUST be unique in the comments part.
165+
{ Id = strCommentId, Author = mentioner.UserName, Date = DateTime.Now, Initials = mentioner.UserName.Substring(0, 1) + mentioner.UserName.Substring(mentioner.UserName.IndexOf(" ") + 1, 1) });
166+
}
167+
}
168+
}

samples/DocumentTaskExample/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Document Task
2+
3+
The sample code shows how to add a DocumentTask to an Excel file with the SDK.
4+
5+
The important methods in Program.cs are:
6+
```csharp
7+
// add a new task to a document
8+
private static void AddNewTask(MainDocumentPart mdp, string title, User assignee, User assigner)
9+
10+
// Add a new paragraph with a comment
11+
private static void AddNewParagraphRunWithComment(MainDocumentPart mdp, string strCommentId, string strParagraphText)
12+
13+
// Add a comment that mentions another user
14+
private static void AddMentionComment(MainDocumentPart mdp, string strCommentId, string mention, string commentText, User mentioner, User mentionee, RandomParagraphIdGenerator pIdGener)
15+
```

samples/DocumentTaskExample/User.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DocumentTaskSample
8+
{
9+
internal class User
10+
{
11+
public User(string userId, string userName, string directoryProvider, string mention, string email)
12+
{
13+
UserId = userId;
14+
UserName = userName;
15+
DirectoryProvider = directoryProvider;
16+
Mention = mention;
17+
Email = email;
18+
}
19+
20+
public string UserId { get; set; }
21+
22+
public string UserName { get; set; }
23+
24+
public string DirectoryProvider { get; set; }
25+
26+
public string Mention { get; set; }
27+
28+
public string Email { get; set; }
29+
}
30+
}

samples/IsolatedStorageExceptionWorkaround/IsolatedStorageExceptionWorkaround.csproj

-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>$(SamplesFrameworks)</TargetFrameworks>
6-
<IsPackable>false</IsPackable>
74
<NoWarn>$(NoWarn);CA1303</NoWarn>
85
</PropertyGroup>
96

@@ -20,8 +17,4 @@
2017
</Otherwise>
2118
</Choose>
2219

23-
<ItemGroup>
24-
<ProjectReference Include="..\..\src\DocumentFormat.OpenXml\DocumentFormat.OpenXml.csproj" />
25-
</ItemGroup>
26-
2720
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
</Project>

samples/NamedSheetView/Program.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
using DocumentFormat.OpenXml;
4+
using DocumentFormat.OpenXml.Office2013.ExcelAc;
5+
using DocumentFormat.OpenXml.Office2021.Excel.NamedSheetViews;
6+
using DocumentFormat.OpenXml.Packaging;
7+
using DocumentFormat.OpenXml.Spreadsheet;
8+
using System;
9+
using System.IO;
10+
using System.Linq;
11+
12+
namespace AddNamedSheetView
13+
{
14+
public class Program
15+
{
16+
public static void Main(string[] args)
17+
{
18+
if (args.Length < 1)
19+
{
20+
Common.ExampleUtilities.ShowHelp(new string[]
21+
{
22+
"NamedSheetView: ",
23+
"Usage: NamedSheetView <filename>",
24+
"Where: <filename> is the .xlsx file in which to add a named sheet view.",
25+
});
26+
}
27+
else if (Common.ExampleUtilities.CheckIfFilesExist(args))
28+
{
29+
InsertNamedSheetView(args[0]);
30+
}
31+
}
32+
33+
public static void InsertNamedSheetView(string xlsxPath)
34+
{
35+
if (xlsxPath == null)
36+
{
37+
throw new ArgumentNullException(nameof(xlsxPath));
38+
}
39+
40+
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(xlsxPath, true))
41+
{
42+
WorksheetPart worksheetPart = spreadsheetDocument.WorkbookPart.GetPartsOfType<WorksheetPart>().First();
43+
NamedSheetViewsPart namedSheetViewsPart = worksheetPart.AddNewPart<NamedSheetViewsPart>();
44+
45+
NamedSheetView namedSheetView = new NamedSheetView();
46+
namedSheetView.Id = "{" + System.Guid.NewGuid().ToString().ToUpper() + "}";
47+
namedSheetView.Name = "testview";
48+
49+
namedSheetViewsPart.NamedSheetViews = new NamedSheetViews(
50+
namedSheetView);
51+
namedSheetViewsPart.NamedSheetViews.AddNamespaceDeclaration("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
52+
}
53+
}
54+
}
55+
}

samples/NamedSheetView/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Named Sheet View
2+
3+
This sample code shows how to add a [Named Sheet View](https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/78cf20a1-2551-45c6-86bf-f1e92bd5fc39) to a document with the SDK. Note that Named Sheet Views are only visible in Excel online.
4+
5+
The important method in Program.cs is:
6+
7+
```csharp
8+
public static void InsertNamedSheetView(string xlsxPath)
9+
```

0 commit comments

Comments
 (0)