文檔半島外圍網上直營>>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交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線