Comment
Содержание
[
Скрывать
]Comment(DocumentBase)
Инициализирует новый экземплярComment
класс.
public Comment(DocumentBase doc)
Параметр | Тип | Описание |
---|---|---|
doc | DocumentBase | Документ владельца. |
Примечания
КогдаComment
создан, он принадлежит указанному документу, но еще не является частью документа иParentNode
являетсянулевой
.
Чтобы добавитьComment
к использованию документаInsertAfter
илиInsertBefore
в абзаце, в который вы хотите вставить комментарий.
После создания комментария не забудьте установить егоAuthor
, Initial
иDateTime
характеристики.
Примеры
Показывает, как распечатать содержимое всех комментариев и их диапазоны комментариев с помощью посетителя документов.
public void CreateCommentsAndPrintAllInfo()
{
Document doc = new Document();
Comment newComment = new Comment(doc)
{
Author = "VDeryushev",
Initial = "VD",
DateTime = DateTime.Now
};
newComment.SetText("Comment regarding text.");
// Добавьте текст в документ, деформируйте его в диапазоне комментариев, а затем добавьте свой комментарий.
Paragraph para = doc.FirstSection.Body.FirstParagraph;
para.AppendChild(new CommentRangeStart(doc, newComment.Id));
para.AppendChild(new Run(doc, "Commented text."));
para.AppendChild(new CommentRangeEnd(doc, newComment.Id));
para.AppendChild(newComment);
// Добавляем два ответа на комментарий.
newComment.AddReply("John Doe", "JD", DateTime.Now, "New reply.");
newComment.AddReply("John Doe", "JD", DateTime.Now, "Another reply.");
PrintAllCommentInfo(doc.GetChildNodes(NodeType.Comment, true));
}
/// <summary>
/// Проходит по каждому комментарию верхнего уровня и печатает его диапазон комментариев, содержимое и ответы.
/// </summary>
private static void PrintAllCommentInfo(NodeCollection comments)
{
CommentInfoPrinter commentVisitor = new CommentInfoPrinter();
// Перебираем все комментарии верхнего уровня. В отличие от комментариев типа ответа, комментарии верхнего уровня не имеют предка.
foreach (Comment comment in comments.Where(c => ((Comment)c).Ancestor == null))
{
// Сначала посещаем начало диапазона комментариев.
CommentRangeStart commentRangeStart = (CommentRangeStart)comment.PreviousSibling.PreviousSibling.PreviousSibling;
commentRangeStart.Accept(commentVisitor);
// Затем просмотрите комментарий и все ответы, которые он может иметь.
comment.Accept(commentVisitor);
foreach (Comment reply in comment.Replies)
reply.Accept(commentVisitor);
// Наконец, переходим к концу диапазона комментариев и затем печатаем текстовое содержимое посетителя.
CommentRangeEnd commentRangeEnd = (CommentRangeEnd)comment.PreviousSibling;
commentRangeEnd.Accept(commentVisitor);
Console.WriteLine(commentVisitor.GetText());
}
}
/// <summary>
/// Печатает информацию и содержимое всех комментариев и диапазонов комментариев, встречающихся в документе.
/// </summary>
public class CommentInfoPrinter : DocumentVisitor
{
public CommentInfoPrinter()
{
mBuilder = new StringBuilder();
mVisitorIsInsideComment = false;
}
/// <summary>
/// Получает открытый текст документа, накопленный посетителем.
/// </summary>
public string GetText()
{
return mBuilder.ToString();
}
/// <summary>
/// Вызывается, когда в документе встречается узел Run.
/// </summary>
public override VisitorAction VisitRun(Run run)
{
if (mVisitorIsInsideComment) IndentAndAppendLine("[Run] \"" + run.Text + "\"");
return VisitorAction.Continue;
}
/// <summary>
/// Вызывается, когда в документе встречается узел CommentRangeStart.
/// </summary>
public override VisitorAction VisitCommentRangeStart(CommentRangeStart commentRangeStart)
{
IndentAndAppendLine("[Comment range start] ID: " + commentRangeStart.Id);
mDocTraversalDepth++;
mVisitorIsInsideComment = true;
return VisitorAction.Continue;
}
/// <summary>
/// Вызывается, когда в документе встречается узел CommentRangeEnd.
/// </summary>
public override VisitorAction VisitCommentRangeEnd(CommentRangeEnd commentRangeEnd)
{
mDocTraversalDepth--;
IndentAndAppendLine("[Comment range end] ID: " + commentRangeEnd.Id + "\n");
mVisitorIsInsideComment = false;
return VisitorAction.Continue;
}
/// <summary>
/// Вызывается, когда в документе встречается узел комментария.
/// </summary>
public override VisitorAction VisitCommentStart(Comment comment)
{
IndentAndAppendLine(
$"[Comment start] For comment range ID {comment.Id}, By {comment.Author} on {comment.DateTime}");
mDocTraversalDepth++;
mVisitorIsInsideComment = true;
return VisitorAction.Continue;
}
/// <summary>
/// Вызывается, когда в документе заканчивается посещение узла комментариев.
/// </summary>
public override VisitorAction VisitCommentEnd(Comment comment)
{
mDocTraversalDepth--;
IndentAndAppendLine("[Comment end]");
mVisitorIsInsideComment = false;
return VisitorAction.Continue;
}
/// <summary>
/// Добавляем строку к StringBuilder и отступаем от нее в зависимости от того, насколько глубоко посетитель находится в дереве документа.
/// </summary>
/// <param name="text"></param>
private void IndentAndAppendLine(string text)
{
for (int i = 0; i < mDocTraversalDepth; i++)
{
mBuilder.Append("| ");
}
mBuilder.AppendLine(text);
}
private bool mVisitorIsInsideComment;
private int mDocTraversalDepth;
private readonly StringBuilder mBuilder;
}
Смотрите также
- class DocumentBase
- class Comment
- пространство имен Aspose.Words
- сборка Aspose.Words
Comment(DocumentBase, string, string, DateTime)
Инициализирует новый экземплярComment
класс.
public Comment(DocumentBase doc, string author, string initial, DateTime dateTime)
Параметр | Тип | Описание |
---|---|---|
doc | DocumentBase | Документ владельца. |
author | String | Имя автора комментария. Не может бытьнулевой . |
initial | String | Инициалы автора комментария. Не может бытьнулевой . |
dateTime | DateTime | Дата и время комментария. |
Примеры
Показывает, как добавить комментарий к абзацу.
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);
// В Microsoft Word мы можем щелкнуть правой кнопкой мыши этот комментарий в теле документа, чтобы отредактировать его или ответить на него.
doc.Save(ArtifactsDir + "InlineStory.AddComment.docx");
Смотрите также
- class DocumentBase
- class Comment
- пространство имен Aspose.Words
- сборка Aspose.Words