示例#1
0
        public void AddCommentRange(Paragraph range, string comment)
        {
            Paragraph cmdPara = new Paragraph(new Run(new Text(comment)));
            Comment   cmt     = new Comment()
            {
                Id       = this.commentId.ToString(),
                Author   = "Trifolia",
                Initials = "TRIF",
                Date     = DateTime.Now
            };

            cmt.AppendChild(cmdPara);
            this.comments.AppendChild(cmt);

            range.InsertBefore(new CommentRangeStart()
            {
                Id = this.commentId.ToString()
            }, range.GetFirstChild <Run>());
            var cmtEnd = range.InsertAfter(new CommentRangeEnd()
            {
                Id = this.commentId.ToString()
            }, range.Elements <Run>().Last());

            range.InsertAfter(new Run(new CommentReference()
            {
                Id = this.commentId.ToString()
            }), cmtEnd);

            this.commentId++;
        }
        public void AddComment()
        {
            //ExStart
            //ExFor:Comment
            //ExFor:InlineStory
            //ExFor:InlineStory.Paragraphs
            //ExFor:InlineStory.FirstParagraph
            //ExFor:Comment.#ctor(DocumentBase, String, String, DateTime)
            //ExSummary:Shows how to add a comment to a paragraph.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Write("Hello world!");

            Comment comment = new Comment(doc, "John Doe", "JD", DateTime.Today);

            builder.CurrentParagraph.AppendChild(comment);
            builder.MoveTo(comment.AppendChild(new Paragraph(doc)));
            builder.Write("Comment text.");

            Assert.AreEqual(DateTime.Today, comment.DateTime);

            // In Microsoft Word, we can right-click this comment in the document body to edit it, or reply to it.
            doc.Save(ArtifactsDir + "InlineStory.AddComment.docx");
            //ExEnd

            doc     = new Document(ArtifactsDir + "InlineStory.AddComment.docx");
            comment = (Comment)doc.GetChild(NodeType.Comment, 0, true);

            Assert.AreEqual("Comment text.\r", comment.GetText());
            Assert.AreEqual("John Doe", comment.Author);
            Assert.AreEqual("JD", comment.Initial);
            Assert.AreEqual(DateTime.Today, comment.DateTime);
        }
        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");
        }
示例#4
0
        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");
        }
示例#5
0
        // Insert a comment on the first paragraph.
        public static void AddCommentOnFirstParagraph(string fileName,
            string author, string initials, string comment)
        {
            // Use the file name and path passed in as an 
            // argument to open an existing Wordprocessing document. 
            using (WordprocessingDocument document =
                WordprocessingDocument.Open(fileName, true))
            {
                // Locate the first paragraph in the document.
                Paragraph firstParagraph =
                    document.MainDocumentPart.Document.Descendants<Paragraph>().First();
                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)
                    {
                        // Obtain an unused ID.
                        id = comments.Descendants<Comment>().Select(e => e.Id.Value).Max();
                    }
                }
                else
                {
                    // No WordprocessingCommentsPart part exists, so add one to the package.
                    WordprocessingCommentsPart commentPart =
                        document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
                    commentPart.Comments = new Comments();
                    comments = commentPart.Comments;
                }

                // Compose a new Comment and add it to the Comments part.
                Paragraph p = new Paragraph(new Run(new Text(comment)));
                Comment cmt =
                    new Comment()
                    {
                        Id = id,
                        Author = author,
                        Initials = initials,
                        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.
                firstParagraph.InsertBefore(new CommentRangeStart() { Id = id }, firstParagraph.GetFirstChild<Run>());

                // Insert the new CommentRangeEnd after last run of paragraph.
                var cmtEnd = firstParagraph.InsertAfter(new CommentRangeEnd() { Id = id }, firstParagraph.Elements<Run>().Last());

                // Compose a run with CommentReference and insert it.
                firstParagraph.InsertAfter(new Run(new CommentReference() { Id = id }), cmtEnd);
            }
        }
        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");
        }
示例#7
0
        /// <summary>
        /// Sets a comments text.
        /// </summary>
        /// <param name="comment">The commit to modify.</param>
        /// <param name="text">The new comment text.</param>
        private static void SetCommentText(Comment comment, string text)
        {
            if (comment.ChildNodes.Count > 0)
                comment.ChildNodes.Clear();

            // Insert some text into the comment.
            var commentParagraph = new Paragraph(comment.Document);
            commentParagraph.AppendChild(new Run(comment.Document, text));
            comment.AppendChild(commentParagraph);
        }
示例#8
0
        public override void ToWordDocument(WordprocessingDocument wordDocument)
        {
            Paragraph paragraph = base.CreateParagraph();

            foreach (string str in _runList)
            {
                paragraph.Append(new Run(str));
            }
            _comment.AppendChild(paragraph);

            wordDocument.MainDocumentPart.WordprocessingCommentsPart.Comments.AppendChild(_comment);
        }
示例#9
0
        /// <summary>
        /// Добавление комментария к параграфу в документе
        /// </summary>
        /// <param name="document">WordprocessingDocument файла результатов</param>
        /// <param name="ind">Индекс параграфа в документе</param>
        /// <param name="comment">Комментарий к ошибке (текст комментария)</param>
        private static void AddCommentOnParagraph(WordprocessingDocument document, int ind, string comment)
        {
            Paragraph paragraph = document.MainDocumentPart.Document.Descendants <Paragraph>().ElementAt(ind);
            Comments  comments  = null;
            int       id        = 0;

            if (document.MainDocumentPart.GetPartsOfType <WordprocessingCommentsPart>().Count() > 0)
            {
                comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments;
                if (comments.HasChildren)
                {
                    id = int.Parse(comments.Descendants <Comment>().Select(e => e.Id.Value).Max()) + 1;
                }
            }
            else
            {
                WordprocessingCommentsPart commentPart = document.MainDocumentPart.AddNewPart <WordprocessingCommentsPart>();
                commentPart.Comments = new Comments();
                comments             = commentPart.Comments;
            }

            Paragraph p   = new Paragraph(new Run(new Text(comment)));
            Comment   cmt = new Comment()
            {
                Id       = id.ToString(),
                Author   = new StringValue("Программа проверки"),
                Initials = new StringValue("Программа проверки"),
                Date     = DateTime.Now
            };

            cmt.AppendChild(p);
            comments.AppendChild(cmt);
            comments.Save();

            paragraph.InsertBefore(new CommentRangeStart()
            {
                Id = id.ToString()
            }, paragraph.GetFirstChild <Run>());
            CommentRangeEnd cmtEnd = paragraph.InsertAfter(new CommentRangeEnd()
            {
                Id = id.ToString()
            }, paragraph.Elements().Last());

            paragraph.InsertAfter(new Run(new CommentReference()
            {
                Id = id.ToString()
            }), cmtEnd);
        }
示例#10
0
        //gavdcodeend 13

        //gavdcodebegin 14
        public static void WordOpenXmlAddComment()
        {
            using (WordprocessingDocument myWordDoc =
                       WordprocessingDocument.Open(@"C:\Temporary\WordDoc01.docx", true))
            {
                // Locate the first paragraph in the document.
                Paragraph firstParagraph =
                    myWordDoc.MainDocumentPart.Document.Descendants <Paragraph>().First();
                Comments myComments = null;
                string   id         = "0";

                if (myWordDoc.MainDocumentPart.
                    GetPartsOfType <WordprocessingCommentsPart>().Count() > 0)
                {
                    myComments =
                        myWordDoc.MainDocumentPart.WordprocessingCommentsPart.Comments;
                    if (myComments.HasChildren)
                    {
                        id = myComments.Descendants <Comment>().Select
                                 (e => e.Id.Value).Max();
                    }
                }
                else
                {
                    WordprocessingCommentsPart commentPart =
                        myWordDoc.MainDocumentPart.AddNewPart <WordprocessingCommentsPart>();
                    commentPart.Comments = new Comments();
                    myComments           = commentPart.Comments;
                }

                Paragraph myParagraph = new Paragraph(
                    new Run(new Text("This is a new comment")));
                Comment newComment =
                    new Comment()
                {
                    Id       = id,
                    Author   = "OtherAuthor",
                    Initials = "ioa",
                    Date     = DateTime.Now
                };
                newComment.AppendChild(myParagraph);
                myComments.AppendChild(newComment);
                myComments.Save();

                firstParagraph.InsertBefore(new CommentRangeStart()
                {
                    Id = id
                }, firstParagraph.GetFirstChild <Run>());

                var commentEnd = firstParagraph.InsertAfter(new CommentRangeEnd()
                {
                    Id = id
                }, firstParagraph.Elements <Run>().Last());

                firstParagraph.InsertAfter(new Run(
                                               new CommentReference()
                {
                    Id = id
                }), commentEnd);
            }
        }
示例#11
0
        private void AddComment(/*Paragraph textPart, */ Run textPoint, string comment)
        {
            Paragraph textPart = textPoint.Parent as Paragraph;

            if (textPart == null)
            {
                return;
            }
            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)
                {
                    // Obtain an unused ID.
                    id = comments.Descendants <Comment>().Select(e => e.Id.Value).Max();
                }
            }
            else
            {
                // No WordprocessingCommentsPart part exists, so add one to the package.
                WordprocessingCommentsPart commentPart =
                    _document.MainDocumentPart.AddNewPart <WordprocessingCommentsPart>();
                commentPart.Comments = new Comments();
                comments             = commentPart.Comments;
            }

            // Compose a new Comment and add it to the Comments part.
            Paragraph p   = new Paragraph(new Run(new Text(comment)));
            Comment   cmt =
                new Comment()
            {
                Id       = id,
                Author   = _author,
                Initials = _initials,
                Date     = DateTime.Now
            };

            cmt.AppendChild(p);
            comments.AppendChild(cmt);
            comments.Save();
            //Коммент ставить перед Runnom

            // Specify the text range for the Comment.
            // Insert the new CommentRangeStart before the first run of paragraph.
            textPart.InsertBefore(new CommentRangeStart()
            {
                Id = id
            }, textPoint);
            var cmtEnd = textPart.InsertAfter(new CommentRangeEnd()
            {
                Id = id
            }, textPoint);

            // Insert the new CommentRangeEnd after last run of paragraph.


            // Compose a run with CommentReference and insert it.
            textPart.InsertAfter(new Run(new CommentReference()
            {
                Id = id
            }), cmtEnd);
        }
示例#12
0
        public static void AddCommentNearElement(this WordprocessingDocument document, OpenXmlElement element,
                                                 Author author, string comment, string fontName = "Times New Roman", string fontSize = "28")
        {
            Comments comments = null;
            int      id       = 0;

            //var lastRun = paragraph.Descendants<Run>().ToList().LastOrDefault();
            if (document.MainDocumentPart.GetPartsOfType <WordprocessingCommentsPart>().Any())
            {
                comments =
                    document.MainDocumentPart.WordprocessingCommentsPart.Comments;
                if (comments.HasChildren)
                {
                    // Obtain an unused ID.
                    id = comments.Descendants <Comment>().Select(e => Int32.Parse(e.Id.Value)).Max();
                    id++;
                }
            }
            else
            {
                WordprocessingCommentsPart commentPart =
                    document.MainDocumentPart.AddNewPart <WordprocessingCommentsPart>();
                commentPart.Comments = new Comments();
                comments             = commentPart.Comments;
            }

            // Compose a new Comment and add it to the Comments part.
            Paragraph p = new Paragraph(new Run(new Text($"{comment} ID: {id}"), new RunProperties()
            {
                FontSize = new FontSize()
                {
                    Val = fontSize
                },
                RunFonts = new RunFonts()
                {
                    Ascii = fontName
                }
            }));

            Comment cmt =
                new Comment()
            {
                Id       = id.ToString(),
                Author   = author.Name,
                Initials = author.Initials,
                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.
            element.InsertBeforeSelf(new CommentRangeStart()
            {
                Id = id.ToString()
            });


            var commentEnd = new CommentRangeEnd()
            {
                Id = id.ToString()
            };

            // Insert the new CommentRangeEnd after last run of paragraph.
            var cmtEnd = element.InsertAfterSelf(commentEnd);

            // Compose a run with CommentReference and insert it.
            commentEnd.InsertAfterSelf(new Run(new CommentReference()
            {
                Id = id.ToString()
            }));
        }
示例#13
0
        // Add Comment
        public static void AddCommentOnFirstParagraph(string filename, string author, string initials, string comment)
        {
            try
            {

                using (WordprocessingDocument document = WordprocessingDocument.Open(filename, true))
                {

                    // Locate first paragraph
                    Paragraph firstParagraph = document.MainDocumentPart.Document.Descendants<Paragraph>().First();
                    Comments comentario = null;
                    string id = "0";

                    // Check if document has a commentpart, if not creates one
                    if (document.MainDocumentPart.GetPartsCountOfType<WordprocessingCommentsPart>() > 0)
                    {
                        comentario = document.MainDocumentPart.WordprocessingCommentsPart.Comments;

                        if (comentario.HasChildren)
                        {
                            // obtain unused id
                            id = comentario.Descendants<Comment>().Select(e => e.Id.Value).Max();
                        }
                    }
                    else
                    {
                        // there is no comment part, so add one
                        WordprocessingCommentsPart commentpart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
                        commentpart.Comments = new Comments();
                        comentario = commentpart.Comments;

                    }

                    // compose a new comment and add to the comments part
                    Paragraph p = new Paragraph(new Run(new Text(comment)));
                    Comment cmt =
                        new Comment()
                        {
                            Id = id,
                            Author = author,
                            Initials = initials,
                            Date = DateTime.Now
                        };

                    cmt.AppendChild(p);
                    comentario.AppendChild(cmt);
                    comentario.Save();

                    // Insert the new CommentRangeStart before the first run of paragraph.
                    firstParagraph.InsertBefore(new CommentRangeStart() { Id = id }, firstParagraph.GetFirstChild<Run>());

                    // Insert the new CommentRangeEnd after last run of paragraph.
                    var cmtEnd = firstParagraph.InsertAfter(new CommentRangeEnd() { Id = id }, firstParagraph.Elements<Run>().Last());

                    // Compose a run with CommentReference and insert it.
                    firstParagraph.InsertAfter(new Run(new CommentReference() { Id = id }), cmtEnd);

                    document.Close();
                }


            }
            catch (Exception ex)
            {

                throw ex;
            }


        } 
示例#14
0
        // Insert a comment on the first paragraph.
        public static void AddCommentOnFirstParagraph(string fileName,
                                                      string author, string initials, string comment)
        {
            // Use the file name and path passed in as an
            // argument to open an existing Wordprocessing document.
            using (WordprocessingDocument document =
                       WordprocessingDocument.Open(fileName, true))
            {
                // Locate the first paragraph in the document.
                Paragraph firstParagraph =
                    document.MainDocumentPart.Document.Descendants <Paragraph>().First();
                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)
                    {
                        // Obtain an unused ID.
                        id = comments.Descendants <Comment>().Select(e => e.Id.Value).Max();
                    }
                }
                else
                {
                    // No WordprocessingCommentsPart part exists, so add one to the package.
                    WordprocessingCommentsPart commentPart =
                        document.MainDocumentPart.AddNewPart <WordprocessingCommentsPart>();
                    commentPart.Comments = new Comments();
                    comments             = commentPart.Comments;
                }

                // Compose a new Comment and add it to the Comments part.
                Paragraph p   = new Paragraph(new Run(new Text(comment)));
                Comment   cmt =
                    new Comment()
                {
                    Id       = id,
                    Author   = author,
                    Initials = initials,
                    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.
                firstParagraph.InsertBefore(new CommentRangeStart()
                {
                    Id = id
                }, firstParagraph.GetFirstChild <Run>());

                // Insert the new CommentRangeEnd after last run of paragraph.
                var cmtEnd = firstParagraph.InsertAfter(new CommentRangeEnd()
                {
                    Id = id
                }, firstParagraph.Elements <Run>().Last());

                // Compose a run with CommentReference and insert it.
                firstParagraph.InsertAfter(new Run(new CommentReference()
                {
                    Id = id
                }), cmtEnd);
            }
        }
示例#15
0
        public void InsertACommentFeature()
        {
            using (WordprocessingDocument document =
                       WordprocessingDocument.Create(ArtifactsDir + "Insert a comment - OpenXML.docx",
                                                     WordprocessingDocumentType.Document))
            {
                // Locate the first paragraph in the document.
                Paragraph firstParagraph =
                    document.MainDocumentPart.Document.Descendants <Paragraph>().First();
                Comments comments;
                string   id = "0";

                // Verify that the document contains a
                // WordProcessingCommentsPart part; if not, add a new one.
                if (document.MainDocumentPart.GetPartsOfType <WordprocessingCommentsPart>().Any())
                {
                    comments =
                        document.MainDocumentPart.WordprocessingCommentsPart.Comments;
                    if (comments.HasChildren)
                    {
                        // Obtain an unused ID.
                        id = comments.Descendants <Comment>().Select(e => e.Id.Value).Max();
                    }
                }
                else
                {
                    // No "WordprocessingCommentsPart" part exists, so add one to the package.
                    WordprocessingCommentsPart commentPart =
                        document.MainDocumentPart.AddNewPart <WordprocessingCommentsPart>();
                    commentPart.Comments = new Comments();
                    comments             = commentPart.Comments;
                }

                // Compose a new Comment and add it to the Comments part.
                Paragraph p   = new Paragraph(new Run(new Text("This is my comment.")));
                Comment   cmt = new Comment
                {
                    Id       = id,
                    Author   = "author",
                    Initials = "initials",
                    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.
                firstParagraph.InsertBefore(new CommentRangeStart {
                    Id = id
                }, firstParagraph.GetFirstChild <Run>());

                // Insert the new CommentRangeEnd after last run of paragraph.
                var cmtEnd = firstParagraph.InsertAfter(new CommentRangeEnd {
                    Id = id
                },
                                                        firstParagraph.Elements <Run>().Last());

                // Compose a run with CommentReference and insert it.
                firstParagraph.InsertAfter(new Run(new CommentReference {
                    Id = id
                }), cmtEnd);
            }
        }