public static void Run() { // ExStart:RemoveRegionText // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithComments(); string fileName = "TestFile.doc"; // Open the document. Document doc = new Document(dataDir + fileName); CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); Node currentNode = commentStart; Boolean isRemoving = true; while (currentNode != null && isRemoving) { if (currentNode.NodeType == NodeType.CommentRangeEnd) { isRemoving = false; } Node nextNode = currentNode.NextPreOrder(doc); currentNode.Remove(); currentNode = nextNode; } dataDir = dataDir + "RemoveRegionText_out.doc"; // Save the document. doc.Save(dataDir); // ExEnd:RemoveRegionText Console.WriteLine("\nComments added successfully.\nFile saved at " + dataDir); }
static void Main(string[] args) { // Create an empty document and DocumentBuilder object. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a Comment. Comment comment = new Comment(doc); // Insert some text into the comment. Paragraph commentParagraph = new Paragraph(doc); commentParagraph.AppendChild(new Run(doc, "This is comment!!!")); comment.AppendChild(commentParagraph); // Create CommentRangeStart and CommentRangeEnd. int commentId = 0; CommentRangeStart start = new CommentRangeStart(doc, commentId); CommentRangeEnd end = new CommentRangeEnd(doc, commentId); // Insert some text into the document. builder.Write("This is text before comment "); // Insert comment and comment range start. builder.InsertNode(comment); builder.InsertNode(start); // Insert some more text. builder.Write("This is commented text "); // Insert end of comment range. builder.InsertNode(end); // And finaly insert some more text. builder.Write("This is text aftr comment"); // Save output document. doc.Save("Insert a Comment in Word Processing document.docx"); }
public void InsertACommentFeature() { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Comment comment = new Comment(doc); // Insert some text into the comment. Paragraph commentParagraph = new Paragraph(doc); commentParagraph.AppendChild(new Run(doc, "This is comment!!!")); comment.AppendChild(commentParagraph); // Create a "CommentRangeStart" and "CommentRangeEnd". int commentId = 0; CommentRangeStart start = new CommentRangeStart(doc, commentId); CommentRangeEnd end = new CommentRangeEnd(doc, commentId); builder.Write("This text is before the comment. "); // Insert comment and comment range start. builder.InsertNode(comment); builder.InsertNode(start); // Insert some more text. builder.Write("This text is commented. "); // Insert end of comment range. builder.InsertNode(end); builder.Write("This text is after the comment."); doc.Save(ArtifactsDir + "Insert a comment - Aspose.Words.docx"); }
/// <summary> /// Iterates over every top-level comment and prints its comment range, contents, and replies. /// </summary> private static void PrintAllCommentInfo(NodeCollection comments) { CommentInfoPrinter commentVisitor = new CommentInfoPrinter(); // Iterate over all top level comments. Unlike reply-type comments, top-level comments have no ancestor. foreach (Comment comment in comments.Where(c => ((Comment)c).Ancestor == null)) { // First, visit the start of the comment range. CommentRangeStart commentRangeStart = (CommentRangeStart)comment.PreviousSibling.PreviousSibling.PreviousSibling; commentRangeStart.Accept(commentVisitor); // Then, visit the comment, and any replies that it may have. comment.Accept(commentVisitor); foreach (Comment reply in comment.Replies) { reply.Accept(commentVisitor); } // Finally, visit the end of the comment range, and then print the visitor's text contents. CommentRangeEnd commentRangeEnd = (CommentRangeEnd)comment.PreviousSibling; commentRangeEnd.Accept(commentVisitor); Console.WriteLine(commentVisitor.GetText()); } }
public static void ExtractContentBetweenCommentRange() { //ExStart //ExId:ExtractBetweenNodes_BetweenComment //ExSummary:Shows how to extract content referenced by a comment using the ExtractContent method. // Load in the document Document doc = new Document(mDataDir + "TestFile.doc"); // This is a quick way of getting both comment nodes. // Your code should have a proper method of retrieving each corresponding start and end node. CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); // Firstly extract the content between these nodes including the comment as well. ArrayList extractedNodesInclusive = ExtractContent(commentStart, commentEnd, true); Document dstDoc = GenerateDocument(doc, extractedNodesInclusive); dstDoc.Save(mDataDir + "TestFile.CommentInclusive Out.doc"); // Secondly extract the content between these nodes without the comment. ArrayList extractedNodesExclusive = ExtractContent(commentStart, commentEnd, false); dstDoc = GenerateDocument(doc, extractedNodesExclusive); dstDoc.Save(mDataDir + "TestFile.CommentExclusive Out.doc"); //ExEnd }
public static void Run() { //ExStart:ExtractContentBetweenCommentRange // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); Document doc = new Document(dataDir + "TestFile.doc"); // This is a quick way of getting both comment nodes. // Your code should have a proper method of retrieving each corresponding start and end node. CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); // Firstly extract the content between these nodes including the comment as well. ArrayList extractedNodesInclusive = Common.ExtractContent(commentStart, commentEnd, true); Document dstDoc = Common.GenerateDocument(doc, extractedNodesInclusive); dstDoc.Save(dataDir + "TestFile.CommentInclusive_out_.doc"); // Secondly extract the content between these nodes without the comment. ArrayList extractedNodesExclusive = Common.ExtractContent(commentStart, commentEnd, false); dstDoc = Common.GenerateDocument(doc, extractedNodesExclusive); dstDoc.Save(dataDir + "TestFile.CommentExclusive_out_.doc"); //ExEnd:ExtractContentBetweenCommentRange Console.WriteLine("\nExtracted content between the comment range successfully.\nFile saved at " + dataDir + "TestFile.CommentExclusive_out_.doc"); }
/// <summary> /// Use an iterator and a visitor to print info of every comment from within a document. /// </summary> private static void PrintAllCommentInfo(List <Comment> comments) { // Create an object that inherits from the DocumentVisitor class CommentInfoPrinter commentVisitor = new CommentInfoPrinter(); // Get the enumerator from the document's comment collection and iterate over the comments using (IEnumerator <Comment> enumerator = comments.GetEnumerator()) { while (enumerator.MoveNext()) { Comment currentComment = enumerator.Current; // Accept our DocumentVisitor it to print information about our comments if (currentComment != null) { // Get CommentRangeStart from our current comment and construct its information CommentRangeStart commentRangeStart = (CommentRangeStart)currentComment.PreviousSibling.PreviousSibling.PreviousSibling; commentRangeStart.Accept(commentVisitor); // Construct current comment information currentComment.Accept(commentVisitor); // Get CommentRangeEnd from our current comment and construct its information CommentRangeEnd commentRangeEnd = (CommentRangeEnd)currentComment.PreviousSibling; commentRangeEnd.Accept(commentVisitor); } } // Output of all information received Console.WriteLine(commentVisitor.GetText()); } }
/// <summary> /// Called when a CommentRangeStart node is encountered in the document. /// </summary> public override VisitorAction VisitCommentRangeStart(CommentRangeStart commentRangeStart) { IndentAndAppendLine("[Comment range start] ID: " + commentRangeStart.Id); mDocTraversalDepth++; mVisitorIsInsideComment = true; return(VisitorAction.Continue); }
/// <summary> /// Inserts a comment into a document. /// </summary> /// <param name="nodeRange">The range of nodes to insert the comment around.</param> /// <param name="comment">The comment to insert.</param> private static void InsertComment(NodeRange nodeRange, Comment comment) { // Create CommentRangeStart and CommentRangeEnd. var commentStart = new CommentRangeStart(nodeRange.Document, comment.Id); var commentEnd = new CommentRangeEnd(nodeRange.Document, comment.Id); // Insert the comment and range nodes at the appropriate locations. nodeRange.Start.ParentNode.InsertBefore(comment, nodeRange.Start); nodeRange.Start.ParentNode.InsertBefore(commentStart, nodeRange.Start); nodeRange.End.ParentNode.InsertAfter(commentEnd, nodeRange.End); }
public void StartComment(string commentId) { Run run = new Run(); AnnotationReferenceMark annotationReferenceMark = new AnnotationReferenceMark(); run.Append(annotationReferenceMark); _runList.Add(run); CommentRangeStart commentRangeStart = new CommentRangeStart() { Id = commentId }; _runList.Add(commentRangeStart); }
public static void Run() { // ExStart:AnchorComment // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithComments(); Document doc = new Document(); Paragraph para1 = new Paragraph(doc); Run run1 = new Run(doc, "Some "); Run run2 = new Run(doc, "text "); para1.AppendChild(run1); para1.AppendChild(run2); doc.FirstSection.Body.AppendChild(para1); Paragraph para2 = new Paragraph(doc); Run run3 = new Run(doc, "is "); Run run4 = new Run(doc, "added "); para2.AppendChild(run3); para2.AppendChild(run4); doc.FirstSection.Body.AppendChild(para2); Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); comment.Paragraphs.Add(new Paragraph(doc)); comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id); CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id); run1.ParentNode.InsertAfter(commentRangeStart, run1); run3.ParentNode.InsertAfter(commentRangeEnd, run3); commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); dataDir = dataDir + "Anchor.Comment_out.doc"; // Save the document. doc.Save(dataDir); // ExEnd:AnchorComment Console.WriteLine("\nComment anchored successfully.\nFile saved at " + dataDir); }
public void AnchorComment() { //ExStart:AnchorComment Document doc = new Document(); Paragraph para1 = new Paragraph(doc); Run run1 = new Run(doc, "Some "); Run run2 = new Run(doc, "text "); para1.AppendChild(run1); para1.AppendChild(run2); doc.FirstSection.Body.AppendChild(para1); Paragraph para2 = new Paragraph(doc); Run run3 = new Run(doc, "is "); Run run4 = new Run(doc, "added "); para2.AppendChild(run3); para2.AppendChild(run4); doc.FirstSection.Body.AppendChild(para2); Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); comment.Paragraphs.Add(new Paragraph(doc)); comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id); CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id); run1.ParentNode.InsertAfter(commentRangeStart, run1); run3.ParentNode.InsertAfter(commentRangeEnd, run3); commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); doc.Save(ArtifactsDir + "WorkingWithComments.AnchorComment.doc"); //ExEnd:AnchorComment }
public void ExtractContentBetweenCommentRange() { //ExStart:ExtractContentBetweenCommentRange Document doc = new Document(MyDir + "Extract content.docx"); // This is a quick way of getting both comment nodes. // Your code should have a proper method of retrieving each corresponding start and end node. CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); // Firstly, extract the content between these nodes including the comment as well. ArrayList extractedNodesInclusive = ExtractContentHelper.ExtractContent(commentStart, commentEnd, true); Document dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodesInclusive); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenCommentRange.IncludingComment.docx"); // Secondly, extract the content between these nodes without the comment. ArrayList extractedNodesExclusive = ExtractContentHelper.ExtractContent(commentStart, commentEnd, false); dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodesExclusive); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenCommentRange.WithoutComment.docx"); //ExEnd:ExtractContentBetweenCommentRange }
/// <summary> /// Edit Comment and CommentEx /// </summary> /// <param name="filePath">Editing target file path</param> /// <param name="log">Logger</param> public void EditElements(string filePath, VerifiableLog log) { using (WordprocessingDocument package = WordprocessingDocument.Open(filePath, true)) { WordprocessingCommentsPart commentPart = package.MainDocumentPart.WordprocessingCommentsPart; WordprocessingCommentsExPart commentExPart = package.MainDocumentPart.WordprocessingCommentsExPart; Comment comment = null; W15.CommentEx commentEx = null; try { //2.1 Change comment text comment = GetComment(commentPart, CommentIDs.CommentID1); Text text = comment.Descendants <Text>().First(); text.Text = CommentStrings.CommentChangeString1; log.Pass("Edited comment text. comment ID=[{0}]. comment text=[{1}].", CommentIDs.CommentID1, CommentStrings.CommentChangeString1); } catch (Exception e) { log.Fail(e.Message); } try { //2.2 Change commnet initials attribute comment = GetComment(commentPart, CommentIDs.CommentID1); comment.Initials = CommentInitials.Initial2; log.Pass("Edited comment attribute of Initials. comment ID=[{0}]. Initials=[{1}].", CommentIDs.CommentID1, CommentInitials.Initial2); //2.2 Change commnet date attribute comment.Date = new DateTimeValue(new DateTime(2015, 12, 24, 12, 34, 56, 77)); log.Pass("Edited comment attribute of Date. comment ID=[{0}]. Date=[{1}/{2}/{3}-{4}:{5}:{6}-{7}].", CommentIDs.CommentID1, 12, 24, 2015, 12, 34, 56, 77); //2.2 Change commnet author attribute comment.Author = CommentAuthors.Author2; log.Pass("Edited comment attribute of Author. comment ID=[{0}]. Author=[{1}].", CommentIDs.CommentID1, CommentAuthors.Author2); } catch (Exception e) { log.Fail(e.Message); } try { //2.3 Change comment parent-child relationship, Case of parent attribute deleteing. commentEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID2); commentEx.ParaIdParent = null; log.Pass("Edited CommentEx parent-child relationship, Set CommentEx parent id is null. comment ID=[{0}]. CommentEx.ParaIdParent=[null].", CommentIDs.CommentID2); //2.3 Change comment parent-child relationship, Case of parent attribute appending. commentEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID3); W15.CommentEx comEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID2); commentEx.ParaIdParent = comEx.ParaId; log.Pass("Edited CommentEx parent-child relationship, Set CommentEx parent id is comment(id=1) have id. comment ID=[{0}]. CommentEx.ParaIdParent=[{1}].", CommentIDs.CommentID3, comEx.ParaId.Value); } catch (Exception e) { log.Fail(e.Message); } try { //2.4 Change CommnetEx done attribute, Case of value "1" setting. GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID1).Done = true; log.Pass("Edited CommentEx attribute of Done. comment ID=[{0}]. Done=[true].", CommentIDs.CommentID1); //2.4 Change CommnetEx done attribute, Case of value "0" setting. GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID3).Done = false; log.Pass("Edited CommentEx attribute of Done. comment ID=[{0}]. Done=[false].", CommentIDs.CommentID3); } catch (Exception e) { log.Fail(e.Message); } try { //2.5 Add comment and CommentEx. CommentRangeStart commentRangeStart1 = new CommentRangeStart(); commentRangeStart1.Id = CommentIDs.CommentID4; CommentRangeEnd CommentRangeEnd1 = new CommentRangeEnd(); CommentRangeEnd1.Id = CommentIDs.CommentID4; Paragraph paragraph1 = package.MainDocumentPart.Document.Descendants <Paragraph>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First(); paragraph1.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertBeforeSelf <CommentRangeStart>(commentRangeStart1); paragraph1.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertAfterSelf <CommentRangeEnd>(CommentRangeEnd1); CommentReference commentReference1 = new CommentReference(); commentReference1.Id = CommentIDs.CommentID4; Run run1 = new Run(); run1.AppendChild <CommentReference>(commentReference1); paragraph1.AppendChild <Run>(run1); Comment comment1 = (Comment)GetComment(commentPart, CommentIDs.CommentID1).Clone(); comment1.Id = CommentIDs.CommentID4; comment1.Descendants <Text>().First().Text = CommentStrings.CommentAppendString1; comment1.Descendants <Paragraph>().First().ParagraphId = AppendCommentExIDs.AppendCommentID1; commentPart.Comments.AppendChild <Comment>(comment1); W15.CommentEx commentEx1 = new W15.CommentEx(); commentEx1.ParaId = AppendCommentExIDs.AppendCommentID1; commentExPart.CommentsEx.AppendChild <W15.CommentEx>(commentEx1); log.Pass("Append new comment. comment ID=[{0}]. comment text=[{1}]. CommentEx.ParaIdParent=[null].", CommentIDs.CommentID4, CommentStrings.CommentAppendString1); //2.5 Add comment and CommentEx. CommentRangeStart commentRangeStart2 = new CommentRangeStart(); commentRangeStart2.Id = CommentIDs.CommentID5; CommentRangeEnd CommentRangeEnd2 = new CommentRangeEnd(); CommentRangeEnd2.Id = CommentIDs.CommentID5; Paragraph paragraph2 = package.MainDocumentPart.Document.Descendants <Paragraph>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First(); paragraph2.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertBeforeSelf <CommentRangeStart>(commentRangeStart2); paragraph2.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertAfterSelf <CommentRangeEnd>(CommentRangeEnd2); CommentReference commentReference2 = new CommentReference(); commentReference2.Id = CommentIDs.CommentID5; Run run2 = new Run(); run2.AppendChild <CommentReference>(commentReference2); paragraph2.AppendChild <Run>(run2); Comment comment2 = (Comment)GetComment(commentPart, CommentIDs.CommentID2).Clone(); comment2.Id = CommentIDs.CommentID5; comment2.Descendants <Text>().First().Text = CommentStrings.CommentAppendString2; comment2.Descendants <Paragraph>().First().ParagraphId = AppendCommentExIDs.AppendCommentID2; commentPart.Comments.AppendChild <Comment>(comment2); W15.CommentEx commentEx2 = new W15.CommentEx(); commentEx2.ParaId = AppendCommentExIDs.AppendCommentID2; commentEx2.ParaIdParent = AppendCommentExIDs.AppendCommentID1; commentExPart.CommentsEx.AppendChild <W15.CommentEx>(commentEx2); log.Pass("Append new comment. comment ID=[{0}]. comment text=[{1}]. CommentEx.ParaIdParent=[{2}].", CommentIDs.CommentID5, CommentStrings.CommentAppendString2, AppendCommentExIDs.AppendCommentID1); } catch (Exception e) { log.Fail(e.Message); } } }
public override VisitorAction VisitCommentRangeStart(CommentRangeStart commentRangeStart) { this.structureBuilder.AppendLine("<CommentRange>"); return(VisitorAction.Continue); }
/// <summary> /// Edit Comment and CommentEx /// </summary> /// <param name="stream">Package stream</param> /// <param name="log">Logger</param> public static void EditElements(Stream stream) { using (WordprocessingDocument package = WordprocessingDocument.Open(stream, true)) { WordprocessingCommentsPart commentPart = package.MainDocumentPart.WordprocessingCommentsPart; WordprocessingCommentsExPart commentExPart = package.MainDocumentPart.WordprocessingCommentsExPart; Comment comment = null; W15.CommentEx commentEx = null; //2.1 Change comment text comment = GetComment(commentPart, CommentIDs.CommentID1); Text text = comment.Descendants <Text>().First(); text.Text = CommentStrings.CommentChangeString1; //2.2 Change comment initials attribute comment = GetComment(commentPart, CommentIDs.CommentID1); comment.Initials = CommentInitials.Initial2; //2.2 Change comment date attribute comment.Date = new DateTimeValue(new DateTime(2015, 12, 24, 12, 34, 56, 77)); //2.2 Change comment author attribute comment.Author = CommentAuthors.Author2; //2.3 Change comment parent-child relationship, Case of parent attribute deletion. commentEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID2); commentEx.ParaIdParent = null; //2.3 Change comment parent-child relationship, Case of parent attribute appending. commentEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID3); W15.CommentEx comEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID2); commentEx.ParaIdParent = comEx.ParaId; //2.4 Change commentEx done attribute, Case of value "1" setting. GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID1).Done = true; //2.4 Change commentEx done attribute, Case of value "0" setting. GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID3).Done = false; //2.5 Add comment and CommentEx. CommentRangeStart commentRangeStart1 = new CommentRangeStart(); commentRangeStart1.Id = CommentIDs.CommentID4; CommentRangeEnd CommentRangeEnd1 = new CommentRangeEnd(); CommentRangeEnd1.Id = CommentIDs.CommentID4; Paragraph paragraph1 = package.MainDocumentPart.Document.Descendants <Paragraph>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First(); paragraph1.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertBeforeSelf <CommentRangeStart>(commentRangeStart1); paragraph1.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertAfterSelf <CommentRangeEnd>(CommentRangeEnd1); CommentReference commentReference1 = new CommentReference(); commentReference1.Id = CommentIDs.CommentID4; Run run1 = new Run(); run1.AppendChild <CommentReference>(commentReference1); paragraph1.AppendChild <Run>(run1); Comment comment1 = (Comment)GetComment(commentPart, CommentIDs.CommentID1).Clone(); comment1.Id = CommentIDs.CommentID4; comment1.Descendants <Text>().First().Text = CommentStrings.CommentAppendString1; comment1.Descendants <Paragraph>().First().ParagraphId = AppendCommentExIDs.AppendCommentID1; commentPart.Comments.AppendChild <Comment>(comment1); W15.CommentEx commentEx1 = new W15.CommentEx(); commentEx1.ParaId = AppendCommentExIDs.AppendCommentID1; commentExPart.CommentsEx.AppendChild <W15.CommentEx>(commentEx1); //2.5 Add comment and CommentEx. CommentRangeStart commentRangeStart2 = new CommentRangeStart(); commentRangeStart2.Id = CommentIDs.CommentID5; CommentRangeEnd CommentRangeEnd2 = new CommentRangeEnd(); CommentRangeEnd2.Id = CommentIDs.CommentID5; Paragraph paragraph2 = package.MainDocumentPart.Document.Descendants <Paragraph>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First(); paragraph2.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertBeforeSelf <CommentRangeStart>(commentRangeStart2); paragraph2.Descendants <Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertAfterSelf <CommentRangeEnd>(CommentRangeEnd2); CommentReference commentReference2 = new CommentReference(); commentReference2.Id = CommentIDs.CommentID5; Run run2 = new Run(); run2.AppendChild <CommentReference>(commentReference2); paragraph2.AppendChild <Run>(run2); Comment comment2 = (Comment)GetComment(commentPart, CommentIDs.CommentID2).Clone(); comment2.Id = CommentIDs.CommentID5; comment2.Descendants <Text>().First().Text = CommentStrings.CommentAppendString2; comment2.Descendants <Paragraph>().First().ParagraphId = AppendCommentExIDs.AppendCommentID2; commentPart.Comments.AppendChild <Comment>(comment2); W15.CommentEx commentEx2 = new W15.CommentEx(); commentEx2.ParaId = AppendCommentExIDs.AppendCommentID2; commentEx2.ParaIdParent = AppendCommentExIDs.AppendCommentID1; commentExPart.CommentsEx.AppendChild <W15.CommentEx>(commentEx2); } }
// Generates content of mainDocumentPart1. private void GenerateMainDocumentPart1Content(MainDocumentPart mainDocumentPart1) { Document document1 = new Document(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 w15 wp14" } }; document1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); document1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); document1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); document1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); document1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml"); document1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); document1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); document1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); Body body1 = new Body(); Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "0008591D", RsidParagraphProperties = "009423EF", RsidRunAdditionDefault = "009423EF", ParagraphId = "0F416DF1", TextId = "77777777" }; BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" }; BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" }; CommentRangeStart commentRangeStart1 = new CommentRangeStart(){ Id = "1" }; CommentRangeStart commentRangeStart2 = new CommentRangeStart(){ Id = "2" }; Run run1 = new Run(); RunProperties runProperties1 = new RunProperties(); RunFonts runFonts1 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia }; runProperties1.Append(runFonts1); Text text1 = new Text(); text1.Text = "Test1"; run1.Append(runProperties1); run1.Append(text1); CommentRangeEnd commentRangeEnd1 = new CommentRangeEnd(){ Id = "1" }; Run run2 = new Run(); RunProperties runProperties2 = new RunProperties(); RunStyle runStyle1 = new RunStyle(){ Val = "a3" }; runProperties2.Append(runStyle1); CommentReference commentReference1 = new CommentReference(){ Id = "1" }; run2.Append(runProperties2); run2.Append(commentReference1); CommentRangeEnd commentRangeEnd2 = new CommentRangeEnd(){ Id = "2" }; Run run3 = new Run(); RunProperties runProperties3 = new RunProperties(); RunStyle runStyle2 = new RunStyle(){ Val = "a3" }; runProperties3.Append(runStyle2); CommentReference commentReference2 = new CommentReference(){ Id = "2" }; run3.Append(runProperties3); run3.Append(commentReference2); paragraph1.Append(bookmarkStart1); paragraph1.Append(bookmarkEnd1); paragraph1.Append(commentRangeStart1); paragraph1.Append(commentRangeStart2); paragraph1.Append(run1); paragraph1.Append(commentRangeEnd1); paragraph1.Append(run2); paragraph1.Append(commentRangeEnd2); paragraph1.Append(run3); Paragraph paragraph2 = new Paragraph(){ RsidParagraphMarkRevision = "009423EF", RsidParagraphAddition = "009423EF", RsidParagraphProperties = "009423EF", RsidRunAdditionDefault = "009423EF", ParagraphId = "5C236322", TextId = "77777777" }; CommentRangeStart commentRangeStart3 = new CommentRangeStart(){ Id = "3" }; Run run4 = new Run(); RunProperties runProperties4 = new RunProperties(); RunFonts runFonts2 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia }; runProperties4.Append(runFonts2); Text text2 = new Text(); text2.Text = "Test2"; run4.Append(runProperties4); run4.Append(text2); CommentRangeEnd commentRangeEnd3 = new CommentRangeEnd(){ Id = "3" }; Run run5 = new Run(); RunProperties runProperties5 = new RunProperties(); RunStyle runStyle3 = new RunStyle(){ Val = "a3" }; runProperties5.Append(runStyle3); CommentReference commentReference3 = new CommentReference(){ Id = "3" }; run5.Append(runProperties5); run5.Append(commentReference3); paragraph2.Append(commentRangeStart3); paragraph2.Append(run4); paragraph2.Append(commentRangeEnd3); paragraph2.Append(run5); SectionProperties sectionProperties1 = new SectionProperties(){ RsidRPr = "009423EF", RsidR = "009423EF" }; PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)11906U, Height = (UInt32Value)16838U }; PageMargin pageMargin1 = new PageMargin(){ Top = 1985, Right = (UInt32Value)1701U, Bottom = 1701, Left = (UInt32Value)1701U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U }; Columns columns1 = new Columns(){ Space = "425" }; DocGrid docGrid1 = new DocGrid(){ Type = DocGridValues.Lines, LinePitch = 360 }; sectionProperties1.Append(pageSize1); sectionProperties1.Append(pageMargin1); sectionProperties1.Append(columns1); sectionProperties1.Append(docGrid1); body1.Append(paragraph1); body1.Append(paragraph2); body1.Append(sectionProperties1); document1.Append(body1); mainDocumentPart1.Document = document1; }
public static Document FromStrings(string[] strings, out Run[] runs) { var builder = new DocumentBuilder(); var runList = new List<Run>(); var comments = new Dictionary<string, Comment>(); foreach (var str in strings) { // Bookmark if (str.StartsWith("@{") && str.EndsWith("}")) { if (str.StartsWith("@{/")) { var bookmarkName = str.Substring(3, str.Length - 4); builder.EndBookmark(bookmarkName); } else { var bookmarkName = str.Substring(2, str.Length - 3); builder.StartBookmark(bookmarkName); } continue; } // Comment if (str.StartsWith("!{") && str.EndsWith("}")) { if (str.StartsWith("!{/")) { var commentName = str.Substring(3, str.Length - 4); var comment = comments[commentName]; var commentEnd = new CommentRangeEnd(builder.Document, comment.Id); builder.InsertNode(commentEnd); } else { var commentName = str.Substring(2, str.Length - 3); var comment = new Comment(builder.Document, "", "", DateTime.Now); comments.Add(commentName, comment); comment.SetText("Comment: " + commentName); builder.InsertNode(comment); var commentStart = new CommentRangeStart(builder.Document, comment.Id); builder.InsertNode(commentStart); } continue; } runList.Add(builder.InsertRun(str)); } runs = runList.ToArray(); return builder.Document; }
public override VisitorAction VisitCommentRangeStart(CommentRangeStart commentRangeStart) { this.structureBuilder.AppendLine("<CommentRange>"); return VisitorAction.Continue; }
/// <summary> /// Edit Comment and CommentEx /// </summary> /// <param name="filePath">Editing target file path</param> /// <param name="log">Logger</param> public void EditElements(string filePath, VerifiableLog log) { using (WordprocessingDocument package = WordprocessingDocument.Open(filePath, true)) { WordprocessingCommentsPart commentPart = package.MainDocumentPart.WordprocessingCommentsPart; WordprocessingCommentsExPart commentExPart = package.MainDocumentPart.WordprocessingCommentsExPart; Comment comment = null; W15.CommentEx commentEx = null; try { //2.1 Change comment text comment = GetComment(commentPart, CommentIDs.CommentID1); Text text = comment.Descendants<Text>().First(); text.Text = CommentStrings.CommentChangeString1; log.Pass("Edited comment text. comment ID=[{0}]. comment text=[{1}].", CommentIDs.CommentID1, CommentStrings.CommentChangeString1); } catch (Exception e) { log.Fail(e.Message); } try { //2.2 Change commnet initials attribute comment = GetComment(commentPart, CommentIDs.CommentID1); comment.Initials = CommentInitials.Initial2; log.Pass("Edited comment attribute of Initials. comment ID=[{0}]. Initials=[{1}].", CommentIDs.CommentID1, CommentInitials.Initial2); //2.2 Change commnet date attribute comment.Date = new DateTimeValue(new DateTime(2015, 12, 24, 12, 34, 56, 77)); log.Pass("Edited comment attribute of Date. comment ID=[{0}]. Date=[{1}/{2}/{3}-{4}:{5}:{6}-{7}].", CommentIDs.CommentID1, 12, 24, 2015, 12, 34, 56, 77); //2.2 Change commnet author attribute comment.Author = CommentAuthors.Author2; log.Pass("Edited comment attribute of Author. comment ID=[{0}]. Author=[{1}].", CommentIDs.CommentID1, CommentAuthors.Author2); } catch (Exception e) { log.Fail(e.Message); } try { //2.3 Change comment parent-child relationship, Case of parent attribute deleteing. commentEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID2); commentEx.ParaIdParent = null; log.Pass("Edited CommentEx parent-child relationship, Set CommentEx parent id is null. comment ID=[{0}]. CommentEx.ParaIdParent=[null].", CommentIDs.CommentID2); //2.3 Change comment parent-child relationship, Case of parent attribute appending. commentEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID3); W15.CommentEx comEx = GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID2); commentEx.ParaIdParent = comEx.ParaId; log.Pass("Edited CommentEx parent-child relationship, Set CommentEx parent id is comment(id=1) have id. comment ID=[{0}]. CommentEx.ParaIdParent=[{1}].", CommentIDs.CommentID3, comEx.ParaId.Value); } catch (Exception e) { log.Fail(e.Message); } try { //2.4 Change CommnetEx done attribute, Case of value "1" setting. GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID1).Done = true; log.Pass("Edited CommentEx attribute of Done. comment ID=[{0}]. Done=[true].", CommentIDs.CommentID1); //2.4 Change CommnetEx done attribute, Case of value "0" setting. GetCommentEx(commentPart, commentExPart, CommentIDs.CommentID3).Done = false; log.Pass("Edited CommentEx attribute of Done. comment ID=[{0}]. Done=[false].", CommentIDs.CommentID3); } catch (Exception e) { log.Fail(e.Message); } try { //2.5 Add comment and CommentEx. CommentRangeStart commentRangeStart1 = new CommentRangeStart(); commentRangeStart1.Id = CommentIDs.CommentID4; CommentRangeEnd CommentRangeEnd1 = new CommentRangeEnd(); CommentRangeEnd1.Id = CommentIDs.CommentID4; Paragraph paragraph1 = package.MainDocumentPart.Document.Descendants<Paragraph>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First(); paragraph1.Descendants<Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertBeforeSelf<CommentRangeStart>(commentRangeStart1); paragraph1.Descendants<Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertAfterSelf<CommentRangeEnd>(CommentRangeEnd1); CommentReference commentReference1 = new CommentReference(); commentReference1.Id = CommentIDs.CommentID4; Run run1 = new Run(); run1.AppendChild<CommentReference>(commentReference1); paragraph1.AppendChild<Run>(run1); Comment comment1 = (Comment)GetComment(commentPart, CommentIDs.CommentID1).Clone(); comment1.Id = CommentIDs.CommentID4; comment1.Descendants<Text>().First().Text = CommentStrings.CommentAppendString1; comment1.Descendants<Paragraph>().First().ParagraphId = AppendCommentExIDs.AppendCommentID1; commentPart.Comments.AppendChild<Comment>(comment1); W15.CommentEx commentEx1 = new W15.CommentEx(); commentEx1.ParaId = AppendCommentExIDs.AppendCommentID1; commentExPart.CommentsEx.AppendChild<W15.CommentEx>(commentEx1); log.Pass("Append new comment. comment ID=[{0}]. comment text=[{1}]. CommentEx.ParaIdParent=[null].", CommentIDs.CommentID4, CommentStrings.CommentAppendString1); //2.5 Add comment and CommentEx. CommentRangeStart commentRangeStart2 = new CommentRangeStart(); commentRangeStart2.Id = CommentIDs.CommentID5; CommentRangeEnd CommentRangeEnd2 = new CommentRangeEnd(); CommentRangeEnd2.Id = CommentIDs.CommentID5; Paragraph paragraph2 = package.MainDocumentPart.Document.Descendants<Paragraph>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First(); paragraph2.Descendants<Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertBeforeSelf<CommentRangeStart>(commentRangeStart2); paragraph2.Descendants<Run>().Where(e => e.InnerText == CommentBodyStrings.Comment2).First().InsertAfterSelf<CommentRangeEnd>(CommentRangeEnd2); CommentReference commentReference2 = new CommentReference(); commentReference2.Id = CommentIDs.CommentID5; Run run2 = new Run(); run2.AppendChild<CommentReference>(commentReference2); paragraph2.AppendChild<Run>(run2); Comment comment2 = (Comment)GetComment(commentPart, CommentIDs.CommentID2).Clone(); comment2.Id = CommentIDs.CommentID5; comment2.Descendants<Text>().First().Text = CommentStrings.CommentAppendString2; comment2.Descendants<Paragraph>().First().ParagraphId = AppendCommentExIDs.AppendCommentID2; commentPart.Comments.AppendChild<Comment>(comment2); W15.CommentEx commentEx2 = new W15.CommentEx(); commentEx2.ParaId = AppendCommentExIDs.AppendCommentID2; commentEx2.ParaIdParent = AppendCommentExIDs.AppendCommentID1; commentExPart.CommentsEx.AppendChild<W15.CommentEx>(commentEx2); log.Pass("Append new comment. comment ID=[{0}]. comment text=[{1}]. CommentEx.ParaIdParent=[{2}].", CommentIDs.CommentID5, CommentStrings.CommentAppendString2, AppendCommentExIDs.AppendCommentID1); } catch (Exception e) { log.Fail(e.Message); } } }
/// <summary> /// This method is called by the Aspose.Words find and replace engine for each match. /// This method highlights the match string, even if it spans multiple runs. /// </summary> ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { // This is a Run node that contains either the beginning or the complete match. Node currentNode = e.MatchNode; // The first (and may be the only) run can contain text before the match, // In this case it is necessary to split the run. if (e.MatchOffset > 0) { currentNode = SplitRun((Run)currentNode, e.MatchOffset); } // This array is used to store all nodes of the match for further highlighting. ArrayList runs = new ArrayList(); // Find all runs that contain parts of the match string. int remainingLength = e.Match.Value.Length; while ( (remainingLength > 0) && (currentNode != null) && (currentNode.GetText().Length <= remainingLength)) { runs.Add(currentNode); remainingLength -= currentNode.GetText().Length; // Select the next Run node. // Have to loop because there could be other nodes such as BookmarkStart etc. do { currentNode = currentNode.NextSibling; }while ((currentNode != null) && (currentNode.NodeType != NodeType.Run)); } // Split the last run that contains the match if there is any text left. if ((currentNode != null) && (remainingLength > 0)) { SplitRun((Run)currentNode, remainingLength); runs.Add(currentNode); } // Now highlight all runs in the sequence. //foreach (Run run in runs) // run.Font.HighlightColor = System.Drawing.Color.Red; Comment comment = new Comment(_doc, "AI", "AI校核", DateTime.Today); comment.Paragraphs.Add(new Paragraph(_doc)); comment.FirstParagraph.Runs.Add(new Run(_doc, "单位错误")); CommentRangeStart commentRangeStart = new CommentRangeStart(_doc, comment.Id); CommentRangeEnd commentRangeEnd = new CommentRangeEnd(_doc, comment.Id); //run1.ParentNode.InsertAfter(commentRangeStart, run1); //run3.ParentNode.InsertAfter(commentRangeEnd, run3); //commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); foreach (Run run in runs) { run.Font.HighlightColor = System.Drawing.Color.Red; run.ParentNode.InsertAfter(commentRangeStart, run); run.ParentNode.InsertAfter(commentRangeEnd, run); commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); } //Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); //comment.Paragraphs.Add(new Paragraph(doc)); //comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); //CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id); //CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id); //run1.ParentNode.InsertAfter(commentRangeStart, run1); //run3.ParentNode.InsertAfter(commentRangeEnd, run3); //commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); // Signal to the replace engine to do nothing because we have already done all what we wanted. return(ReplaceAction.Skip); }