Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Apr 23, 2022 12:13 pm

Screenshot from 2022-04-23 12-25-17.png
I am using spire.doc's free version for doing some operations on a docx file.
I am trying to insert comments to a text in a docx file.
But my comments will be like paragraphs which I am storing in array and then passing those array elements one by one in an iteration.
Below is my code snippet

Code: Select all
protected void createGrammarCheckedDocument(ArrayList<String> textToBeProcessed, S3ObjectInputStream objectInputStream, ArrayList<String> textToBeReplaced, Job job) {
        System.out.println("Text to be processed ===> " + textToBeProcessed.size());
        System.out.println("Text to be replaced  ==> " + textToBeReplaced);
        Document documentToBeCreated = new Document(objectInputStream);
        try {
            for(int i = 0 ; i < textToBeProcessed.size(); i++) {
                for (int j = 0; j < textToBeReplaced.size(); j++) {
                    //Find the specific string to add comment
                    TextSelection textSelection = documentToBeCreated.findString(textToBeProcessed.get(i), false, true);
                    System.out.println("String in line  ==> " + textSelection);
                    //Create a start mark
                    CommentMark commentMarkStart = new CommentMark(documentToBeCreated);
                    commentMarkStart.setType(CommentMarkType.Comment_Start);

                    //Create an end mark
                    CommentMark commentMarkEnd = new CommentMark(documentToBeCreated);
                    commentMarkEnd.setType(CommentMarkType.Comment_End);

                    //Create a comment
                    Comment comment = new Comment(documentToBeCreated);
                    comment.getBody().addParagraph().setText(textToBeReplaced.get(j));
                    comment.getFormat().setAuthor("PaperTrue");

                    //Find the paragraph where the string is located
                    Paragraph paragraph = textSelection.getAsOneRange().getOwnerParagraph();

                    //Get the index of the string in the paragraph
                    int index = paragraph.getChildObjects().indexOf(textSelection.getAsOneRange());

                    //Add the comment to the paragraph
                    paragraph.getChildObjects().add(comment);

                    paragraph.getChildObjects().insert(index, commentMarkStart);
                    paragraph.getChildObjects().insert(index + 2, commentMarkEnd);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        documentToBeCreated.saveToFile(commonFileSavingProcess("#" + job.getJobNo() + " GrammarChecked.docx", job));
    }


The comments are generated, but the created file is not in proper format
below are the snapshots of my output file,

SiddhantB
 
Posts: 1
Joined: Sat Apr 23, 2022 11:58 am

Sun Apr 24, 2022 9:21 am

Hi,

Thank you for your inquiry.
I did an initial test on my side, but didn't find any format problems. When creating multiple comments in a loop, you need to ensure that each comment has a different ID, please add the code below.
Code: Select all
             //Set start mark ID
             commentMarkStart.setCommentId(num);
             //Set end mark ID
             commentMarkEnd.setCommentId(num);


In addition, you are using the free version, I suggest that you try the latest commercial version (Spire.Doc for Java Version:5.4.2), it is more stable than the free one. If the problem still exists, please provide the following information for further investigation. You can attach your file here or send it to us via email (support@e-iceblue.com). Thank you in advance.
1) The specific content of the array textToBeProcessed
2) Your input and output document

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.Doc