本文整理汇总了C#中Comment.AppendChild方法的典型用法代码示例。如果您正苦于以下问题:C# Comment.AppendChild方法的具体用法?C# Comment.AppendChild怎么用?C# Comment.AppendChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Comment
的用法示例。
在下文中一共展示了Comment.AppendChild方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddCommentOnFirstParagraph
// 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);
}
}
示例2: Main
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");
}
示例3: SetCommentText
/// <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);
}
示例4: AddCommentOnFirstParagraph
// 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;
}
}