Module: Asposewordsjavaforruby::ProcessComments

Defined in:
lib/asposewordsjavaforruby/processcomments.rb

Instance Method Summary collapse

Instance Method Details

#extract_comments(doc) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/asposewordsjavaforruby/processcomments.rb', line 25

def extract_comments(doc)
    # Call method
    collected_comments  = Rjb::import('java.util.ArrayList').new

    # Collect all comments in the document
    node_type = Rjb::import('com.aspose.words.NodeType')
    comments = doc.getChildNodes(node_type.COMMENT, true).toArray()
    
    save_format = Rjb::import('com.aspose.words.SaveFormat')
    
    comments.each do |comment|
        author = comment.getAuthor()
        date_time = comment.getDateTime()
        format = comment.toString(save_format.TEXT)
        puts "Author:" + author.to_s + " DateTime:" + date_time.to_string + " Comment:" + format.to_s
    end 
end

#initializeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/asposewordsjavaforruby/processcomments.rb', line 3

def initialize()
    # The path to the documents directory.
    data_dir = File.dirname(File.dirname(File.dirname(__FILE__))) + '/data/'
    
    # Open the document.
    doc = Rjb::import('com.aspose.words.Document').new(data_dir + 'TestComments.doc')

    # Get all comments from document
    extract_comments(doc)
          
    # Remove comments by the "pm" author.
    remove_comment($doc, "pm");
    puts "Comments from 'pm' are removed!"
    
    # Remove all comments.
    remove_comments(doc)
    puts "All comments are removed!"

    # Save the document.
    doc.save(data_dir + "TestComments Out.doc")    
end

#remove_comment(doc, author_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/asposewordsjavaforruby/processcomments.rb', line 43

def remove_comment(doc, author_name)
    raise 'author_name not specified.' if author_name.empty?

    # Collect all comments in the document
    node_type = Rjb::import('com.aspose.words.NodeType')
    comments = doc.getChildNodes(node_type.COMMENT, true)
    comments_count = comments.getCount()
    
    # Look through all comments and remove those written by the authorName author.
    i = comments_count
    i = i - 1
    while (i >= 0) do
        comment = comments.get(i)
        author = comment.getAuthor().chomp('"').reverse.chomp('"').reverse
        if (author == author_name) then
            comment.remove()
        end
        i = i - 1
    end
end

#remove_comments(doc) ⇒ Object



64
65
66
67
68
69
# File 'lib/asposewordsjavaforruby/processcomments.rb', line 64

def remove_comments(doc)
    # Collect all comments in the document
    node_type = Rjb::import('com.aspose.words.NodeType')
    comments = doc.getChildNodes(node_type.COMMENT, true)
    comments.clear()
end