2

Apache poi version: 4.0

I have 2 docs (docx) with comments and formatting.

How can i copy all text (with comments) from first to second?

As i read here we couldn't write comments.xml section by XWPFDocument - it's open comments section only for read.

When i copied one document to second like this:

private void copyDoc(XWPFDocument to_document, XWPFDocument from_document) {
    for (IBodyElement bodyElement : to_document.getBodyElements()) {
        BodyElementType elementType = bodyElement.getElementType();

        if (elementType.equals(BodyElementType.PARAGRAPH)) {
             ...
        } else if (elementType.equals(BodyElementType.TABLE)) {
             ...
        }
    }
}

i take paragraphs and runs with comment sections, but i don't have their comments in comments.xml of destination file.

So my file is corrupted and opening with errors ("where is no comments for some notice" style), where i have red sections of broken comments.

Is there a way to correct copying all comments to one doc file?

--- UPDATED ---

I used this example to copy comments, but i have some problems.

At first, i tried to take all paragraphs from source files, and all comments by @axel-richter method. But in result doc, where i have comments from 2 files, some of them have same id.

And if MS-Word open file without problems - all comments are correct, LibreOffice write each same-id comments as one.

For example:

  • comment from 1.docx with id 1 and text "some comment 1"
  • comment from 2.docx with id 1 and text "some comment 2"

Result:

  • "Some text from 1.docx from resulting docx" <- with comment "some comment 1 some comment 2"
  • "Some text from 2.docx from resulting docx" <- with comment "some comment 1 some comment 2"

When, i rewrite copying of paragraphs and made slide (+10) for comment_id.

CTP paragraph_object = insert_paragraph.getCTP();

if (!paragraph_object.getCommentRangeStartList().isEmpty()) {
    CTMarkupRange comment_start = paragraph_object.getCommentRangeStartArray(0);
    BigInteger current_comment_id = comment_start.getId();
    BigInteger new_comment_id = current_comment_id.add(slide);

    comment_start.setId(new_comment_id);
    paragraph_object.getCommentRangeEndArray(0).setId(new_comment_id);
}

But from paragraph i cannot change commentReference and my new result docx opened by MS-Word with mistakes and broken inserted comments.

In comments.xml and document.xml of word file i have all new comments with new id (in comments.xml) and correct commentStart/End (in document.xml).

But commentReference remained old, and i have no idea how i can change it and don't know will it help me

mikewoe
  • 270
  • 2
  • 12

0 Answers0