简体   繁体   English

Apache poi:在段落中插入文本并查看 word 文档中的跟踪更改

[英]Apache poi: insert text in a paragraph and view track changes in a word document

I´m using Apache poi library with java 11. I´m trying to see the track changes after adding a new text into a paragraph in a word document:我正在使用带有 java 11 的 Apache poi 库。在将新文本添加到 word 文档的段落中后,我试图查看音轨变化:

 private void setSectionRun(XWPFParagraph paragraph){
    // insert xml node
    paragraph.getCTP().addNewIns().setAuthor("Kane");

    XWPFRun newRun = paragraph.createRun();
    newRun.setText(". Hello world");
    paragraph.addRun(newRun);
}

And I get the following output in document.xml.我在 document.xml 中得到以下输出。 In this case the track changes is not working:在这种情况下,轨道更改不起作用:

<w:ins w:author="Kane"/>
<w:r>
    <w:t>. Hello world</w:t>
</w:r>

In any case, if I manually edit the document I can see the track changes and accept or reject the insertion with the following result:无论如何,如果我手动编辑文档,我可以看到轨道更改并接受或拒绝插入,结果如下:

<w:ins w:id="0" w:author="Kane" w:date="2022-04-20T15:33:00Z">
    <w:r w:rsidR="00B00A22">
        <w:t>. Hello world</w:t>
    </w:r>
</w:ins>

The problem is that I can't activate the track changes when I insert a new text in a paragraph with Apache poi.问题是当我使用 Apache poi 在段落中插入新文本时,我无法激活轨道更改。

On the other hand, with newRun.getCTR().getRPr().addNewRPrChange().setAuthor("Kane") the format changes of a XWPFRun are detected and I can see the change control correctly, but not the new text insertions at the paragraph level.另一方面,使用newRun.getCTR().getRPr().addNewRPrChange().setAuthor("Kane")检测到XWPFRun的格式更改,我可以正确看到更改控件,但看不到新文本插入段落级别。

Is there a way to enable the track change on new text inserts?有没有办法在新的文本插入上启用轨道更改?

Thanks in advance.提前致谢。

Apache POI does not support tracking changes up to now.到目前为止,Apache POI 不支持跟踪更改。

As you see in your XML, your code places an empty w:ins element above the w:r element.正如您在 XML 中看到的,您的代码在w:r元素上方放置了一个空的w:ins元素。 But Microsoft Word wraps the w:r element with the w:ins element.但是 Microsoft Word 用w:ins元素包装了w:r元素。 The w:r element is a child element of the w:ins element. w:r元素是w:ins元素的子元素。

One possibility to code that would be having a class XWPFInsRun which extends XWPFRun and a method to create such XWPFInsRun in a XWPFParagraph .编码的一种可能性是具有扩展XWPFRun的类XWPFInsRun和在XWPFParagraph中创建此类XWPFInsRun的方法。 Doing this one has all options of XWPFRun extended by the options of CTRunTrackChange - author and date for example.这样做会使 XWPFRun 的所有选项XWPFRun的选项CTRunTrackChange - 例如作者和日期。

Complete example:完整示例:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class CreateWordInsRun {
    
 static XWPFInsRun createInsRun(XWPFParagraph paragraph, String author, java.util.Calendar date) {
  CTRunTrackChange trackChange = paragraph.getCTP().addNewIns();
  trackChange.setAuthor(author);
  trackChange.setDate(date);
  trackChange.addNewR();
  return new XWPFInsRun(
    trackChange,
    trackChange.getRArray(0),
    paragraph
   );
 }

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

  XWPFDocument doc = new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("Paragraph 1");

  paragraph = doc.createParagraph();
  XWPFInsRun insRun = createInsRun(paragraph, "Kane", new java.util.GregorianCalendar());
  insRun.setText("Paragraph 2 inserted by Kane");
  System.out.println(insRun.getAuthor());
  System.out.println(insRun.getDate());
  
  paragraph = doc.createParagraph();
  run = paragraph.createRun();
  run.setText("Paragraph 3");
    
  FileOutputStream out = new FileOutputStream("./WordDocument.docx");
  doc.write(out);
  out.close();
  doc.close();    
 }
 
 static class XWPFInsRun extends XWPFRun {
  private CTRunTrackChange trackChange;
  public XWPFInsRun(CTRunTrackChange trackChange, CTR run, IRunBody p) {
   super(run, p);
   this.trackChange = trackChange;
  }
  public String getAuthor() {
   return this.trackChange.getAuthor();   
  }
  public void setAuthor(String author) {
   this.trackChange.setAuthor(author);   
  }
  public java.util.Calendar getDate() {
   return this.trackChange.getDate();   
  }
  public void setDate(java.util.Calendar date) {
   this.trackChange.setDate(date);   
  }
  //... further methods
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM