Java版Word开发工具Aspose.Words功能解析:在Word(DOCX / DOC)中插入或删除注释

注释用于Word文档DOCX或DOC中,以建议改进和修改。让我们探讨如何使用Java以编程方式插入注释以及删除或删除注释。您可以根据需要添加作者姓名,缩写,注释文本,日期和时间。

在本文中,将学习以下与Word文档中的注释相关的用例:

>>如果想要测试这项新功能,可aspose.words最新下载(技术交流q群:761297826)icon-default.png?t=M85Bhttps://www.evget.com/product/4116/download下载最新版试用。

  • 使用Java在现有Word文档中插入注释
  • 使用Java在新Word文档中插入注释
  • 使用Java从Word文档中删除特定注释
  • 使用Java从Word文档中删除所有注释

①使用Java在现有Word文档中插入注释

可以使用Aspose.Words for Java API在现有的Microsoft Word文件DOCX或DOC中插入或添加注释。这在审查文档时可能会有所帮助,例如主管可以对可行性报告提出一些变更或改进建议。此外,具有word文档编辑权限的任何人都可以使用注释。您需要按照以下步骤在Word文件(DOCX / DOC)中插入注释:

  • 使用Document类加载现有的DOCX文件
  • 建立注解
  • 保存DOCX文件

以下代码段显示了如何使用Java在Word文档中插入注释:

// Load source word document
Document doc = new Document(dataDir + "Comments.docx");

// Initialize DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);

// Create new comment
Comment comment = new Comment(doc, "Aspose", "Initials", new java.util.Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(new com.aspose.words.Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new com.aspose.words.Run(doc, "Sample Comment"));

// Save output file
doc.save(dataDir + "Comments_Output.docx");

下面的屏幕快照显示了在现有Word文档中添加的示例注释:

②使用Java在新的Word文档中插入注释

创建新的word文档时,注释也很有用。例如,某些文本可能需要详细说明,可以在注释的帮助下进行解释。同样,在成百上千的用例中,注释可以在创建新的DOCX文件时提供帮助。您可以按照以下步骤轻松添加或插入评论:

  • 初始化DocumentBuilder对象
  • 添加示例文字
  • 创建自定义注解
  • 保存DOCX文件

下面的代码段显示了如何使用Java从头创建新的Word文档时插入注释:

// Initialize new word document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Add some text
builder.write("Some text is added.");

// Create new comment
Comment comment = new Comment(doc, "Aspose", "Initials", new java.util.Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(new com.aspose.words.Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new com.aspose.words.Run(doc, "Sample Comment"));

// Save output DOCX file
doc.save(dataDir + "Comments_Output.docx");

下面的屏幕截图显示了在新Word文档上添加注释的输出:

③使用Java从Word文档中删除特定注释

将建议的改进或修改合并到Word文档中时,通常会删除注释。当您需要删除特定评论时,可以按照以下步骤操作:

  • 加载源字文件
  • 指定作者姓名
  • 删除指定作者的注解

下面的代码段显示了如何使用Java从Word文件中删除特定注释:

// Open the document.
Document doc = new Document(dataDir + "Comments.docx");
String authorName = "Aspose";
// Collect all comments in the document
NodeCollection comments = doc.getChildNodes(NodeType.COMMENT, true);
// Look through all comments and remove those written by the Aspose author.
for (int i = comments.getCount() - 1; i >= 0; i--) {
    Comment comment = (Comment) comments.get(i);
    if (comment.getAuthor().equals(authorName))
        comment.remove();
}
// Save output DOCX file
doc.save(dataDir + "output.docx");

④使用Java从Word文档中删除所有注释

Word文档的所有注释都可以立即删除。您可以按照以下步骤删除所有评论:

  • 打开Word docx文件
  • 收集文件中的所有注解
  • 删除所有注解

以下代码片段详细说明了如何使用Java从Word文档中删除所有注释:

// Open the document.
Document doc = new Document(dataDir + "Comments.docx");
// Collect all comments in the document
NodeCollection comments = doc.getChildNodes(com.aspose.words.NodeType.COMMENT, true);
// Remove all comments.
comments.clear();
doc.save(dataDir + "output.docx");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值