public override string ToString() { Comment.Insert(0, "/* "); if (StartNewLineWithStar) { Comment.Replace(Environment.NewLine, Environment.NewLine + " * "); Comment[Comment.Length - 1] = '/'; } else { Comment.Append(" */"); } return(Comment.ToString()); }
private static void GenerateWorksheetCommentsPartContent(WorksheetCommentsPart worksheetCommentsPart, XLWorksheet xlWorksheet) { var comments = new Comments(); var commentList = new CommentList(); var authorsDict = new Dictionary<String, Int32>(); foreach (var c in xlWorksheet.Internals.CellsCollection.GetCells(c => c.HasComment)) { var comment = new Comment {Reference = c.Address.ToStringRelative()}; var authorName = c.Comment.Author; Int32 authorId; if (!authorsDict.TryGetValue(authorName, out authorId)) { authorId = authorsDict.Count; authorsDict.Add(authorName, authorId); } comment.AuthorId = (UInt32)authorId; var commentText = new CommentText(); foreach (var rt in c.Comment) { commentText.Append(GetRun(rt)); } comment.Append(commentText); commentList.Append(comment); } var authors = new Authors(); foreach (var author in authorsDict.Select(a => new Author {Text = a.Key})) { authors.Append(author); } comments.Append(authors); comments.Append(commentList); worksheetCommentsPart.Comments = comments; }
// Adds a comment to the first slide of the presentation document. // The presentation document must contain at least one slide. private static void AddCommentToPresentation(string file, string initials, string name, string text) { using (PresentationDocument doc = PresentationDocument.Open(file, true)) { // Declare a CommentAuthorsPart object. CommentAuthorsPart authorsPart; // Verify that there is an existing comment authors part. if (doc.PresentationPart.CommentAuthorsPart == null) { // If not, add a new one. authorsPart = doc.PresentationPart.AddNewPart <CommentAuthorsPart>(); } else { authorsPart = doc.PresentationPart.CommentAuthorsPart; } // Verify that there is a comment author list in the comment authors part. if (authorsPart.CommentAuthorList == null) { // If not, add a new one. authorsPart.CommentAuthorList = new CommentAuthorList(); } // Declare a new author ID. uint authorId = 0; CommentAuthor author = null; // If there are existing child elements in the comment authors list... if (authorsPart.CommentAuthorList.HasChildren) { // Verify that the author passed in is on the list. var authors = authorsPart.CommentAuthorList.Elements <CommentAuthor>().Where(a => a.Name == name && a.Initials == initials); // If so... if (authors.Any()) { // Assign the new comment author the existing author ID. author = authors.First(); authorId = author.Id; } // If not... if (author == null) { // Assign the author passed in a new ID authorId = authorsPart.CommentAuthorList.Elements <CommentAuthor>().Select(a => a.Id.Value).Max(); } } // If there are no existing child elements in the comment authors list. if (author == null) { authorId++; // Add a new child element(comment author) to the comment author list. author = authorsPart.CommentAuthorList.AppendChild <CommentAuthor> (new CommentAuthor() { Id = authorId, Name = name, Initials = initials, ColorIndex = 0 }); } // Get the first slide, using the GetFirstSlide method. SlidePart slidePart1 = GetFirstSlide(doc); // Declare a comments part. SlideCommentsPart commentsPart; // Verify that there is a comments part in the first slide part. if (slidePart1.GetPartsOfType <SlideCommentsPart>().Count() == 0) { // If not, add a new comments part. commentsPart = slidePart1.AddNewPart <SlideCommentsPart>(); } else { // Else, use the first comments part in the slide part. commentsPart = slidePart1.GetPartsOfType <SlideCommentsPart>().First(); } // If the comment list does not exist. if (commentsPart.CommentList == null) { // Add a new comments list. commentsPart.CommentList = new CommentList(); } // Get the new comment ID. uint commentIdx = author.LastIndex == null ? 1 : author.LastIndex + 1; author.LastIndex = commentIdx; // Add a new comment. Comment comment = commentsPart.CommentList.AppendChild <Comment>( new Comment() { AuthorId = authorId, Index = commentIdx, DateTime = DateTime.Now }); // Add the position child node to the comment element. comment.Append( new Position() { X = 100, Y = 200 }, new Text() { Text = text }); // Save the comment authors part. authorsPart.CommentAuthorList.Save(); // Save the comments part. commentsPart.CommentList.Save(); } }
// Generates content of wordprocessingCommentsPart1. private void GenerateWordprocessingCommentsPart1Content(WordprocessingCommentsPart wordprocessingCommentsPart1) { Comments comments1 = new Comments(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 w15 wp14" } }; comments1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); comments1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); comments1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); comments1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); comments1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); comments1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); comments1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); comments1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); comments1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); comments1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); comments1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); comments1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml"); comments1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); comments1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); comments1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); comments1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); Comment comment1 = new Comment(){ Initials = "M.T", Author = "Masaki Tamura (Pasona Tech)", Date = System.Xml.XmlConvert.ToDateTime("2012-01-19T19:33:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "1" }; Paragraph paragraph3 = new Paragraph(){ RsidParagraphAddition = "009423EF", RsidRunAdditionDefault = "009423EF", ParagraphId = "549EEC1D", TextId = "77777777" }; ParagraphProperties paragraphProperties1 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "a4" }; paragraphProperties1.Append(paragraphStyleId1); Run run6 = new Run(); RunProperties runProperties6 = new RunProperties(); RunStyle runStyle4 = new RunStyle(){ Val = "a3" }; runProperties6.Append(runStyle4); AnnotationReferenceMark annotationReferenceMark1 = new AnnotationReferenceMark(); run6.Append(runProperties6); run6.Append(annotationReferenceMark1); Run run7 = new Run(); RunProperties runProperties7 = new RunProperties(); RunFonts runFonts6 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia }; runProperties7.Append(runFonts6); Text text3 = new Text(); text3.Text = "Comment1"; run7.Append(runProperties7); run7.Append(text3); paragraph3.Append(paragraphProperties1); paragraph3.Append(run6); paragraph3.Append(run7); comment1.Append(paragraph3); Comment comment2 = new Comment(){ Initials = "M.T", Author = "Masaki Tamura (Pasona Tech)", Date = System.Xml.XmlConvert.ToDateTime("2012-01-19T19:33:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "2" }; Paragraph paragraph4 = new Paragraph(){ RsidParagraphAddition = "009423EF", RsidRunAdditionDefault = "009423EF", ParagraphId = "6FD4E96D", TextId = "77777777" }; ParagraphProperties paragraphProperties2 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId(){ Val = "a4" }; paragraphProperties2.Append(paragraphStyleId2); Run run8 = new Run(); RunProperties runProperties8 = new RunProperties(); RunStyle runStyle5 = new RunStyle(){ Val = "a3" }; runProperties8.Append(runStyle5); AnnotationReferenceMark annotationReferenceMark2 = new AnnotationReferenceMark(); run8.Append(runProperties8); run8.Append(annotationReferenceMark2); Run run9 = new Run(); RunProperties runProperties9 = new RunProperties(); RunFonts runFonts7 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia }; runProperties9.Append(runFonts7); Text text4 = new Text(); text4.Text = "Comment2"; run9.Append(runProperties9); run9.Append(text4); paragraph4.Append(paragraphProperties2); paragraph4.Append(run8); paragraph4.Append(run9); comment2.Append(paragraph4); Comment comment3 = new Comment(){ Initials = "M.T", Author = "Masaki Tamura (Pasona Tech)", Date = System.Xml.XmlConvert.ToDateTime("2012-01-19T19:33:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "3" }; Paragraph paragraph5 = new Paragraph(){ RsidParagraphAddition = "009423EF", RsidRunAdditionDefault = "009423EF", ParagraphId = "560BF315", TextId = "77777777" }; ParagraphProperties paragraphProperties3 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId3 = new ParagraphStyleId(){ Val = "a4" }; paragraphProperties3.Append(paragraphStyleId3); Run run10 = new Run(); RunProperties runProperties10 = new RunProperties(); RunStyle runStyle6 = new RunStyle(){ Val = "a3" }; runProperties10.Append(runStyle6); AnnotationReferenceMark annotationReferenceMark3 = new AnnotationReferenceMark(); run10.Append(runProperties10); run10.Append(annotationReferenceMark3); Run run11 = new Run(); RunProperties runProperties11 = new RunProperties(); RunFonts runFonts8 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia }; runProperties11.Append(runFonts8); Text text5 = new Text(); text5.Text = "Comment3"; run11.Append(runProperties11); run11.Append(text5); paragraph5.Append(paragraphProperties3); paragraph5.Append(run10); paragraph5.Append(run11); comment3.Append(paragraph5); comments1.Append(comment1); comments1.Append(comment2); comments1.Append(comment3); wordprocessingCommentsPart1.Comments = comments1; }
public void Append(object value) { Comment.Append(value); }
// Generates content of wordprocessingCommentsPart1. private void GenerateWordprocessingCommentsPart1Content(WordprocessingCommentsPart wordprocessingCommentsPart1) { Comments comments1 = new Comments(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 w15 wp14" } }; comments1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); comments1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); comments1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); comments1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); comments1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); comments1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); comments1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); comments1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); comments1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); comments1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); comments1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); comments1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml"); comments1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); comments1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); comments1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); comments1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); Comment comment1 = new Comment(){ Initials = "M.T", Author = "Masaki Tamura (Pasona Tech)", Date = System.Xml.XmlConvert.ToDateTime("2012-04-04T13:52:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "1" }; Paragraph paragraph2 = new Paragraph(){ RsidParagraphAddition = "00C47703", RsidRunAdditionDefault = "00C47703", ParagraphId = "61B2BF87", TextId = "77777777" }; ParagraphProperties paragraphProperties1 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "CommentText" }; paragraphProperties1.Append(paragraphStyleId1); Run run4 = new Run(); RunProperties runProperties4 = new RunProperties(); RunStyle runStyle3 = new RunStyle(){ Val = "CommentReference" }; runProperties4.Append(runStyle3); AnnotationReferenceMark annotationReferenceMark1 = new AnnotationReferenceMark(); run4.Append(runProperties4); run4.Append(annotationReferenceMark1); Run run5 = new Run(); RunProperties runProperties5 = new RunProperties(); RunFonts runFonts5 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia }; runProperties5.Append(runFonts5); Text text2 = new Text(); text2.Text = "A"; run5.Append(runProperties5); run5.Append(text2); paragraph2.Append(paragraphProperties1); paragraph2.Append(run4); paragraph2.Append(run5); comment1.Append(paragraph2); Comment comment2 = new Comment(){ Initials = "M.T", Author = "Masaki Tamura (Pasona Tech)", Date = System.Xml.XmlConvert.ToDateTime("2012-04-04T13:52:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "2" }; Paragraph paragraph3 = new Paragraph(){ RsidParagraphAddition = "00C47703", RsidRunAdditionDefault = "00C47703", ParagraphId = "216A897E", TextId = "77777777" }; ParagraphProperties paragraphProperties2 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId(){ Val = "CommentText" }; paragraphProperties2.Append(paragraphStyleId2); Run run6 = new Run(); RunProperties runProperties6 = new RunProperties(); RunStyle runStyle4 = new RunStyle(){ Val = "CommentReference" }; runProperties6.Append(runStyle4); AnnotationReferenceMark annotationReferenceMark2 = new AnnotationReferenceMark(); run6.Append(runProperties6); run6.Append(annotationReferenceMark2); Run run7 = new Run(); RunProperties runProperties7 = new RunProperties(); RunFonts runFonts6 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia }; runProperties7.Append(runFonts6); Text text3 = new Text(); text3.Text = "B"; run7.Append(runProperties7); run7.Append(text3); paragraph3.Append(paragraphProperties2); paragraph3.Append(run6); paragraph3.Append(run7); comment2.Append(paragraph3); comments1.Append(comment1); comments1.Append(comment2); wordprocessingCommentsPart1.Comments = comments1; }
// Generates content of worksheetCommentsPart1. private void GenerateWorksheetCommentsPart1Content(WorksheetCommentsPart worksheetCommentsPart1) { Comments comments1 = new Comments(); Authors authors1 = new Authors(); Author author1 = new Author(); author1.Text = "Author"; authors1.Append(author1); CommentList commentList1 = new CommentList(); Comment comment1 = new Comment() { Reference = "V10", AuthorId = (UInt32Value)0U, ShapeId = (UInt32Value)0U }; CommentText commentText1 = new CommentText(); Run run14 = new Run(); RunProperties runProperties14 = new RunProperties(); Bold bold1 = new Bold(); FontSize fontSize1 = new FontSize() { Val = 9D }; Color color1 = new Color() { Indexed = (UInt32Value)81U }; RunFont runFont1 = new RunFont() { Val = "Tahoma" }; RunPropertyCharSet runPropertyCharSet1 = new RunPropertyCharSet() { Val = 1 }; runProperties14.Append(bold1); runProperties14.Append(fontSize1); runProperties14.Append(color1); runProperties14.Append(runFont1); runProperties14.Append(runPropertyCharSet1); Text text14 = new Text(); text14.Text = "Author:"; run14.Append(runProperties14); run14.Append(text14); Run run15 = new Run(); RunProperties runProperties15 = new RunProperties(); FontSize fontSize2 = new FontSize() { Val = 9D }; Color color2 = new Color() { Indexed = (UInt32Value)81U }; RunFont runFont2 = new RunFont() { Val = "Tahoma" }; RunPropertyCharSet runPropertyCharSet2 = new RunPropertyCharSet() { Val = 1 }; runProperties15.Append(fontSize2); runProperties15.Append(color2); runProperties15.Append(runFont2); runProperties15.Append(runPropertyCharSet2); Text text15 = new Text() { Space = SpaceProcessingModeValues.Preserve }; text15.Text = "\nThis is a comment"; run15.Append(runProperties15); run15.Append(text15); commentText1.Append(run14); commentText1.Append(run15); comment1.Append(commentText1); commentList1.Append(comment1); comments1.Append(authors1); comments1.Append(commentList1); worksheetCommentsPart1.Comments = comments1; }
/// <summary> /// Adds all the comments defined in the List to the current worksheet. /// </summary> /// <param name="worksheetPart">Worksheet Part of file.</param> /// <param name="commentsToAddList">List of CellComment which contain cell coordinates and the text value to set as comment.</param> public static void InsertComments(WorksheetPart worksheetPart, List <CellComment> commentsToAddList) { if (commentsToAddList.Any()) { string commentsVmlXml = string.Empty; // Create all the comment VML Shape XML foreach (var commentToAdd in commentsToAddList) { commentsVmlXml += GetCommentVMLShapeXML(ConvertColumnNumberToName(commentToAdd.col), commentToAdd.row.ToString()); } // The VMLDrawingPart should contain all the definitions for how to draw every comment shape for the worksheet VmlDrawingPart vmlDrawingPart = worksheetPart.AddNewPart <VmlDrawingPart>(); using (XmlTextWriter writer = new XmlTextWriter(vmlDrawingPart.GetStream(FileMode.Create), Encoding.UTF8)) { writer.WriteRaw("<xml xmlns:v=\"urn:schemas-microsoft-com:vml\"\r\n xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n xmlns:x=\"urn:schemas-microsoft-com:office:excel\">\r\n <o:shapelayout v:ext=\"edit\">\r\n <o:idmap v:ext=\"edit\" data=\"1\"/>\r\n" + "</o:shapelayout><v:shapetype id=\"_x0000_t202\" coordsize=\"21600,21600\" o:spt=\"202\"\r\n path=\"m,l,21600r21600,l21600,xe\">\r\n <v:stroke joinstyle=\"miter\"/>\r\n <v:path gradientshapeok=\"t\" o:connecttype=\"rect\"/>\r\n </v:shapetype>" + commentsVmlXml + "</xml>"); } // Create the comment elements foreach (var commentToAdd in commentsToAddList) { WorksheetCommentsPart worksheetCommentsPart = worksheetPart.WorksheetCommentsPart ?? worksheetPart.AddNewPart <WorksheetCommentsPart>(); // We only want one legacy drawing element per worksheet for comments if (worksheetPart.Worksheet.Descendants <LegacyDrawing>().SingleOrDefault() == null) { string vmlPartId = worksheetPart.GetIdOfPart(vmlDrawingPart); LegacyDrawing legacyDrawing = new LegacyDrawing() { Id = vmlPartId }; worksheetPart.Worksheet.Append(legacyDrawing); } Comments comments; bool appendComments = false; if (worksheetPart.WorksheetCommentsPart.Comments != null) { comments = worksheetPart.WorksheetCommentsPart.Comments; } else { comments = new Comments(); appendComments = true; } // We only want one Author element per Comments element if (worksheetPart.WorksheetCommentsPart.Comments == null) { Authors authors = new Authors(); Author author = new Author { Text = "Author Name" }; authors.Append(author); comments.Append(authors); } CommentList commentList; bool appendCommentList = false; if (worksheetPart.WorksheetCommentsPart.Comments != null && worksheetPart.WorksheetCommentsPart.Comments.Descendants <CommentList>().SingleOrDefault() != null) { commentList = worksheetPart.WorksheetCommentsPart.Comments.Descendants <CommentList>().Single(); } else { commentList = new CommentList(); appendCommentList = true; } Comment comment = new Comment() { Reference = string.Concat(ConvertColumnNumberToName(commentToAdd.col), commentToAdd.row), AuthorId = (UInt32Value)0U }; CommentText commentTextElement = new CommentText(); Run run = new Run(); RunProperties runProperties = new RunProperties(); Bold bold = new Bold(); FontSize fontSize = new FontSize() { Val = 8D }; Color color = new Color() { Indexed = (UInt32Value)81U }; RunFont runFont = new RunFont() { Val = "Tahoma" }; RunPropertyCharSet runPropertyCharSet = new RunPropertyCharSet() { Val = 1 }; runProperties.Append(bold); runProperties.Append(fontSize); runProperties.Append(color); runProperties.Append(runFont); runProperties.Append(runPropertyCharSet); Text text = new Text { Text = commentToAdd.text }; run.Append(runProperties); run.Append(text); commentTextElement.Append(run); comment.Append(commentTextElement); commentList.Append(comment); // Only append the Comment List if this is the first time adding a comment if (appendCommentList) { comments.Append(commentList); } // Only append the Comments if this is the first time adding Comments if (appendComments) { worksheetCommentsPart.Comments = comments; } } } }