Esempio n. 1
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);
        }
        private static int AddOtherParts(Stream stream, int count)
        {
            const int firstId = 0;

            using WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true);
            wordDocument.AddParagraphIdFeature();

            MainDocumentPart mainDocumentPart = wordDocument.MainDocumentPart !;

            // Add Header.
            var headerParagraph = new Paragraph(new Run(new Text("Header")));

            var headerPart = mainDocumentPart.AddNewPart <HeaderPart>();

            headerPart.Header = new Header(headerParagraph);

            // Add Footer.
            var footerParagraph = new Paragraph(new Run(new Text("Footer")));

            var footerPart = mainDocumentPart.AddNewPart <FooterPart>();

            footerPart.Footer = new Footer(footerParagraph);

            // Add Comments.
            var comments = new Comments();

            for (var id = firstId; id < firstId + count; id++)
            {
                var paragraph = new Paragraph(new Run(new Text($"Comment #{id}")));

                comments.AppendChild(new Comment(paragraph)
                {
                    Id     = id.ToString(),
                    Author = $"Author #{id}",
                });
            }

            var commentsPart = mainDocumentPart.AddNewPart <WordprocessingCommentsPart>();

            commentsPart.Comments = comments;

            // Add Footnotes
            var footnotes = new Footnotes();

            for (var id = firstId; id < firstId + count; id++)
            {
                var paragraph = new Paragraph(new Run(new Text($"Footnote #{id}")));

                footnotes.AppendChild(new Footnote(paragraph)
                {
                    Id = id
                });
            }

            var footnotesPart = mainDocumentPart.AddNewPart <FootnotesPart>();

            footnotesPart.Footnotes = footnotes;

            // Add Endnotes.
            var endnotes = new Endnotes();

            for (var id = firstId; id < firstId + count; id++)
            {
                var paragraph = new Paragraph(new Run(new Text($"Endnote #{id}")));

                endnotes.AppendChild(new Endnote(paragraph)
                {
                    Id = id
                });
            }

            var endnotesPart = mainDocumentPart.AddNewPart <EndnotesPart>();

            endnotesPart.Endnotes = endnotes;

            return((3 * count) + 2);
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
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);
        }
Esempio n. 5
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()
            }));
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        private static void AddMentionComment(MainDocumentPart mdp, string strCommentId, string mention, string commentText, User mentioner, User mentionee)
        {
            mdp.AddNewPart <WordprocessingCommentsPart>();
            mdp.WordprocessingCommentsPart.Comments = new Comments();
            Comments comments = mdp.WordprocessingCommentsPart.Comments;

            comments.AppendChild <Comment>(
                new Comment(
                    new Paragraph(
                        new Run(
                            new RunProperties(
                                new Color()
            {
                Val = "2B579A"
            },
                                new Shading()
            {
                Val = ShadingPatternValues.Clear, Color = "auto", Fill = "E6E6E6"
            }),
                            new FieldChar()
            {
                FieldCharType = FieldCharValues.Begin
            }),
                        new Run(
                            new FieldCode(" HYPERLINK \"mailto:" + mentionee.Email + "\"")
            {
                Space = SpaceProcessingModeValues.Preserve
            }),
                        new BookmarkStart()
            {
                Id = "2", Name = "_@_0FD9AD1E39C946AB9F9E1352162C9910Z"
            },
                        new Run(
                            new RunProperties(
                                new Color()
            {
                Val = "2B579A"
            },
                                new Shading()
            {
                Val = ShadingPatternValues.Clear, Color = "auto", Fill = "E6E6E6"
            }),
                            new FieldChar()
            {
                FieldCharType = FieldCharValues.Separate
            }),
                        new BookmarkEnd()
            {
                Id = "2"
            },
                        new Run(
                            new RunProperties(
                                new RunStyle()
            {
                Val = "Mention"
            },
                                new NoProof()),
                            new Text(mention)),
                        new Run(
                            new RunProperties(
                                new Color()
            {
                Val = "2B579A"
            },
                                new Shading()
            {
                Val = ShadingPatternValues.Clear, Color = "auto", Fill = "E6E6E6"
            }),
                            new FieldChar()
            {
                FieldCharType = FieldCharValues.End
            }),
                        new Run(
                            new Text(commentText)
            {
                Space = SpaceProcessingModeValues.Preserve
            }),
                        new Run(
                            new AnnotationReferenceMark()))

                    // These ids MUST be unique in the document.
            {
                TextId = "FFFFFF04"
            })

                // This id MUST be unique in the comments part.
            {
                Id = strCommentId, Author = mentioner.UserName, Date = DateTime.Now, Initials = mentioner.UserName.Substring(0, 1) + mentioner.UserName.Substring(mentioner.UserName.IndexOf(" ") + 1, 1)
            });
        }