Issue adding comments to DOCX with API/Ruby SDK

I cannot get either the API or Ruby SDK working when attempting to add comments to a word doc. I have tried using simple coordinates 0.0.0.0 and using the coordinates from a text search. For the Ruby SDK, I have used the nodes and document positions returned from the text search directly and built them with the SDK tooling. Regardless of what I do, I am getting this error message back: "Message\": \"Object reference not set to an instance of an object.. Finding text and getting all comments work as expected.

This is the Ruby code I am using:

 range_start = AsposeWordsCloud::DocumentPosition.new({ NodeId: node.range_start.node.node_id, Offset: node.range_start.offset })
    range_end = AsposeWordsCloud::DocumentPosition.new({ NodeId: node.range_end.node.node_id, Offset: node.range_end.offset })
    request_comment = AsposeWordsCloud::CommentInsert.new({
                                                            RangeStart: range_start,
                                                            RangeEnd: range_end,
                                                            Initial: 'IA',
                                                            Author: 'John Doe',
                                                            Text: 'A new Comment'
                                                          })
    begin
      Tempfile.create(['document', '.docx']) do |temp_file_2|
        temp_file_2.binmode
        temp_file_2.write(@document)
        temp_file_2.rewind
        insert_request = AsposeWordsCloud::InsertCommentOnlineRequest.new(document: @document, comment: request_comment)
        @words_api.insert_comment_online(insert_request)
      end
    rescue StandardError => e
      error_info = {
        error: e.message,
        class: e.class.name,
        backtrace: e.backtrace&.take(1)
      }
      error_info[:response_body] = e.response_body if e.respond_to?(:response_body)
      return { success: false, error: "Upload failed: #{error_info.inspect}", debug_info: @debug_info }

This is the version I attempted with the API call:


  def get_aspose_token(client_id, client_secret)
    response = HTTParty.post(
      'https://api.aspose.cloud/connect/token',
      body: {
        grant_type: 'client_credentials',
        client_id: client_id,
        client_secret: client_secret
      }
    )
    raise "Token error: #{response.body}" unless response.code == 200
    JSON.parse(response.body)['access_token']
  end

  def add_comment_via_httparty(temp_file_path, node_id, offset, author, initials, text, token)
    url = 'https://api.aspose.cloud/v4.0/words/online/post/comments'
    comment = {
      'RangeStart' => { 'Node' => { 'NodeId' => node_id }, 'Offset' => offset },
      'RangeEnd'   => { 'Node' => { 'NodeId' => node_id }, 'Offset' => offset },
      'Author'     => author,
      'Initial'    => initials,
      'Text'       => text
    }
    HTTParty.put(
      url,
      headers: { 'Authorization' => "Bearer #{token}" },
      body: {
        'document' => File.open(temp_file_path, 'rb'),
        'comment'  => comment.to_json
      },
      multipart: true
    )
  end

@rachel.cp

It seems you are encountering an issue when trying to add comments to a DOCX document using the Aspose API or Ruby SDK. The error message "Object reference not set to an instance of an object" typically indicates that there is a null reference in your code, which could be due to incorrect handling of the document nodes or positions.

Here are a few suggestions to troubleshoot and resolve the issue:

  1. Check Node and Offset Values: Ensure that the node.range_start.node.node_id and node.range_start.offset values you are using to create the DocumentPosition objects are valid and not null. If these values are derived from a text search, confirm that the search is returning the expected results.

  2. Use Valid Range for Comments: When creating comments, ensure that the RangeStart and RangeEnd positions are correctly defined. Both should point to valid nodes within the document. If they are the same, it may lead to issues. You might want to try using different nodes or offsets for the start and end positions.

  3. Review the API Documentation: Make sure you are following the correct structure for the comment object as per the API documentation. The comment should be properly constructed with all required fields filled.

  4. Example of Adding Comments: Here’s a simplified example of how to add comments using the Aspose API in C#. This might help you understand the structure better:

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.Write("Some text is added.");
    
    Comment comment = new Comment(doc, "Author Name", "Initials", DateTime.Now);
    comment.Paragraphs.Add(new Paragraph(doc));
    comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text."));
    
    // Insert comment range start and end
    CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id);
    CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id);
    builder.CurrentParagraph.AppendChild(commentRangeStart);
    builder.CurrentParagraph.AppendChild(commentRangeEnd);
    builder.CurrentParagraph.AppendChild(comment);
    
    doc.Save("output.docx");
    
  5. Debugging: Add debugging statements to log the values of node.range_start and node.range_end before you create the DocumentPosition objects. This will help you identify if any of these values are null or incorrect.

If you continue to face issues, consider sharing more details about the specific node and offset values you are using, as well as any additional error messages you might be receiving.

If you need further assistance, feel free to ask!

Sources:
[1]: Working with Comments