如何检测到曲目更改请求已删除了文本? [英] How do I detect that text have been deleted by a track change request?

查看:77
本文介绍了如何检测到曲目更改请求已删除了文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用apache-poi读取单词文件,并且可以正常工作.

我使用XWPFRun实例列表阅读文档文本,并且工作正常.

但是,如果为文档启用了轨道更改,那么如果不接受删除操作,我还将获得XWPFRun实例,用于删除已删除的文本.而且我不想包含此文本.

那么,有没有一种方法可以检测XWPDRun节点的轨道更改状态,或者甚至更好的方法来解析文档,就像接受所有轨道更改一样?

解决方案

XWPFRun 尚不支持此功能.但是我们可以确定是否有标记为已删除的文本行.

常规文本运行的 XML 如下:

 < w:r>< w:t> Lorem</w:t></w:r> 

已删除的文本运行如下:

 < w:del w:id ="0" w:author ="axel" w:date ="2020-04-23T18:57:00Z">< w:r w:rsidDel ="00C63AEB">< w:delText> ipsum</w:delText></w:r></w:del> 

因此,已删除的运行位于 del 元素内.但这很难获得.

但是,正常文本运行的文本在 t 元素中,而在删除文本运行的 delText 元素中.因此, XWPFRun.getText(0)将为已删除的文本运行返回 null ,因为这仅遍历运行的 t 元素. XWPFRun.text() XWPFRun.toString()也会返回已删除运行的文本,因为这些方法会遍历运行中包含文本的所有元素.

此外,已删除的文本运行的 CTR 对象中具有 rsidDel 属性,而从未删除的运行中获取该属性将得到 null .

>

此外,文本运行的 CTR 中的 getDelTextList 将返回未删除运行的空列表,但将返回已删除运行的填充列表.

示例,用于检测来自 WordExample.docx 的已删除运行.

  import java.io.FileInputStream;导入org.apache.poi.xwpf.usermodel.*;公共类WordReadDeletedRuns {公共静态void main(String [] args)引发异常{字符串inFilePath ="./WordExample.docx";XWPFDocument document =新的XWPFDocument(new FileInputStream(inFilePath));对于(IBodyElement bodyElement:document.getBodyElements()){如果(XWPFParagraph的bodyElement实例){XWPFParagraph段落=(XWPFParagraph)bodyElement;对于(IRunElement runElement:paragraph.getIRuns()){如果(XWPFRun的runElement instance){XWPFRun run =(XWPFRun)runElement;System.out.println(找到文字运行:" + run.text());System.out.println(run.getText(0));//对于已删除的运行,为nullSystem.out.println(run.getCTR().getRsidDel());//对于未删除的运行,为null;对于已删除的运行,为byte []System.out.println(run.getCTR().getDelTextList().size());//空列表(未删除运行),填充列表(已删除运行)}}}}document.close();}} 

I am using apache-poi to read word files, and it is working.

I read the document text using a list of XWPFRun instances, and that is working fine.

But if track change is enabled for the document, I also get XWPFRun instances for text which have been deleted, if the delete have not been accepted. And I would like not to include this text.

So is there a way to detect track change status for a XWPDRun node, or even better a way to parse the document as if all track changes were accepted?

解决方案

This is not yet supported by XWPFRun. But we could determine whether there are text runs marked as deleted.

Normal text run's XML looks like:

<w:r>
 <w:t>Lorem</w:t>
</w:r>

Deleted text runs look like:

<w:del w:id="0" w:author="axel" w:date="2020-04-23T18:57:00Z">
 <w:r w:rsidDel="00C63AEB">
  <w:delText>ipsum</w:delText>
 </w:r>
</w:del>

So deleted runs are within a del element. But this is tricky to get.

But while normal text run's text is in a t element, it is in a delText element for deleted text runs. So XWPFRun.getText(0) will return null for deleted text runs, because this only traverses the t elements of the run. XWPFRun.text() or XWPFRun.toString() will return the text of the deleted runs too, because those methods traverses all elements which contain text in the run.

Furthermore, deleted text runs have rsidDel attribute in it's CTR object while getting that attribute from not deleted runs will get null.

And furthermore, getDelTextList from CTR of a text run will return a empty list for not deleted runs, but will return a filled list for deleted runs.

Example to detect deleted runs from a WordExample.docx.

import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.*;

public class WordReadDeletedRuns {

 public static void main(String[] args) throws Exception {

  String inFilePath = "./WordExample.docx";

  XWPFDocument document = new XWPFDocument(new FileInputStream(inFilePath));
  for (IBodyElement bodyElement : document.getBodyElements()) {
   if (bodyElement instanceof XWPFParagraph) {
    XWPFParagraph paragraph = (XWPFParagraph)bodyElement;
    for (IRunElement runElement : paragraph.getIRuns()) {
     if (runElement instanceof XWPFRun) {
      XWPFRun run = (XWPFRun)runElement;
      System.out.println("Text run found: " + run.text());
      System.out.println(run.getText(0)); // null for deleted runs
      System.out.println(run.getCTR().getRsidDel()); // null for not deleted runs, byte[] for deleted runs
      System.out.println(run.getCTR().getDelTextList().size()); // empty list for not deleted runs, filled list for deleted runs
     }
    }
   }
  }
  document.close();
 }
}

这篇关于如何检测到曲目更改请求已删除了文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆