//Добавление комментария public void AddCommentOnParagraph(DocumentFormat.OpenXml.Wordprocessing.Paragraph comPar, string comment) { DocumentFormat.OpenXml.Wordprocessing.Comments comments = null; string id = "0"; // Verify that the document contains a // WordProcessingCommentsPart part; if not, add a new one. if (document.MainDocumentPart.GetPartsCountOfType<WordprocessingCommentsPart>() > 0) { comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments; if (comments.HasChildren == true) { // Obtain an unused ID. id = comments.Descendants<DocumentFormat.OpenXml.Wordprocessing.Comment>().Select(e => e.Id.Value).Max() + 1; } } else { // No WordprocessingCommentsPart part exists, so add one to the package. WordprocessingCommentsPart commentPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>(); commentPart.Comments = new DocumentFormat.OpenXml.Wordprocessing.Comments(); comments = commentPart.Comments; } // Compose a new Comment and add it to the Comments part. DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new Text(comment))); DocumentFormat.OpenXml.Wordprocessing.Comment cmt = new DocumentFormat.OpenXml.Wordprocessing.Comment() { Id = id, Author = "FRChecking System", Date = DateTime.Now }; cmt.AppendChild(p); comments.AppendChild(cmt); comments.Save(); // Specify the text range for the Comment. // Insert the new CommentRangeStart before the first run of paragraph. comPar.InsertBefore(new CommentRangeStart() { Id = id }, comPar.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.Run>()); // Insert the new CommentRangeEnd after last run of paragraph. var cmtEnd = comPar.InsertAfter(new CommentRangeEnd() { Id = id }, comPar.Elements<DocumentFormat.OpenXml.Wordprocessing.Run>().Last()); // Compose a run with CommentReference and insert it. comPar.InsertAfter(new DocumentFormat.OpenXml.Wordprocessing.Run(new CommentReference() { Id = id }), cmtEnd); }
public void Bug665268() { var comment = new DocumentFormat.OpenXml.Wordprocessing.Comment(); comment.Date = new DateTimeValue(); comment.Date.InnerText = "2007-04-24T15:42:11.037"; Assert.True(comment.Date.HasValue); Assert.Equal("2007-04-24T15:42:11.037", comment.Date.InnerText); }
public void W005_AddCommentsPart() { const string author = "Some Name"; const string comment = "A comment."; const string initials = "ABC"; const string id = "0"; using (var stream = GetStream(TestFiles.Plain, true)) using (var doc = WordprocessingDocument.Open(stream, true)) { W.Paragraph firstParagraph = doc.MainDocumentPart.Document.Descendants <W.Paragraph>().First(); W.Comments comments = null; WordprocessingCommentsPart commentPart = doc.MainDocumentPart.AddNewPart <WordprocessingCommentsPart>(); commentPart.Comments = new W.Comments(); comments = commentPart.Comments; var p = new W.Paragraph(new W.Run(new W.Text(comment))); var cmt = new W.Comment() { Id = id, Author = author, Initials = initials, Date = DateTime.Now, }; cmt.AppendChild(p); comments.AppendChild(cmt); comments.Save(); firstParagraph.InsertBefore( new W.CommentRangeStart() { Id = id }, firstParagraph.GetFirstChild <W.Run>()); var cmtEnd = firstParagraph.InsertAfter( new W.CommentRangeEnd() { Id = id }, firstParagraph.Elements <W.Run>().Last()); firstParagraph.InsertAfter(new W.Run(new W.CommentReference() { Id = id }), cmtEnd); var v = new OpenXmlValidator(FileFormatVersions.Office2013); var errs = v.Validate(doc); Assert.Empty(errs); } }
public void Bug665268() { this.MyTestInitialize(TestContext.GetCurrentMethod()); var comment = new DocumentFormat.OpenXml.Wordprocessing.Comment(); comment.Date = new DateTimeValue(); comment.Date.InnerText = "2007-04-24T15:42:11.037"; Assert.True(comment.Date.HasValue); Assert.Equal("2007-04-24T15:42:11.037", comment.Date.InnerText); }
public void W005_AddCommentsPart() { var fileInfo = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, Guid.NewGuid().ToString() + ".docx")); var orig = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, "Plain.docx")); File.Copy(orig.FullName, fileInfo.FullName); string author = "Eric White"; string comment = "A comment."; string initials = "ew"; using (WordprocessingDocument doc = WordprocessingDocument.Open(fileInfo.FullName, true)) { W.Paragraph firstParagraph = doc.MainDocumentPart.Document.Descendants<W.Paragraph>().First(); W.Comments comments = null; string id = "0"; WordprocessingCommentsPart commentPart = doc.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>(); commentPart.Comments = new W.Comments(); comments = commentPart.Comments; W.Paragraph p = new W.Paragraph(new W.Run(new W.Text(comment))); W.Comment cmt = new W.Comment() { Id = id, Author = author, Initials = initials, Date = DateTime.Now }; cmt.AppendChild(p); comments.AppendChild(cmt); comments.Save(); firstParagraph.InsertBefore(new W.CommentRangeStart() { Id = id }, firstParagraph.GetFirstChild<W.Run>()); var cmtEnd = firstParagraph.InsertAfter(new W.CommentRangeEnd() { Id = id }, firstParagraph.Elements<W.Run>().Last()); firstParagraph.InsertAfter(new W.Run(new W.CommentReference() { Id = id }), cmtEnd); OpenXmlValidator v = new OpenXmlValidator(FileFormatVersions.Office2013); var errs = v.Validate(doc); Assert.Equal(0, errs.Count()); } if (TestUtil.DeleteTempFiles) fileInfo.Delete(); }