文档半岛外围网上直营>>Aspose中文文档>>将注释插入文字处理文档
将注释插入文字处理文档
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
使用Aspose.Words
在 Aspose.Words 中,您可以使用Comment类来处理注释。
以下代码示例演示如何将注释插入到 Word 文档中:
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");
点击复制
使用 Open XML SDK
需要添加的命名空间:
using System; using System.Linq; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
点击复制
以下代码示例演示如何从文档中删除分页符:
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);
}
}
点击复制

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线