ADDIN EN REFLIST

@Test
  public void test23() throws Exception {
    Document document = new Document("C:\\Users\\thh\\Desktop\\参考文献-测试.docx");
    int i = 0;
    for (Paragraph paragraph : document.getFirstSection().getBody().getParagraphs()) {
      if (i++ > 0) {
        String text = paragraph.toString(SaveFormat.TEXT).trim();
        System.out.println(text);
        if (!text.isBlank()) {
          paragraph.getRuns().clear();
          paragraph.getRuns().add(new Run(paragraph.getDocument(), "test text"));
        }
      }
    }
    document.save("src/main/resources/docx/templates/result.docx", SaveFormat.DOCX);
  }

I have a field like this in my document, how can I replace clearing all runs in the paragraph and then re-adding the text. Or how to convert this field into normal text first

参考文献-测试.docx (11.8 KB)

@Crane You can use the following simple code to replace ADDIN field with it’s value (convert the field into normal text):

Document doc = new Document("C:\\Temp\\in.docx");
for (Field f : doc.getRange().getFields())
{
    if (f.getType() == FieldType.FIELD_ADDIN)
        f.unlink();
}
doc.save("C:\\temp\\out.docx");
1 Like