|
| 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 | +} |
0 commit comments