文檔半島外圍網上直營>>Aspose中文文檔>>刪除所有或特定作者的評論
                刪除所有或特定作者的評論
Aspose.Words是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
使用Aspose.Words
以下代碼示例展示了如何刪除文檔中的所有注釋:
	
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET
static void RemoveComments(Document doc)
{
// Collect all comments in the document
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Remove all comments.
comments.Clear();
}
	點擊復制
	
以下代碼示例展示了如何刪除特定作者的評論:
	
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET
static void RemoveComments(Document doc, string authorName)
{
// Collect all comments in the document
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Look through all comments and remove those written by the authorName author.
for (int i = comments.Count - 1; i >= 0; i--)
{
Comment comment = (Comment)comments[i];
if (comment.Author == authorName)
comment.Remove();
}
}
	點擊復制
	
使用 Open XML SDK
需要添加的命名空間:
	
using System; using System.Collections.Generic; using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
點擊復制
	
以下代碼示例展示了如何刪除文檔中的注釋:
public void DeleteCommentsByAllOrASpecificAuthorFeature()
{
RemoveComments("");
}
private void RemoveComments(string author)
{
// Get an existing Wordprocessing document.
using (WordprocessingDocument document =
WordprocessingDocument.Open(MyDir + "Comments.docx", true))
{
WordprocessingCommentsPart commentPart =
document.MainDocumentPart.WordprocessingCommentsPart;
// If no "WordprocessingCommentsPart" exists, there can be no comments.
// Stop execution and return from the method.
if (commentPart == null)
return;
// Create a list of comments by the specified author.
// If the author name is empty, then list all authors.
List<Comment> commentsToDelete =
commentPart.Comments.Elements<Comment>().ToList();
if (!String.IsNullOrEmpty(author))
{
commentsToDelete = commentsToDelete.Where(c => c.Author == author).ToList();
}
IEnumerable<string> commentIds =
commentsToDelete.Select(r => r.Id.Value);
foreach (Comment c in commentsToDelete)
c.Remove();
// Save changes to the comments part.
commentPart.Comments.Save();
Document doc = document.MainDocumentPart.Document;
// Delete the "CommentRangeStart" for each deleted comment in the main document.
List<CommentRangeStart> commentRangeStartToDelete =
doc.Descendants<CommentRangeStart>().Where(c => commentIds.Contains(c.Id.Value)).ToList();
foreach (CommentRangeStart c in commentRangeStartToDelete)
c.Remove();
// Delete the "CommentRangeEnd" for each deleted comment in the main document.
List<CommentRangeEnd> commentRangeEndToDelete =
doc.Descendants<CommentRangeEnd>().Where(c => commentIds.Contains(c.Id.Value)).ToList();
foreach (CommentRangeEnd c in commentRangeEndToDelete)
c.Remove();
// Delete the "CommentReference" for each deleted comment in the main document.
List<CommentReference> commentRangeReferenceToDelete =
doc.Descendants<CommentReference>().Where(c => commentIds.Contains(c.Id.Value)).ToList();
foreach (CommentReference c in commentRangeReferenceToDelete)
c.Remove();
using (Stream stream = File.Open(ArtifactsDir + "Remove comments - OpenXML.docx", FileMode.CreateNew))
{
doc.Save(stream);
}
}
}
	點擊復制

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線